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;

namespace SlnMesnac.WPF.ViewModel
{
    public class HandOverViewModel : INotifyPropertyChanged
    {
        private IBaseStaffService _baseStaffService;
        private ProdPlanInfoService _prodPlanInfoService;
        private ProdPlanDetailService _prodPlanDetailService;
        private IRecordStaffAttendanceService _recordStaffAttendanceService;
        private ProdPLanInfo planInfo;
        private ProdPlanDetail planDetail;
        private int times;

        #region
        /// <summary>
        /// 按钮文字转换事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        #endregion



        public HandOverViewModel()
        {
            _prodPlanDetailService = App.ServiceProvider.GetService<ProdPlanDetailService>();
            _prodPlanInfoService = App.ServiceProvider.GetService<ProdPlanInfoService>();
            _recordStaffAttendanceService = App.ServiceProvider.GetService<IRecordStaffAttendanceService>();
            planInfo = _prodPlanInfoService.GetRecordStaffAttendancesByConditions("", "", "", "", "1").FirstOrDefault();
            HandoverCommand = new RelayCommand(Handover);
            Init();
            _baseStaffService = App.ServiceProvider.GetService<IBaseStaffService>();

        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            if(planInfo != null)
            {
                //planDetail = _prodPlanDetailService.GetPlanDetailsByPlanCode(planInfo.PlanCode);
                //_recordStaffAttendanceService.GetLastestOffRecord();
                //PlanAmountText = planDetail.PlanAmount.ToString();
                //CompleteAmountText = planDetail.CompleteAmount.ToString();
                //hidUtils.Initial();
                //EmployeeLoginViewModel.hidUtils.StartScan();
                //EmployeeLoginViewModel.hidUtils.pushReceiveDataEvent += (bytes, str) =>
                //{
                //    if (MainWindowViewModel.wins == 3)
                //    {
                //        if (times < 2)//打卡超过2次无效
                //        {
                //            str = str.ToString().Replace(" ", "");
                //            BaseStaffInfo user = _baseStaffService.GetStaffInfoByCardId(str);
                //            if (user != null)
                //            {
                //                string staffType = user.StaffType;
                //                if (staffType == "1")//判断是否为班长
                //                {
                //                    if (times == 0 && planDetail.CurrentStaffId == user.StaffId)
                //                    {
                //                        //显示记录
                //                        StaffIdText = user.StaffId;
                //                        StaffNameText = user.StaffName;
                //                        StaffTypeText = user.StaffType;
                //                        TeamCodeText = user.TeamCode;
                //                        //数量+1
                //                        times++;
                //                    }
                //                    else if (times == 1)
                //                    {
                //                        StaffIdText = user.StaffId;
                //                        //判断是否为下一班组长
                //                        planDetail.CurrentStaffId = user.StaffId;
                //                        StaffNameText = user.StaffName;
                //                        StaffTypeText = user.StaffType;
                //                        TeamCodeText = user.TeamCode;
                //                        _prodPlanDetailService.Insert(planDetail);//更换班组长后插入一条明细记录
                //                                                                  //数量+1
                //                        times++;
                //                    }
                //                    else
                //                    {
                //                        HintText = "当前班组长先打卡！";
                //                    }
                //                }
                //                else
                //                {
                //                    HintText = "打卡人员非班长，打卡无效！";
                //                }
                //            }
                //            else
                //            {
                //                HintText = "没有匹配的员工，打卡失败！";
                //            }
                //        }
                //    }                 
                //};                               
            }         
        }

        #region 参数

        /// <summary>
        /// 计划数量Text
        /// </summary>
        private string planAmountText;
        public string PlanAmountText
        {
            get { return planAmountText; }
            set { planAmountText = value; OnPropertyChanged("PlanAmountText"); }
        }

        /// <summary>
        /// 完成数量Text
        /// </summary>
        private string completeAmountText;
        public string CompleteAmountText
        {
            get { return completeAmountText; }
            set { completeAmountText = value; OnPropertyChanged("CompleteAmountText"); }
        }

        /// <summary>
        /// 员工id
        /// </summary>
        private string staffIdText;
        public string StaffIdText
        {
            get { return staffIdText; }
            set { staffIdText = value; OnPropertyChanged("StaffIdText"); }
        }

        /// <summary>
        /// 员工名称
        /// </summary>
        private string staffNameText;
        public string StaffNameText
        {
            get { return staffNameText; }
            set { staffNameText = value; OnPropertyChanged("StaffNameText"); }
        }

        /// <summary>
        /// 员工类型
        /// </summary>
        private string staffTypeText;
        public string StaffTypeText
        {
            get { return staffTypeText; }
            set { staffTypeText = value; OnPropertyChanged("StaffTypeText"); }
        }

        /// <summary>
        /// 班组编号
        /// </summary>
        private string teamCodeText;
        public string TeamCodeText
        {
            get { return teamCodeText; }
            set { teamCodeText = value; OnPropertyChanged("TeamCodeText"); }
        }

        /// <summary>
        /// 提示信息
        /// </summary>
        private string hintText;
        public string HintText
        {
            get { return hintText; }
            set { hintText = value; OnPropertyChanged("HintText"); }
        }

        /// <summary>
        /// 信息
        /// </summary>
        private string messageText;
        public string MessageText
        {
            get { return messageText; }
            set { messageText = value; OnPropertyChanged("MessageText"); }
        }
        #endregion

        /// <summary>
        /// 确认交班
        /// </summary>
        public ICommand HandoverCommand { get; private set; }
        private void Handover()
        {
            //判断两个班长是否都已打卡
            if(times == 2)
            {
                //在工单明细插入一条，记录两个班长
                EmployeeLoginViewModel.hidUtils.StopScan();
                //关闭窗口
                MainWindowViewModel.wins = 1;
                Application.Current.Windows.OfType<HandOverWin>().First().Close();
            }
            else
            {
                HintText = "有人未打卡，请检查！";
            }
        }

        public void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
