using ConsoleApp;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using SlnMesnac.Business.business;

namespace SlnMesnac.WPF.ViewModel
{
    public class RemoveConfirmViewModel : INotifyPropertyChanged
    {       
        private IBaseStaffService _baseStaffService;
        private IRecordStaffAttendanceService _recordStaffAttendanceService;
        public static int times = 0;

        /// <summary>
        /// 按钮文字转换事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region 定义属性
        /// <summary>
        /// 提示信息
        /// </summary>
        private string hintText;
        public string HintText
        {
            get { return hintText; }
            set { hintText = value; OnPropertyChanged("HintText"); }
        }
        #endregion

        public RemoveConfirmViewModel()
        {
            HintText = "";
            times = 0;
            _baseStaffService = App.ServiceProvider.GetService<IBaseStaffService>();
            _recordStaffAttendanceService = App.ServiceProvider.GetService<IRecordStaffAttendanceService>();
            Init();
            ConfirmCommand = new RelayCommand(Confirm);            
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            //hidUtils.Initial();
            EmployeeLoginViewModel.hidUtils.StartScan();
            EmployeeLoginViewModel._transmitToRemoveConfigAction += (str) =>
            {
                if (MainWindowViewModel.wins == 2)
                {
                    str = str.ToString().Replace(" ", "");
                    BaseStaffInfo user = _baseStaffService.GetStaffInfoByCardId(str);
                    if (times < 1)
                    {
                        if (user != null)
                        {
                            string staffType = user.StaffType;
                            if (staffType == "1")//判断是否为班长,判断后续是否为当前班组的班长
                            {
                                HintText = "成功！";
                                times++;
                                RfidHandleBusniess.staffId = user.StaffId;
                            }
                            else
                            {
                                HintText = "非班组长，无效！";
                            }
                        }
                        else
                        {
                            HintText = "没有匹配的员工，打卡失败！";
                        }
                    }
                    else
                    {
                        HintText = "已成功，请勿重打！";
                        times = 1;
                    }
                }                
            };
        }

        /// <summary>
        /// 确认
        /// </summary>
        public ICommand ConfirmCommand { get; private set; }
        private void Confirm()
        {
            //在工单明细插入一条，记录两个班长
            EmployeeLoginViewModel.hidUtils.StopScan();
            //关闭窗口
            Application.Current.Windows.OfType<RemoveConfirmWin>().First().Close();
            MainWindowViewModel.wins = 1;
        }
    }
}
