using System;

using TQLComm;

namespace XHWK.WKTool
{
    public class PenEvents : PenEvent
    {
        //int SA = 0;
        //int SB = 0;
        //int SC = 0;
        //int SD = 0;
        //int Sindex = 0;

        //int S = 0;
        //int O = 0;
        //int B = 0;
        //int P = 0;
        public static bool ConnectPen = false;
        public bool ShowPointLog = false;

        public static bool DrawIngend = true;

        public PenCommManager Manager;

        public int InToType = 0;
        /// <summary>
        /// TQL事件初始化，进入类型1录制2录屏
        /// </summary>
        /// <param name="type">进入类型1录制2录屏</param>
        public PenEvents(int type)//(Form1 form)
        {
            InToType = type;
            //this.form = form;
            Manager = new PenCommManager(this);
            Manager.Init();

            ////获取离线数据量
            //Manager.GetOfflineDataNo();
            ////开始下载离线数据
            //Manager.StartDownloadOfflineData();
            ////继续
            //Manager.ContinueDownloadOfflineData();
            ////暂停下载
            //Manager.PauseDownloadOfflineData();
            //删除离线数据
            Manager.RemoveOfflineData();
        }


        public void StartRecvDataTimer1()
        {
            DrawIngend = false;
            System.Timers.Timer t = new System.Timers.Timer(1000);
            t.Elapsed += new System.Timers.ElapsedEventHandler(timerCall);
            t.AutoReset = false;//设置是执行一次（false）还是一直执行(true)；
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
        }

        //委托执行的方法
        private void timerCall(object source, System.Timers.ElapsedEventArgs e)
        {
            DrawIngend = true;
            //MessageBox.Show("ni");
        }

        public void OnGetOIDType(uint oType)
        {

        }

        public void OnSetOIDType(bool bSuccess)
        {

        }

        public void onConfirmReceiveOfflineData(bool bSuccess)
        {
        }

        /// <summary>
        /// 智能笔连接状态
        /// </summary>
        /// <param name="bConnected"></param>
        public void onConnectionStatus(bool bConnected)
        {
            ConnectPen = bConnected;
            try
            {
                if (bConnected)
                {
                    App.TQLPenStatus = true;
                    //form.AddLog("智能笔已连接！");
                    if (App.W_XHMicroLessonSystemWindow != null)
                        App.W_XHMicroLessonSystemWindow.UpdateDevStatus();
                }
                else
                {
                    App.TQLPenStatus = false;
                    //form.DisConnect();
                    //form.AddLog("智能笔已断开连接！");
                    if (App.W_XHMicroLessonSystemWindow != null)
                        App.W_XHMicroLessonSystemWindow.UpdateDevStatus();
                }
            }
            catch (Exception)
            {
            }
        }

        public void onContinueOfflineData(bool bSuccess)
        {

        }
        /// <summary>
        /// 下载离线数据
        /// </summary>
        /// <param name="bEnd"></param>
        public void onEndOfDownloadOfflineData(bool bEnd)
        {
            if (bEnd)
            {
                ////继续
                //Manager.ContinueDownloadOfflineData();
                ////暂停下载
                //Manager.PauseDownloadOfflineData();
                //下载离线数据成功！
                //删除离线数据
                Manager.RemoveOfflineData();
            }
            else
            {
                //下载离线数据失败！
                //删除离线数据
                Manager.RemoveOfflineData();
            }
        }

        public void onGetAutoPowerOffTime(short Data)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetoffTextString(Data.ToString());
            //form.AddLog("当前自动休眠时间:" + Data.ToString() + "!");
        }

        public void onGetAutoPowerOnMode(bool bSetOn)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetSevseepComBox(bSetOn);
            //form.AddLog("当前唤醒模式笔尖压力是否唤醒:" + bSetOn.ToString() + "!");
        }

        public void onGetBatteryPower(short Data)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetBatteryTextString(Data.ToString() + "%");
            //form.AddLog("当前剩余电量:" + Data.ToString() + "%!");
        }

        public void onGetBeepStatus(bool bSetOn)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetBeetComBox(bSetOn);
            //form.AddLog("当前蜂鸣器模式是否响动:" + bSetOn.ToString() + "!");
        }

        public void onGetCustomerID(uint ID1, uint ID2)
        {

        }

        public void onGetElementCode(uint OID, uint SID, int Size)
        {

        }

        public void onGetFWVersion(string strData)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetBTTextString(strData);
            //form.AddLog("当前蓝牙版本号:" + strData + "!");
        }

        public void onGetMCUFWVersion(string strData)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetMCUTextString(strData);
            //form.AddLog("当前MCU版本号:" + strData + "!");
        }

        public void onGetMacAddress(string macAddress)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetMAcTextString(macAddress);
            //form.AddLog("当前MAC:" + macAddress + "!");
        }

        public void onGetOID4ElementCode(int SA, int SB, int SC, int SD, int index)
        {
            //if (this.SA != SA || this.SB != SB || this.SC != SC || this.SD != SD || this.Sindex != index)
            //{
            //    this.SA = SA; this.SB = SB; this.SC = SC; this.SD = SD; this.Sindex = index;
            //    form.AddLog("SA:" + SA.ToString() + "," + "SB:" + SB.ToString() + "," + "SC:" + SC.ToString() + "," + "SD:" + SD.ToString() + "," + "index:" + index.ToString());
            //}
        }

        public void onGetOfflineDataNo(int Data)
        {
            ConnectPen = true;

            if (Data > 0)
            {
                //开始下载离线数据
                Manager.StartDownloadOfflineData();
            }
            //form.AddLog("当前离线数据量:" + Data.ToString() + "!");
            //if (Data == 0 && form.StartDownload == true)
            //{
            //    form.StartDownload = false;
            //    form.StartOrStop(form.StartDownload);
            //    form.AddLog("下载离线数据成功！");
            //}

        }

        public void onGetPenColor(short Data)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetLEDComBox(Data);
            //switch (Data)
            //{
            //    case 1:
            //        form.timing.SetPenColor(Color.Blue);
            //        break;
            //    case 2:
            //        form.timing.SetPenColor(Color.Green);
            //        break;
            //    case 3:
            //        form.timing.SetPenColor(Color.Cyan);
            //        break;
            //    case 4:
            //        form.timing.SetPenColor(Color.Red);
            //        break;
            //    case 5:
            //        form.timing.SetPenColor(Color.Magenta);
            //        break;
            //    case 6:
            //        form.timing.SetPenColor(Color.Yellow);
            //        break;
            //    case 7:
            //        form.timing.SetPenColor(Color.Black);
            //        break;
            //    default:
            //        break;
            //}

        }
        /// <summary>
        /// 获取笔名
        /// </summary>
        /// <param name="localName"></param>
        public void onGetPenLocalName(string localName)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetNameTextString(localName);
            //form.AddLog("当前笔名:" + localName + "!");
        }

        public void onGetPressureCalibrationData(uint LData, uint HData)
        {

        }

        private DateTime ConvertLongToDateTime(long d)
        {
            DateTime dtStart = Convert.ToDateTime("1970-1-1");
            long lTime = long.Parse(d.ToString());
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.AddSeconds(lTime);
            return dtResult;
            // return dtResult;
        }

        public void onGetRTC(long Data)
        {
            //DateTime time = ConvertLongToDateTime(Data);
            //if (form.PenAttribute != null) form.PenAttribute.SetRTCTextString(time.ToString("yyyy-MM-dd HH:mm:ss"));
            //form.AddLog("当前RTC:" + time.ToString("yyyy-MM-dd HH:mm:ss") + "!");
        }

        public void onGetSensitivityLevel(short Data)
        {
            //ConnectPen = true;
            //if (form.PenAttribute != null) form.PenAttribute.SetSevComBox(Data);
            //form.AddLog("当前压力敏感度:" + Data.ToString() + "!");
        }
        /// <summary>
        /// 内存使用量
        /// </summary>
        /// <param name="Data"></param>
        public void onGetUsedMemory(short Data)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetmemTextString(Data.ToString());
            //form.AddLog("当前使用内存为:" + Data.ToString() + "!");
        }
        /// <summary>
        /// 获取蓝牙适配器连接状态
        /// </summary>
        /// <param name="bReady"></param>
        public void onHIDGetReady(bool bReady)
        {
            //if (bReady)
            //{
            //    form.AddLog("蓝牙适配器已连接！");
            //}
            //else
            //{
            //    form.AddLog("蓝牙适配器已断开连接！");
            //}
        }

        public void onPauseOfflineData(bool bSuccess)
        {

        }

        /// <summary>
        /// 书写点
        /// </summary>
        /// <param name="dot"></param>
        /// <param name="count"></param>
        public void onReceiveDot(Dot dot, int count)
        {
            if (InToType == 1)
            {
                //录制模式
                if (dot.type == DotType.PEN_HOVER)
                {
                    return;
                }
                else if (dot.type == DotType.PEN_DOWN)
                {
                    App.W_XHMicroLessonSystemWindow.TqlDown();
                }
                else if (dot.type == DotType.PEN_UP)
                {
                    App.W_XHMicroLessonSystemWindow.TqlUp();
                }
                else if (dot.type == DotType.PEN_MOVE)
                {
                    double CX = (double)dot.x + (double)dot.fx / 100.00;
                    double CY = (double)dot.y + (double)dot.fy / 100.00;
                    App.W_XHMicroLessonSystemWindow.TqlPenWrite
                    (
                        CX,
                        CY,
                        dot.force
                    );
                    //form.AddLog("x:" + (dot.x + dot.fx / 100f).ToString() + ", " + "y:" + (dot.y + dot.fy / 100f).ToString());
                }
            }
            else if (InToType == 2)
            {
                //录屏模式
                if (dot.type == DotType.PEN_HOVER)
                {
                    return;
                }
                else if (dot.type == DotType.PEN_DOWN)
                {
                    App.W_PracticeWindow.TqlDown();
                }
                else if (dot.type == DotType.PEN_UP)
                {
                    App.W_PracticeWindow.TqlUp();
                }
                else if (dot.type == DotType.PEN_MOVE)
                {
                    double CX = (double)dot.x + (double)dot.fx / 100.00;
                    double CY = (double)dot.y + (double)dot.fy / 100.00;
                    App.W_PracticeWindow.TqlPenWrite
                    (
                        CX,
                        CY,
                        dot.force
                    );
                    //form.AddLog("x:" + (dot.x + dot.fx / 100f).ToString() + ", " + "y:" + (dot.y + dot.fy / 100f).ToString());
                }
            }
            ////可用于分页使用
            //if (this.S != dot.sectionId || this.O != dot.ownerId || this.B != dot.noteId || this.P != dot.pageId)
            //{
            //    this.S = dot.sectionId; this.O = dot.ownerId; this.B = dot.noteId; this.P = dot.pageId;
            //    //form.AddLog("Dot_S:" + this.S.ToString() + ", " + "Dot_O:" + this.O.ToString() + ", " + "Dot_B:" + this.B.ToString() + ", " + "Dot_P:" + this.P.ToString());
            //}

            //if (dot.type == DotType.PEN_HOVER) return;
            //if (this.S != dot.sectionId || this.O != dot.ownerId || this.B != dot.noteId || this.P != dot.pageId)
            //{
            //    this.S = dot.sectionId; this.O = dot.ownerId; this.B = dot.noteId; this.P = dot.pageId;
            //    //form.AddLog("Dot_S:" + this.S.ToString() + ", " + "Dot_O:" + this.O.ToString() + ", " + "Dot_B:" + this.B.ToString() + ", " + "Dot_P:" + this.P.ToString());
            //}
            //if (ShowPointLog == true)
            //{
            //    //form.AddLog("x:" + (dot.x + dot.fx / 100f).ToString() + ", " + "y:" + (dot.y + dot.fy / 100f).ToString());
            //}
            ///*  if (dot.type == DotType.PEN_DOWN || dot.type == DotType.PEN_MOVE || dot.type == DotType.PEN_UP)
            //  {
            //      form.AddLog("Dot_type:" + dot.type.ToString() + ", " + "Dot_force:" + dot.force.ToString());
            //  } */

            ////if (DrawingBox.DrawType != 2) DrawingBox.DrawType = 1;
            ////form.Drawing(dot);
            //if (DrawIngend == true)
            //{
            //    StartRecvDataTimer1();
            //}
            ///* long unixTimeStamp = (long)(dot.timeLong - 28800000);//1478162177;
            // System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(2010, 1, 1)); // 当地时区
            // DateTime dt = startTime.AddMilliseconds(unixTimeStamp);
            // form.AddLog("ON:" + dt.ToString("MM/dd HH:mm:ss.fff"));*/
        }
        /// <summary>
        /// 接收脱机数据
        /// </summary>
        /// <param name="dot"></param>
        /// <param name="count"></param>
        public void onReceiveOfflineData(Dot dot, int count)
        {
            //if (form.StartDownload == false)
            //{
            //    Manager.StopDownloadOfflineData();
            //    return;
            //}
            //if (this.S != dot.sectionId || this.O != dot.ownerId || this.B != dot.noteId || this.P != dot.pageId)
            //{
            //    this.S = dot.sectionId; this.O = dot.ownerId; this.B = dot.noteId; this.P = dot.pageId;
            //    form.AddLog("Dot_S:" + this.S.ToString() + ", " + "Dot_O:" + this.O.ToString() + ", " + "Dot_B:" + this.B.ToString() + ", " + "Dot_P:" + this.P.ToString());
            //}

            ///*long unixTimeStamp = (long)(dot.timeLong - 28800000);//1478162177;
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(2010, 1, 1)); // 当地时区
            //DateTime dt = startTime.AddMilliseconds(unixTimeStamp);
            //form.AddLog("OFF:" + dt.ToString("MM/dd HH:mm:ss.fff"));
            //*/

            ///* DateTime tmp = new System.DateTime(2010, 1, 1);
            // tmp.AddMilliseconds(dot.timeLong);
            // form.AddLog("TIME:" + tmp.ToString("MM/dd HH:mm:ss"));*/

            //// form.AddLog("TIME:" + dot.timeLong.ToString());

            //if (DrawingBox.DrawType != 2) DrawingBox.DrawType = 3;
            //form.Drawing(dot);
        }

        public void onReceiveOriginalDot(Dot dot, int count)
        {
            //form.Drawing(dot);
        }

        public void onRemoveOfflineData(bool bRemoved)
        {
            //if (bRemoved)
            //{

            //    form.AddLog("删除离线数据成功！");
            //    form.StartDownload = false;
            //    form.ContinueDownload = true;
            //    form.DrawOffType = -1;
            //}
            //else form.AddLog("删除离线数据失败！");
        }

        public void onSetAutoPowerOffTime(bool bsuccess)
        {

            //if (bsuccess) form.AddLog("设置自动休眠时间成功！");
            //else form.AddLog("设置自动休眠时间失败！");
        }

        public void onSetAutoPowerOnMode(bool bsuccess)
        {
            //if (bsuccess) form.AddLog("设置笔的唤醒模式成功！");
            //else form.AddLog("设置笔的唤醒模式失败！");
        }

        public void onSetBeepStatus(bool bsuccess)
        {
            //if (bsuccess) form.AddLog("设置蜂鸣器成功！");
            //else form.AddLog("设置蜂鸣器失败！");
        }

        public void onSetCustomerID(bool bsuccess)
        {

        }

        public void onSetElementCode(bool bSuccess)
        {

        }

        public void onSetPenColor(bool bsuccess)
        {
            //if (Form1.SetColor == false) return;
            //Form1.SetColor = false;
            //if (bsuccess) form.AddLog("设置笔LED灯颜色成功！");
            //else form.AddLog("设置笔LED灯颜色失败！");
        }

        public void onSetPenLocalName(bool bsuccess)
        {
            //if (bsuccess) form.AddLog("设置笔名成功！");
            //else form.AddLog("设置笔名失败！");
        }

        public void onSetSensitivityLevel(bool bsuccess)
        {
            //if (bsuccess) form.AddLog("设置压力敏感度成功！");
            //else form.AddLog("设置压力敏感度失败！");
        }

        public void onStartDownloadOfflineData(bool bStart)
        {

        }

        public void onStopDownloadOfflineData(bool bStart)
        {

        }

        public void onWriteRTC(bool bsuccess)
        {
            //if (bsuccess)
            //{
            //    form.AddLog("设置RTC成功！");
            //    Manager.GetRTC();
            //}
            //else form.AddLog("设置RTC失败！");
        }


        public void DownloadOfflineDataRate(float Rate)
        {
            //form.SetProGress(Rate);
        }

        public void onGetMCUFCode(string strData)
        {
            //if (form.PenAttribute != null) form.PenAttribute.SetMcuCodeTextString(strData);
            //form.AddLog("MCU序列号:" + strData + "!");
        }

        public void OnGetPenCodeType(uint oType)
        {

        }

        public void onSetPenHover(byte type)
        {
            //if (type == 1)
            //{
            //    form.AddLog("设置悬浮打开！");
            //    form.PenAttribute.SetHoverComBox(0);
            //}
            //else if (type == 0)
            //{
            //    form.AddLog("设置悬浮关闭！");
            //    form.PenAttribute.SetHoverComBox(1);
            //}
        }
    }
}
