using HandyControl.Data;
using HKCardIN.Helper;
using HKCardIN.Logic;
using HKCardIN.Logic.Model;
using HKCardIN.Views;
using HKLog;
using Stylet;
using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using UHFHelper;

namespace HKCardIN.ViewModels
{
    public class RootViewModel : Conductor<IScreen>
    {
        bool IsLocker = false;

        public RootViewModel()
        {
            CodeVisible = Visibility.Collapsed;
            ContentVisible = Visibility.Visible;
        }

        #region 属性
        decimal _ShowMoney;
        public decimal ShowMoney
        {
            get => _ShowMoney;
            set => SetAndNotify(ref _ShowMoney, value);
        }
        Visibility _ContentVisible;
        public Visibility ContentVisible
        {
            get => _ContentVisible;
            set => SetAndNotify(ref _ContentVisible, value);
        }
        Visibility _CodeVisible;
        public Visibility CodeVisible
        {
            get => _CodeVisible;
            set => SetAndNotify(ref _CodeVisible, value);
        }
        string _InputMoney;
        public string InputMoney
        {
            get => _InputMoney;
            set
            {
                SetAndNotify(ref _InputMoney, value);
                decimal.TryParse(value, out decimal data);
                ShowMoney = data;
            }
        }
        string _CardNo;
        public string CardNo
        {
            get => _CardNo;
            set => SetAndNotify(ref _CardNo, value);
        }
        UserAndCardInfo _Info;
        public UserAndCardInfo Info
        {
            get => _Info;
            set => SetAndNotify(ref _Info, value);
        }
        #endregion

        #region 命令

        public void ReadAction()
        {
            if (!UHFCardHelper.GetInstance().ComOpen)
            {
                var res = UHFCardHelper.GetInstance().OpenPort();
                HKLogImport.WriteInfo(res.ResMes);
            }
            var RCardNo = UHFCardHelper.GetInstance().ReadCard();
            if (!Regex.IsMatch(RCardNo, "\\d{19}"))
            {
                var res = HandyControl.Controls.MessageBox.Show(new MessageBoxInfo
                {
                    Button = MessageBoxButton.YesNo,
                    IconKey = "InfoGeometry",
                    IconBrushKey = "InfoBrush",
                    YesContent = "是",
                    NoContent = "否",
                    Message = "此卡是新卡，前往制卡",
                    Caption = "提示！"
                });
                if (res == MessageBoxResult.Yes)
                {
                    CardView view = new CardView();
                    if (view.ShowDialog().Value) HandyControl.Controls.Growl.InfoGlobal("制卡成功");
                    else HandyControl.Controls.Growl.InfoGlobal("制卡失败");
                }
                Info = BaseLogic.GetInstance().PullUserAndCardInfo(CardNo);
                UHFCardHelper.GetInstance().ClosePort();
            }
            else
            {
                CardNo = RCardNo;
                Info = BaseLogic.GetInstance().PullUserAndCardInfo(CardNo);
                UHFCardHelper.GetInstance().ClosePort();
            }

        }
        public void InputAction(string input)
        {
            if (IsLocker) return;
            ShowMoney = decimal.Parse(input);
        }
        public void LockSreenAction()
        {
            IsLocker = true;
            ContentVisible = Visibility.Collapsed;
            CodeVisible = Visibility.Visible;
        }
        public void UnLockAction(HandyControl.Controls.PasswordBox input)
        {
            if (input.Password.Equals(DataBus.LockCode))
            {
                IsLocker = false;
                CodeVisible = Visibility.Collapsed;
                ContentVisible = Visibility.Visible;
            }
        }
        public void SaveAction()
        {
            if (!DataBus.NetWordState)
            {
                HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
                return;
            }

            if (BaseLogic.GetInstance().PushMoneyToServer(CardNo, ShowMoney))
            {
                HandyControl.Controls.Growl.SuccessGlobal($"【{CardNo}】充值成功，充值金额【{ShowMoney}】");
                Task.Run(() =>
                {
                    Thread.Sleep(5 * 1000);
                    Info = null;
                });
            }
            else
                HandyControl.Controls.Growl.InfoGlobal($"【{CardNo}】充值失败");
        }
        #endregion

        #region 方法
        protected override void OnViewLoaded()
        {
            MainThread();
        }
        private void MainThread()
        {
            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                try
                {
                    //1.检测网络上下线
                    bool network = HKHelper.GetInstance().GetNetworkState();
                    if (network != DataBus.NetWordState)
                    {
                        if (network) HandyControl.Controls.Growl.InfoGlobal("网络连接成功");
                        else HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
                        DataBus.NetWordState = network;
                    }
                    Thread.Sleep(3000);
                }
                catch (Exception ex)
                {
                    HandyControl.Controls.Growl.InfoGlobal(ex.Message);
                }
            }), "循环状态监测线程", false);
        }
        #endregion
    }
}
