using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Timer = System.Threading.Timer;

namespace HighWayIot.Winform.UserControlPages
{
    public partial class MonitorMainPage : UserControl
    {
        LogHelper logHelper = LogHelper.Instance;

        /// <summary>
        /// 班次服务类
        /// </summary>
        private SysShiftTimeService _shiftTimeService = SysShiftTimeService.Instance;

        /// <summary>
        /// 报表服务类
        /// </summary>
        private ZxDailyReportService _zxDailyReportService = ZxDailyReportService.Instance;

        /// <summary>
        /// datagridview 数据源
        /// 必须使用BindingList， 如果使用LIST，无法实现更改，添加、删除数据源自动更新datagridview
        /// </summary>
        private BindingList<MonitorDataSource> _monitorDataSources = new BindingList<MonitorDataSource>();

        public MonitorMainPage()
        {
            InitializeComponent();

            MonitorDataGridView.AutoGenerateColumns = false;

            DateTimeRefresh();

            MonitorDataGridView.DataSource = null;
            MonitorDataGridView.DataSource = _monitorDataSources;
        }

        /// <summary>
        /// TImer事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataRefresh_Tick(object sender, EventArgs e)
        {
            if(DateTime.Now.Second == 0)
            {
                DateTimeRefresh();
            }
            BindData();
        }

        /// <summary>
        /// 白夜班时间，现在时间控件刷新
        /// </summary>
        private void DateTimeRefresh()
        {
            var timeList = _shiftTimeService.GetShiftInfos();
            var morningShift = timeList.Where(x => x.ShiftName == "早").FirstOrDefault();
            var midShift = timeList.Where(x => x.ShiftName == "中").FirstOrDefault();
            var nightShift = timeList.Where(x => x.ShiftName == "夜").FirstOrDefault();

            if(morningShift == null || midShift == null || nightShift == null)
            {
                logHelper.Error("检查班次数据库是否早中夜班配置齐全！");
                return;
            }

            string dayString = morningShift.ShiftStartTime.Substring(0, 5) + "-" + midShift.ShiftEndTime.Substring(0, 5);
            string nightString = nightShift.ShiftStartTime.Substring(0, 5) + "-" + nightShift.ShiftEndTime.Substring(0, 5);
            DayTimeLabel.Text = dayString;
            NightTimeLabel.Text = nightString;

            NowDateProductNumLabel.Text = DateTime.Now.ToString("MM 月 dd 日 产量");
        }

        /// <summary>
        /// 数据刷新
        /// </summary>
        private void BindData()
        {
            List<ZxDailyReportEntity> dailyEntity = _zxDailyReportService.Get25DailyReportInfos();
            _monitorDataSources.Clear();
            for(int i = 0; i < dailyEntity.Count; i++)
            {
                _monitorDataSources.Add(new MonitorDataSource()
                {
                    No = i + 1,
                    VulcanizationNo = dailyEntity[i].VulcanizationNo,
                    StartTime = dailyEntity[i].StartTime.ToString("MM月dd日 HH:mm:ss"),
                    RecipeName = dailyEntity[i].RecipeName,
                    RecipeCode = dailyEntity[i].RecipeCode,
                    SpecCode = dailyEntity[i].SpecCode,
                    DeviceNo = dailyEntity[i].DeviceNo,
                    RawTireWeight = dailyEntity[i].RawTireWeight ?? 0,
                    BaseRubTimeSpan = GeneralUtils.DateTimeToString(dailyEntity[i].StartTime, dailyEntity[i].BaseEndTime),
                    MidRubTimeSpan = GeneralUtils.DateTimeToString(dailyEntity[i].StartTime, dailyEntity[i].MidEndTime),
                    FaceRubTimeSpan = GeneralUtils.DateTimeToString(dailyEntity[i].StartTime, dailyEntity[i].FaceEndTime),
                });
            }
            
        }
    }
}
