using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonClassLib
{
    public class LEDHelper
    {
        //string ip = "192.168.1.141";

        /// <summary>
        /// 射频门创建LED节目
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="logoPath"></param>
        /// <param name="sResult"></param>
        /// <returns></returns>
        public static bool CreateScanScreen(string IP,string logoPath,out string sResult)
        {
            byte[] byScreenName = System.Text.Encoding.UTF8.GetBytes("ScanScreen");

            int nRet = ListenDll.LV_CreateScreen(byScreenName);

            if (nRet != 0)
            {
                sResult="创建屏幕失败"+ GetErrInfo();
                return false;
            }

            byte[] byProgramId = System.Text.Encoding.UTF8.GetBytes("ScanProgram");

            page pageInfo = new page();
            pageInfo.pageId = 0;
            pageInfo.resolution = "256X64";
            pageInfo.bgColor = "#000000";
            string strBgFileName = "";//本地的一个图片文件
            pageInfo.bgImg = strBgFileName;
            pageInfo.eqType = "Q5";
            pageInfo.pageTime = 20;
            pageInfo.guid = Guid.NewGuid().ToString();

            string strPageInfo = JsonExtension.ToJSON(pageInfo);
            byte[] byPageInfo = System.Text.Encoding.UTF8.GetBytes(strPageInfo);

            nRet = ListenDll.LV_AddPageToProgram(byScreenName, byProgramId, byPageInfo);

            if (nRet != 0)
            {
                sResult = "添加页面失败" + GetErrInfo();
                return false;
            }

            PicArea picArea = new PicArea();

            picArea.id = 1;
            picArea.type = 1;
            picArea.borderEffect = 0;
            picArea.borderSW = 0;
            picArea.borderSpeed = 0;
            picArea.borderType = 0;
            picArea.left = 2;
            picArea.top = 2;
            picArea.width = 60;
            picArea.height = 60;
            picArea.pauseTime = 5;
            picArea.rotation = 0;
            //picArea.siderType = 6;//老的特效,设备系统版本低于4.4.7支持

            //下面4项为新特效模式,设备系统版本4.4.7及以上才支持
            picArea.picStyleIn = 0;//特效为0时,特效时间无效
            picArea.picStyleInTime = 10 * 1000;
            picArea.picStyleOut = 0;//特效为0时,特效时间无效
            picArea.picStyleOutTime = 10 * 1000;


            picArea.picScaleType = 0;


            picArea.srcGroup = new List<PicArea.SrcGroup>();
            PicArea.SrcGroup s = new PicArea.SrcGroup();
            s.Id = 1;

            s.src = logoPath;
            picArea.srcGroup.Add(s);
            picArea.zIndex = 1;
            string strPicjson = JsonExtension.ToJSON(picArea);//对象转为Json

            byte[] byPicInfo = System.Text.Encoding.UTF8.GetBytes(strPicjson);

            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byPicInfo);

            if (nRet != 0)
            {
                sResult = "添加图片失败" + GetErrInfo();
                return false;
            }

            //射频门
            TextArea textArea = new TextArea();

            textArea.id = 2;
            textArea.type = 2;
            textArea.borderEffect = 0;
            textArea.borderSW = 0;
            textArea.borderSpeed = 0;
            textArea.borderType = 0;
            textArea.left = 64;
            textArea.top = 15;
            textArea.width = 128;
            textArea.height = 30;

            textArea.background = "#000000";
            textArea.fontColor = "#00ff00";
            textArea.fontFamily = "宋体";
            textArea.fontSize = 30;
            textArea.italic = 0;
            textArea.bold = 0;
            textArea.textLine = 0;
            textArea.lineHeight = 12;
            textArea.pauseTime = 5;
            textArea.scrollSpeed = 1;
            textArea.siderType = 0;
            textArea.textAlign = 1;
            textArea.colorEffect = 0;
            textArea.textTop = 1;
            textArea.rotation = 0;
            textArea.text = "射频门";
            textArea.textShow = 0;//0: 多行 1:单行

            textArea.zIndex = 1;
            string strTextjson = JsonExtension.ToJSON(textArea);//对象转为Json

            byte[] byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);


            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加字幕失败" + GetErrInfo();
                return false;
            }

            //箱表
            TextArea textArea2 = new TextArea();

            textArea2.id = 3;
            textArea2.type = 2;
            textArea2.borderEffect = 0;
            textArea2.borderSW = 0;
            textArea2.borderSpeed = 0;
            textArea2.borderType = 0;
            textArea2.left = 192;
            textArea2.top = 5;
            textArea2.width = 64;
            textArea2.height = 54;

            textArea2.background = "#000000";
            textArea2.fontColor = "#ff0000";
            textArea2.fontFamily = "宋体";
            textArea2.fontSize = 20;
            textArea2.italic = 0;
            textArea2.bold = 0;
            textArea2.textLine = 0;
            textArea2.lineHeight = 12;
            textArea2.pauseTime = 5;
            textArea2.scrollSpeed = 1;
            textArea2.siderType = 0;
            textArea2.textAlign = 0;
            textArea2.colorEffect = 0;
            textArea2.textTop = 1;
            textArea2.rotation = 0;
            textArea2.text = "箱:\r\n表:";
            //textArea2.text = "";
            textArea2.textShow = 0;//0: 多行 1:单行

            textArea2.zIndex = 1;
            strTextjson = JsonExtension.ToJSON(textArea2);//对象转为Json

            byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);



            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加字幕失败"+GetErrInfo();
                return false;
            }

            //箱
            StaticTextArea staticTextAreaBox = new StaticTextArea();

            staticTextAreaBox.id = 4;
            staticTextAreaBox.type = 200;//静态区域和字幕区域属性有通用的地方.但大部分

            staticTextAreaBox.left = 220;
            staticTextAreaBox.top = 5;
            staticTextAreaBox.width = 30;
            staticTextAreaBox.height = 27;



            staticTextAreaBox.background = "#000000";
            staticTextAreaBox.fontColor = "#ff0000";
            staticTextAreaBox.fontFamily = "宋体";//当前只支持默认字体.字体需要设备端有对应字库才能支持
            staticTextAreaBox.fontSize = 20;

            staticTextAreaBox.text = "0";
            //staticTextAreaBox.text = "";

            staticTextAreaBox.zIndex = 1;
            strTextjson = JsonExtension.ToJSON(staticTextAreaBox);//对象转为Json

            byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);

            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加静态区域失败" + GetErrInfo();
                return false;
            }

            //表
            StaticTextArea staticTextAreaGoods = new StaticTextArea();

            staticTextAreaGoods.id = 5;
            staticTextAreaGoods.type = 200;//静态区域和字幕区域属性有通用的地方.但大部分

            staticTextAreaGoods.left = 220;
            staticTextAreaGoods.top = 36;
            staticTextAreaGoods.width = 30;
            staticTextAreaGoods.height = 27;



            staticTextAreaGoods.background = "#000000";
            staticTextAreaGoods.fontColor = "#ff0000";
            staticTextAreaGoods.fontFamily = "宋体";//当前只支持默认字体.字体需要设备端有对应字库才能支持
            staticTextAreaGoods.fontSize = 20;
            //staticTextAreaGoods.italic = 0;
            //staticTextAreaGoods.bold = 0;
            //staticTextAreaGoods.textLine = 0;
            staticTextAreaGoods.text = "0";
            //staticTextAreaGoods.text = "";

            staticTextAreaGoods.zIndex = 1;
            strTextjson = JsonExtension.ToJSON(staticTextAreaGoods);//对象转为Json

            byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);

            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加静态区域失败" + GetErrInfo();
                return false;
            }

            //发送节目
            byte[] byIp = System.Text.Encoding.UTF8.GetBytes(IP);
            byte[] byProgramName = System.Text.Encoding.UTF8.GetBytes("ScanProgramName");

            byte[] byPassword = System.Text.Encoding.UTF8.GetBytes("");

            nRet = ListenDll.LV_SendProgramToDevice(byScreenName, byIp, byProgramId, byProgramName, byPassword);

            if (nRet != 0)
            {
                sResult = "发送节目失败" + GetErrInfo();
                return false;
            }

            //发送计划
            List<ProgramPlan> listPlan = new List<ProgramPlan>();

            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

            ProgramPlan plan1 = new ProgramPlan();
            plan1.createDate = Convert.ToInt64(ts.TotalMilliseconds);
            plan1.programId = "ScanProgram";
            plan1.programName = "ScanProgramName";
            plan1.programType = 1;
            plan1.updateDate = Convert.ToInt64(ts.TotalMilliseconds);
            plan1.date = new List<ProgramPlan.Date>();
            plan1.planWeekDay = new List<int>();
            plan1.time = new List<ProgramPlan.Time>();
            plan1.isDefault = 0;

            listPlan.Add(plan1);


            string strPlanJson = JsonExtension.ToJSON(listPlan);//对象转为Json
            byte[] byPlanInfo = System.Text.Encoding.UTF8.GetBytes(strPlanJson);
            string strDefVideo = "";//如果是默认节目,同时默认节目中存在视频文件,该字符串才不为空字符串
            byte[] byDefVideo = System.Text.Encoding.UTF8.GetBytes(strDefVideo);

            nRet = ListenDll.LV_SendProgramPlan(byIp, byPlanInfo, byDefVideo);

            if (nRet != 0)
            {
                sResult = "发送计划失败" + GetErrInfo();
                return false;
            }

            sResult = string.Empty;
            return true;
        }
        /// <summary>
        /// 更新射频门LED节目
        /// </summary>
        /// <param name="boxCount"></param>
        /// <param name="goodsCount"></param>
        /// <param name="ip"></param>
        /// <param name="sResult"></param>
        /// <returns></returns>
        public static bool refreshScanScreen(string boxCount, string goodsCount, string ip,out string sResult)
        {
            RefreshData refreshData = new RefreshData();
            refreshData.areaId = 4;//区域ID必须与节目中的静态文本区域ID一致才能刷新
            refreshData.text = boxCount;
            refreshData.fontColor = "#ff0000";
            refreshData.refreshTimes = 0;
            refreshData.base64 = 0;

            RefreshData refreshData2 = new RefreshData();
            refreshData2.areaId = 5;//区域ID必须与节目中的静态文本区域ID一致才能刷新
            refreshData2.text = goodsCount;
            refreshData2.fontColor = "#ff0000";
            refreshData2.refreshTimes = 0;
            refreshData2.base64 = 0;

            List<RefreshData> list = new List<RefreshData>();
            list.Add(refreshData);
            list.Add(refreshData2);

            string strRefreshData = JsonExtension.ToJSON(list);//对象转为Json

            byte[] byRefreshData = System.Text.Encoding.UTF8.GetBytes(strRefreshData);
            byte[] byIp = System.Text.Encoding.UTF8.GetBytes(ip);
            int nRet = ListenDll.LV_RefreshFixedArea(byIp, byRefreshData);

            if (nRet != 0)
            {
                sResult = "创建屏幕失败" + GetErrInfo();
                return false;
            }

            nRet = ListenDll.LV_CleanProgramTemporary();

            if (nRet != 0)
            {
                sResult = "清除缓存失败" + GetErrInfo();
                return false;
            }

            sResult = string.Empty;
            return true;

        }


        /// <summary>
        /// 拆码垛机更新LED节目
        /// </summary>
        /// <param name="count"></param>
        /// <param name="totalCount"></param>
        /// <param name="ip"></param>
        /// <param name="sResult"></param>
        /// <returns></returns>
        public static bool refreshUnpack(string count, string totalCount, string ip, out string sResult)
        {
            RefreshData refreshData = new RefreshData();
            refreshData.areaId = 4;//区域ID必须与节目中的静态文本区域ID一致才能刷新
            refreshData.text = count;
            refreshData.fontColor = "#ff0000";
            refreshData.refreshTimes = 0;
            refreshData.base64 = 0;

            RefreshData refreshData2 = new RefreshData();
            refreshData2.areaId = 5;//区域ID必须与节目中的静态文本区域ID一致才能刷新
            refreshData2.text = totalCount;
            refreshData2.fontColor = "#ff0000";
            refreshData2.refreshTimes = 0;
            refreshData2.base64 = 0;

            List<RefreshData> list = new List<RefreshData>();
            list.Add(refreshData);
            list.Add(refreshData2);

            string strRefreshData = JsonExtension.ToJSON(list);//对象转为Json

            byte[] byRefreshData = System.Text.Encoding.UTF8.GetBytes(strRefreshData);
            byte[] byIp = System.Text.Encoding.UTF8.GetBytes(ip);
            int nRet = ListenDll.LV_RefreshFixedArea(byIp, byRefreshData);

            if (nRet != 0)
            {
                sResult = "创建屏幕失败" + GetErrInfo();
                return false;
            }

            nRet = ListenDll.LV_CleanProgramTemporary();

            if (nRet != 0)
            {
                sResult = "清除缓存失败" + GetErrInfo();
                return false;
            }

            sResult = string.Empty;
            return true;
        }
        /// <summary>
        /// 拆码垛机创建LED节目
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="logoPath"></param>
        /// <param name="sResult"></param>
        /// <returns></returns>
        public static bool CreateUnpackScreen(string IP, string logoPath, out string sResult)
        {
            byte[] byScreenName = System.Text.Encoding.UTF8.GetBytes("ScanScreen");

            int nRet = ListenDll.LV_CreateScreen(byScreenName);

            if (nRet != 0)
            {
                sResult = "创建屏幕失败"+GetErrInfo();
                return false;
            }

            byte[] byProgramId = System.Text.Encoding.UTF8.GetBytes("ScanProgram");

            page pageInfo = new page();
            pageInfo.pageId = 0;
            pageInfo.resolution = "256X64";
            pageInfo.bgColor = "#000000";
            string strBgFileName = "";//本地的一个图片文件
            pageInfo.bgImg = strBgFileName;
            pageInfo.eqType = "Q5";
            pageInfo.pageTime = 20;
            pageInfo.guid = Guid.NewGuid().ToString();

            string strPageInfo = JsonExtension.ToJSON(pageInfo);
            byte[] byPageInfo = System.Text.Encoding.UTF8.GetBytes(strPageInfo);

            nRet = ListenDll.LV_AddPageToProgram(byScreenName, byProgramId, byPageInfo);

            if (nRet != 0)
            {
                sResult = "添加页面失败" + GetErrInfo();
                return false;
            }

            PicArea picArea = new PicArea();

            picArea.id = 1;
            picArea.type = 1;
            picArea.borderEffect = 0;
            picArea.borderSW = 0;
            picArea.borderSpeed = 0;
            picArea.borderType = 0;
            picArea.left = 2;
            picArea.top = 2;
            picArea.width = 60;
            picArea.height = 60;
            picArea.pauseTime = 5;
            picArea.rotation = 0;
            //picArea.siderType = 6;//老的特效,设备系统版本低于4.4.7支持

            //下面4项为新特效模式,设备系统版本4.4.7及以上才支持
            picArea.picStyleIn = 0;//特效为0时,特效时间无效
            picArea.picStyleInTime = 10 * 1000;
            picArea.picStyleOut = 0;//特效为0时,特效时间无效
            picArea.picStyleOutTime = 10 * 1000;


            picArea.picScaleType = 0;


            picArea.srcGroup = new List<PicArea.SrcGroup>();
            PicArea.SrcGroup s = new PicArea.SrcGroup();
            s.Id = 1;

            s.src = logoPath;
            picArea.srcGroup.Add(s);
            picArea.zIndex = 1;
            string strPicjson = JsonExtension.ToJSON(picArea);//对象转为Json

            byte[] byPicInfo = System.Text.Encoding.UTF8.GetBytes(strPicjson);

            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byPicInfo);

            if (nRet != 0)
            {
                sResult = "添加页面失败"+ GetErrInfo();
                return false;
            }
            //拆垛机
            TextArea textArea = new TextArea();

            textArea.id = 2;
            textArea.type = 2;
            textArea.borderEffect = 0;
            textArea.borderSW = 0;
            textArea.borderSpeed = 0;
            textArea.borderType = 0;
            textArea.left = 64;
            textArea.top = 15;
            textArea.width = 128;
            textArea.height = 30;

            textArea.background = "#000000";
            textArea.fontColor = "#00ff00";
            textArea.fontFamily = "宋体";
            textArea.fontSize = 30;
            textArea.italic = 0;
            textArea.bold = 0;
            textArea.textLine = 0;
            textArea.lineHeight = 12;
            textArea.pauseTime = 5;
            textArea.scrollSpeed = 1;
            textArea.siderType = 0;
            textArea.textAlign = 1;
            textArea.colorEffect = 0;
            textArea.textTop = 1;
            textArea.rotation = 0;
            textArea.text = "拆垛机";
            textArea.textShow = 0;//0: 多行 1:单行

            textArea.zIndex = 1;
            string strTextjson = JsonExtension.ToJSON(textArea);//对象转为Json

            byte[] byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);


            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);
            //MessageBox.Show(nRet == 0 ? "添加字幕成功\r\n Text added successfully" : "添加字幕失败\r\nText added failed");

            //第共
            TextArea textArea2 = new TextArea();

            textArea2.id = 3;
            textArea2.type = 2;
            textArea2.borderEffect = 0;
            textArea2.borderSW = 0;
            textArea2.borderSpeed = 0;
            textArea2.borderType = 0;
            textArea2.left = 192;
            textArea2.top = 5;
            textArea2.width = 64;
            textArea2.height = 54;

            textArea2.background = "#000000";
            textArea2.fontColor = "#ff0000";
            textArea2.fontFamily = "宋体";
            textArea2.fontSize = 20;
            textArea2.italic = 0;
            textArea2.bold = 0;
            textArea2.textLine = 0;
            textArea2.lineHeight = 12;
            textArea2.pauseTime = 5;
            textArea2.scrollSpeed = 1;
            textArea2.siderType = 0;
            textArea2.textAlign = 0;
            textArea2.colorEffect = 0;
            textArea2.textTop = 1;
            textArea2.rotation = 0;
            textArea2.text = "第  箱\r\n共  箱";
            //textArea2.text = "";
            textArea2.textShow = 0;//0: 多行 1:单行

            textArea2.zIndex = 1;
            strTextjson = JsonExtension.ToJSON(textArea2);//对象转为Json

            byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);



            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加字幕失败" + GetErrInfo();
                return false;
            }
            //第
            StaticTextArea staticTextAreaBox = new StaticTextArea();

            staticTextAreaBox.id = 4;
            staticTextAreaBox.type = 200;//静态区域和字幕区域属性有通用的地方.但大部分

            staticTextAreaBox.left = 211;
            staticTextAreaBox.top = 1;
            staticTextAreaBox.width = 30;
            staticTextAreaBox.height = 27;



            staticTextAreaBox.background = "#000000";
            staticTextAreaBox.fontColor = "#ff0000";
            staticTextAreaBox.fontFamily = "宋体";//当前只支持默认字体.字体需要设备端有对应字库才能支持
            staticTextAreaBox.fontSize = 20;

            staticTextAreaBox.text = "5";
            //staticTextAreaBox.text = "";

            staticTextAreaBox.zIndex = 1;
            strTextjson = JsonExtension.ToJSON(staticTextAreaBox);//对象转为Json

            byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);

            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加静态区域失败" + GetErrInfo();
                return false;
            }
            //共
            StaticTextArea staticTextAreaGoods = new StaticTextArea();

            staticTextAreaGoods.id = 5;
            staticTextAreaGoods.type = 200;//静态区域和字幕区域属性有通用的地方.但大部分

            staticTextAreaGoods.left = 211;
            staticTextAreaGoods.top = 33;
            staticTextAreaGoods.width = 30;
            staticTextAreaGoods.height = 27;



            staticTextAreaGoods.background = "#000000";
            staticTextAreaGoods.fontColor = "#ff0000";
            staticTextAreaGoods.fontFamily = "宋体";//当前只支持默认字体.字体需要设备端有对应字库才能支持
            staticTextAreaGoods.fontSize = 20;
            //staticTextAreaGoods.italic = 0;
            //staticTextAreaGoods.bold = 0;
            //staticTextAreaGoods.textLine = 0;
            staticTextAreaGoods.text = "60";
            //staticTextAreaGoods.text = "";

            staticTextAreaGoods.zIndex = 1;
            strTextjson = JsonExtension.ToJSON(staticTextAreaGoods);//对象转为Json

            byTextInfo = System.Text.Encoding.UTF8.GetBytes(strTextjson);

            nRet = ListenDll.LV_AddArea(byScreenName, byProgramId, 0, byTextInfo);

            if (nRet != 0)
            {
                sResult = "添加静态区域失败" + GetErrInfo();
                return false;
            }

            //发送节目
            byte[] byIp = System.Text.Encoding.UTF8.GetBytes(IP);
            byte[] byProgramName = System.Text.Encoding.UTF8.GetBytes("ScanProgramName");

            byte[] byPassword = System.Text.Encoding.UTF8.GetBytes("");

            nRet = ListenDll.LV_SendProgramToDevice(byScreenName, byIp, byProgramId, byProgramName, byPassword);

            if (nRet != 0)
            {
                sResult = "发送节目失败" + GetErrInfo();
                return false;
            }
            //发送计划
            List<ProgramPlan> listPlan = new List<ProgramPlan>();

            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

            ProgramPlan plan1 = new ProgramPlan();
            plan1.createDate = Convert.ToInt64(ts.TotalMilliseconds);
            plan1.programId = "ScanProgram";
            plan1.programName = "ScanProgramName";
            plan1.programType = 1;
            plan1.updateDate = Convert.ToInt64(ts.TotalMilliseconds);
            plan1.date = new List<ProgramPlan.Date>();
            plan1.planWeekDay = new List<int>();
            plan1.time = new List<ProgramPlan.Time>();
            plan1.isDefault = 0;

            listPlan.Add(plan1);


            string strPlanJson = JsonExtension.ToJSON(listPlan);//对象转为Json
            byte[] byPlanInfo = System.Text.Encoding.UTF8.GetBytes(strPlanJson);
            string strDefVideo = "";//如果是默认节目,同时默认节目中存在视频文件,该字符串才不为空字符串
            byte[] byDefVideo = System.Text.Encoding.UTF8.GetBytes(strDefVideo);

            nRet = ListenDll.LV_SendProgramPlan(byIp, byPlanInfo, byDefVideo);

            if (nRet != 0)
            {
                sResult = "发送节目失败" + GetErrInfo();
                return false;
            }

            sResult=string.Empty;
            return true;
        }

        private static string GetErrInfo()
        {
            byte[] byErrInfo = new byte[1024];
            int nRet = ListenDll.LV_GetLastErrInfo(byErrInfo, 1024);
            if (nRet > 0)
            {
                return Encoding.UTF8.GetString(byErrInfo);
            }
            else
            {
                return "";
            }
        }
    }
}
