using SicUI.Models;
using System;
using System.Collections.Generic;
using Aitex.Core.Common.DeviceData;
using System.IO;
using System.Drawing;
using Aitex.Core.Util;
using VM.PlatformSDKCS;

namespace SicUI.Client.Models.Platform.TM
{
    public class Device2ViewModel : SicUIViewModelBase
    {
        [Subscription("Load.Rotation.IsServoBusy")]
        public bool LdRotationIsBusy { get; set; }

        [Subscription("Load.Rotation.IsServoOn")]
        public bool LdRotationIsServoOn { get; set; }

        [Subscription("Load.Rotation.IsMoveDone")]
        public bool LdRotationIsServoDone { get; set; }

        [Subscription("Load.Rotation.IsServoError")]
        public bool LdRotationIsServoError { get; set; }

        [Subscription("TM.LLTrayPresence.DeviceData")]
        public AITSensorData LoadTrayPresence { get; set; }

        [Subscription("TM.LoadTrayHomeSensor.DeviceData")]
        public AITSensorData LoadTrayHomeSensor { get; set; }

        [Subscription("TM.LLWaferPlaced.DeviceData")]
        public AITSensorData LoadWaferPlaced { get; set; }

        [Subscription("Load.Rotation.CurPos")]
        public double LdRotationCurPos { get; set; }

        [Subscription("Load.Rotation.CCD1Degree")]
        public double LdCCD1Degree { get; set; }

        [Subscription("Load.Rotation.CCD2Degree")]
        public double LdCCD2Degree { get; set; }

        [Subscription("Load.Rotation.CCD3Degree")]
        public double LdCCD3Degree { get; set; }

        [Subscription("LoadLock.IsOnline")]
        public bool LoadLockIsOnline { get; set; }

        [Subscription("LoadLock.Status")]
        public string LLStatus { get; set; }

        public bool LdRotationBtnEnable => !LdRotationIsBusy && !LdRotationIsServoError && !LoadLockIsOnline && LLStatus == "Idle";
        public Device2ViewModel()
        {

        }

        protected override void OnActivate()
        {
            base.OnActivate();
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
        }

        //protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
        //{
        //    base.InvokeAfterUpdateProperty(data);
        //    if (data.ContainsKey("LoadLock.HikVisionMaster.OutputImage"))
        //    {
        //        var bp = (Bitmap)data["LoadLock.HikVisionMaster.OutputImage"];
        //        OutputRanderImage = new(bp);
        //    }
        //}

        private byte[] _oldbyte;
        protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
        {
            base.InvokeBeforeUpdateProperty(data);
            var bp = (byte[])QueryDataClient.Instance.Service.GetData("LoadLock.HikVisionMaster.OutputImage");
            if (bp != null)
            {
                if (_oldbyte!=null&&!Array.Equals(_oldbyte,bp))
                {
                    Bitmap b = BytesToBitmap(bp);
                    OutputRanderImage?.Dispose();
                    OutputRanderImage = new ImageBaseData(b);
                }
            }
            _oldbyte = bp;
        }

        public Bitmap BytesToBitmap(byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                return new Bitmap(ms); // 或使用Image.FromStream:ml-citation{ref="1,18" data="citationList"}
            }
        }

        public void LdRotationStop()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.Stop");
        }

        public void LdRotationReset()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.ServoReset");
        }

        public void LdRotationServoOn()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.ServoOn");
        }

        public void LdRotationMoveOneCircle()
        {
            InvokeClient.Instance.Service.DoOperation($"LoadLock.MoveOneCircle");
        }

        public void LdRotationRelativeHome()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.MoveRelativeHome");
        }

        public void LdRotationRelativeHomeOffset()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.HomeOffset");
        }

        public void LdRotationMoveCCD1Pos()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.MoveCCD1Pos");
        }

        public void LdRotationMoveCCD2Pos()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.MoveCCD2Pos");
        }

        public void LdRotationMoveCCD3Pos()
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.MoveCCD3Pos");
        }

        public void LdRotationJogCW(float angle)
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.JogCW", angle);
        }

        public void LdRotationJogCCW(float angle)
        {
            InvokeClient.Instance.Service.DoOperation($"Load.Rotation.JogCCW", angle);
        }

        #region CCD
        [Subscription("LoadLock.HikGigeCamera.IsConnected")]
        public bool CamConnection { get; set; }

        [Subscription("LoadLock.HikGigeCamera.Message")]
        public string CamMessage { get; set; }

        public bool CTS { get; set; } = false;
        #endregion

        #region VM

        [Subscription("LoadLock.HikVisionMaster.SampleCircle")]
        public double[] SampleCircle { get; set; }

        [Subscription("LoadLock.HikVisionMaster.CurrentCircle")]
        public double[] CurrentCircle { get; set; }

        [Subscription("LoadLock.HikVisionMaster.Distance")]
        public double Distance { get; set; }

        [Subscription("LoadLock.HikVisionMaster.IsProcessing")]
        public string VMIsProcessing { get; set; }

        [Subscription("LoadLock.HikVisionMaster.LoadFile")]
        public string VMIsLoad { get; set; }

        [Subscription("LoadLock.HikVisionMaster.ModuleStatus")]
        public bool VMRun { get; set; }

        [Subscription("LoadLock.HikVisionMaster.Result")]
        public bool VMResult { get; set; }

        public ImageBaseData OutputRanderImage { get; set; }

        public void TestLocalFile(string value, bool isinch6)
        {
            InvokeClient.Instance.Service.DoOperation($"LoadLock.HikVisionMaster.ExecuteLocalFile", value + (isinch6 ? "FindLine" : ""));
        }

        public void TestCCD(string value,bool isinch6)
        {
            InvokeClient.Instance.Service.DoOperation($"LoadLock.HikVisionMaster.ExecuteGrab", value + (isinch6? "FindLine" : ""));
        }

        public void SetSample(string value)
        {
            InvokeClient.Instance.Service.DoOperation($"LoadLock.HikVisionMaster.SetSample", value);
        }

        public void ReloadSolution()
        {
            InvokeClient.Instance.Service.DoOperation($"LoadLock.HikVisionMaster.ReloadSolution");
        }
        #endregion

    }
}
