using Susi4.APIs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace irisIoControll.Service.Impl
{
    public class susiControl
    {
        class DeviceInfo
        {
            public UInt32 ID;
            public UInt32 SupportInput;
            public UInt32 SupportOutput;

            public DeviceInfo(UInt32 DeviceID)
            {
                ID = DeviceID;
                SupportInput = 0;
                SupportOutput = 0;
            }
        }

        class DevPinInfo
        {
            public UInt32 ID;

            private string _Name = "";
            public string Name
            {
                get { return _Name; }
            }

            override public string ToString()
            {
                return String.Format("{0} ({1})", ID, Name);
            }

            public DevPinInfo(UInt32 DeviceID)
            {
                ID = DeviceID;

                UInt32 Length = 32;
                StringBuilder sb = new StringBuilder((int)Length);
                if (SusiBoard.SusiBoardGetStringA(SusiBoard.SUSI_ID_MAPPING_GET_NAME_GPIO(ID), sb, ref Length) == SusiStatus.SUSI_STATUS_SUCCESS)
                {
                    _Name = sb.ToString();
                }
            }
        }

        const int MAX_BANK_NUM = 4;

        List<DeviceInfo> DevList = new List<DeviceInfo>();
        DeviceInfo Dev = null;
        List<DevPinInfo> DevPinList = new List<DevPinInfo>();
        DevPinInfo DevPin = null;

        public bool Available
        {
            get { return (DevList.Count > 0); }
        }

        public static bool SusiInitializer()
        {
            try
            {
                UInt32 Status = SusiLib.SusiLibInitialize();

                if (Status != SusiStatus.SUSI_STATUS_SUCCESS && Status != SusiStatus.SUSI_STATUS_INITIALIZED)
                    return false;

                SetDir(0, 2);//红灯
                SetDir(0, 3);//绿灯
                SetDir(0, 7);//黄灯
                SetDir(1, 6);//人体感应模块
            }
            catch (Exception e)
            {
                string msg = "主板I/O口初始化失败！" + e.Message;
                MessageBox.Show(msg);
                //LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, msg);    

                return false;
            }
            return true;
        }


        public static void SetLevel(UInt32 level, UInt32 id)
        {
            UInt32 Status;
            Status = SusiGPIO.SusiGPIOSetLevel(id, 1, level);
            if (Status != SusiStatus.SUSI_STATUS_SUCCESS)
                MessageBox.Show(String.Format("SusiGPIOSetLevel() failed. (0x{0:X8})", Status));
        }

        public static UInt32 GetLevel(UInt32 id)
        {
            UInt32 Status;
            UInt32 Value;

            Status = SusiGPIO.SusiGPIOGetLevel(id, 1, out Value);
            return Value;
        }

        public static void SetDir(UInt32 Value, UInt32 id)
        {
            UInt32 Status;
            Status = SusiGPIO.SusiGPIOSetDirection(id, 1, Value);
            if (Status != SusiStatus.SUSI_STATUS_SUCCESS)
                MessageBox.Show(String.Format("SusiGPIOSetDirection() failed. (0x{0:X8})", Status));
        }

        public static void setBacklightEnable(int id, int enable)
        {
            SusiVga.SusiVgaSetBacklightEnable((uint)id, (uint)enable);
        }

    }
}
