using BPASmartClient.Message;
using System.Text;

namespace BPASmartClient.JakaRobot
{
    public class JaKaHelper
    {

        #region 果汁机设备
        /*public const string SENCE_取咖啡杯     = "10000";
        public const string SENCE_取果汁杯     = "10001";
        public const string SENCE_取茶水杯     = "10002";
        public const string SENCE_取水杯       = "10003";
        public const string SENCE_取咖啡杯检测 = "11000";
        public const string SENCE_取果汁杯检测 = "11001";
        public const string SENCE_取茶水杯检测 = "11002";
        public const string SENCE_取水杯检测   = "11003";

        public const string SENCE_接咖啡       = "12000";
        public const string SENCE_接果汁1      = "12001";
        public const string SENCE_接果汁2      = "12002";
        public const string SENCE_接果汁3      = "12003";
        public const string SENCE_接果汁4      = "12004";
        public const string SENCE_接茶         = "12005";
        public const string SENCE_接水         = "12006";

        public const string SENCE_放咖啡杯     = "13000";
        public const string SENCE_放果汁杯1    = "13001";
        public const string SENCE_放果汁杯2    = "13002";
        public const string SENCE_放果汁杯3    = "13003";
        public const string SENCE_放果汁杯4    = "13004";
        public const string SENCE_放茶水杯     = "13005";
        public const string SENCE_放水杯       = "13006";*/

        //public const string SENCE_取杯 = "10000";
        //public const string SENCE_放杯位检测 = "10100";
        //public const string SENCE_取杯检测 = "11000";

        //public const string SENCE_接咖啡 = "12000";
        //public const string SENCE_接果汁1 = "12001";
        //public const string SENCE_接果汁2 = "12002";
        //public const string SENCE_接果汁3 = "12003";
        //public const string SENCE_接果汁4 = "12004";
        //public const string SENCE_接茶 = "12005";
        //public const string SENCE_接水 = "12006";
        //public const string SENCE_接茶_接水 = "12007";

        //public const string SENCE_放咖啡杯 = "13000";//接饮料位---放饮料过渡位
        //public const string SENCE_放果汁杯1 = "13001";
        //public const string SENCE_放果汁杯2 = "13002";
        //public const string SENCE_放果汁杯3 = "13003";
        //public const string SENCE_放果汁杯4 = "13004";
        //public const string SENCE_放茶水杯 = "13005";

        //public const string SENCE_放杯 = "14000";//放杯过渡位到-放杯

        //public const string SENCE_放杯检测 = "15000";

        //public const string SENCE_初始位 = "20000";

        #endregion
        private int rshd = -1;
        private bool login = false;

        /// <summary>
        /// 机器人的状态
        /// </summary>
        private JKTYPE.RobotStatus robot_Status;
        private JKTYPE.ProgramState program_status;
        //private volatile static JaKaHelper _Instance;
        //public static JaKaHelper GetInstance => _Instance ?? (_Instance = new JaKaHelper());
        //private JaKaHelper() { }
        public bool IsIdle { get; set; } = false;
        public bool IsConnected { get { return login; } }

        public void Connect(string ip)
        {
            bool ErrorFlag = false;
            while (rshd == -1 || login == false)
            {
                try
                {
                    jakaAPI.create_handler(ip.ToCharArray(), ref rshd);

                    login = true;

                }
                catch (Exception ex)
                {
                    if (!ErrorFlag)
                    {
                        MessageLog.GetInstance.ShowEx(ex.ToString());
                        ErrorFlag = true;
                        login = false;
                    }
                    Thread.Sleep(3000);
                }
            }
            if (login)
            {
                try
                {
                    Power_On();//打开机器人电源
                    Thread.Sleep(1000);
                    Enable_robot();//机器人上使能
                    MessageLog.GetInstance.Show("机器人已上电使能");
                }
                catch (Exception ex)
                {
                    MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
                }
            }
        }
        public void Power_On()
        {
            if (login)
            {
                try
                {
                    jakaAPI.power_on(ref rshd);
                }
                catch (System.AccessViolationException ave)
                {
                    MessageLog.GetInstance.ShowEx(ave.ToString());
                }
                //catch (Exception ex)
                //{
                //    MessageLog.GetInstance.ShowEx(ex.ToString());
                //}
            }
            else
            {
                MessageLog.GetInstance.Show("jaka机器人未连接成功！");
            }
        }

        public void Power_Off()
        {
            if (login)
            {
                try
                {
                    jakaAPI.power_off(ref rshd);
                }
                catch (Exception ex)
                {

                }
            }
            else
            {
                MessageLog.GetInstance.Show("jaka机器人未连接成功！");
            }
        }

        public void Enable_robot()
        {
            if (!(rshd == -1))
            {
                try
                {
                    jakaAPI.enable_robot(ref rshd);
                }
                catch (Exception ex)
                {

                }
            }
            else
            {
                MessageLog.GetInstance.Show("jaka机器人未连接成功！");
            }
        }

        public void disEnable_robot()
        {
            if (!(rshd == -1))
            {
                try
                {
                    jakaAPI.disable_robot(ref rshd);
                }
                catch (Exception ex)
                {

                }
            }
            else
            {
                MessageLog.GetInstance.Show("jaka机器人未连接成功！");
            }
        }

        /// <summary>
        /// 设置机器人的DO值
        /// </summary>
        /// <param name="DOIndex"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Set_RobotDO(int DOIndex, bool value)
        {
            try
            {
                jakaAPI.set_digital_output(ref rshd, JKTYPE.IOType.IO_CABINET, DOIndex, value);
                return value;

            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 设置机器人的AO值
        /// </summary>
        /// <param name="value">设置值</param>
        /// <returns></returns>
        public int Set_RobotAO1(int Value)
        {
            try
            {
                return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, Value);

            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        /// <summary>
        /// 设置机器人的AO值
        /// </summary>
        /// <param name="Index">索引值</param>
        /// <param name="value">设置值</param>
        /// <returns></returns>
        public int Set_RobotAO(int Index, int Value)
        {
            try
            {
                return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, Value);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取机器人的AO值
        /// </summary>
        /// <param name="Index">索引值</param>
        /// <param name="value">设置值</param>
        /// <returns></returns>
        public float Get_RobotAO(int Index, int value)
        {
            try
            {
                float bResult = 0;
                jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, ref bResult);
                return bResult;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取机器人的AO值
        /// </summary>
        /// <param name="Index">索引值</param>
        /// <param name="value">设置值</param>
        /// <returns></returns>
        public float Get_RobotAO1()
        {
            try
            {
                float bResult = 0;
                jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, ref bResult);
                return bResult;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取机器人的DI输入
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool Get_RobotDI(int index)
        {
            bool bResult = false;
            jakaAPI.get_digital_input(ref rshd, JKTYPE.IOType.IO_CABINET, index, ref bResult);
            return bResult;
        }

        /// <summary>
        /// 获取机器人的状态
        /// </summary>
        public void GetRobotStatus()
        {
            jakaAPI.get_robot_status(ref rshd, ref robot_Status);
        }

        public JKTYPE.ProgramState GetProgramStatus()
        {
            jakaAPI.get_program_state(ref rshd, ref program_status);
            return program_status;

        }

        StringBuilder jakafile;
        JKTYPE.ProgramState status;
        char[] file;
        public void JaKaProgramName(String Programname)
        {
            try
            {
                //加载
                file = Programname.ToCharArray();
                MessageLog.GetInstance.Show($"调用文件名：{Programname}");
                status = new JKTYPE.ProgramState();
                jakaAPI.get_program_state(ref rshd, ref status);
                if (status != JKTYPE.ProgramState.PROGRAM_IDLE)
                {
                    MessageLog.GetInstance.Show($"程序运行中，无法加载程序!!!");
                }
                else
                {
                    if (jakaAPI.program_load(ref rshd, file) == 0)
                    {
                        MessageLog.GetInstance.Show($"加载程序完成");
                    }
                    else
                    {
                        MessageLog.GetInstance.Show($"加载程序失败！！！");
                    }
                }
                if (status == JKTYPE.ProgramState.PROGRAM_RUNNING)
                {
                    MessageLog.GetInstance.Show($"程序处于已处于启动状态！！！");
                }
                else if (status == JKTYPE.ProgramState.PROGRAM_IDLE)
                {
                    jakafile = new StringBuilder();
                    jakaAPI.get_loaded_program(ref rshd, jakafile);
                    if (jakafile.Length == 0)
                    {
                        MessageLog.GetInstance.Show($"未加载程序，无法运行！！！");
                    }
                    else
                    {
                        MessageLog.GetInstance.Show($"当前加载程序为：" + jakafile);
                        jakaAPI.program_run(ref rshd);
                        MessageLog.GetInstance.Show($"程序启动");
                    }
                }
                else
                {
                    MessageLog.GetInstance.Show($"程序处于暂停状态，无法重新启动程序！！！");
                }
            }
            catch (Exception ex)
            {
                MessageLog.GetInstance.ShowEx(ex.ToString());
            }
        }
    }
}