using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DeviceRepair.Models.Plan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TsSFCDevice.Client.Biz.Impl;

namespace TsSFCDevice.Client.Launch
{
    public partial class XtraCalendarShow : DevExpress.XtraEditors.XtraForm
    {
        int PlanAutoID = 0;

        List<DailyPlanCompleteStatus> Datas;
        DateTime ServerDate = DateTime.MaxValue;

        public DateTime Value
        {
            get
            {
                return DateTime.Parse(calendarControl1.EditValue + "");
            }
        }

        public XtraCalendarShow() : this(0, DateTime.Now)
        {
            InitializeComponent();
        }

        public XtraCalendarShow(int m_PlanAutoID, DateTime dateTime)
        {
            InitializeComponent();
            PlanAutoID = m_PlanAutoID;
            calendarControl1.EditValue = dateTime;
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XtraCalendarShow_Load(object sender, EventArgs e)
        {
            try
            {
                if (PlanAutoID == 0)
                    throw new Exception($"传入的计划主键编号不能为0!");

                ServerDate = CommonRepository.Instance.ServiceTime();
                Datas = PlanRepository.Instance.AM_PLAN_Scheduler(PlanAutoID)?.ToList();

                this.calendarControl1.CellStyleProvider = new SFCStyleProvider() { Datas = Datas, ServerDate = ServerDate };
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
                this.DialogResult = DialogResult.Cancel;
            }
        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (DateTime.Parse(calendarControl1.EditValue + "") > ServerDate)
                {
                    throw new Exception($"不允许超前保养！");
                }

                if ((Datas.First(x => x.MaintenanceDay.Date == calendarControl1.DateTime.Date)?.IsComplete ?? 0) == 1)
                {
                    throw new Exception($"当日保养已全部完成,无法继续！");
                }

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }


        private void calendarControl1_ContextButtonCustomize(object sender, CalendarContextButtonCustomizeEventArgs e)
        {
            e.Item.AllowGlyphSkinning = DefaultBoolean.True;
            e.Item.ImageOptions.SvgImage = Properties.Resources.ok;
        }
    }

    public class SFCStyleProvider : ICalendarCellStyleProvider
    {
        public List<DailyPlanCompleteStatus> Datas;
        public DateTime ServerDate = DateTime.MaxValue;

        public void UpdateAppearance(CalendarCellStyle cell)
        {
            if (Datas != null)
            {
                DailyPlanCompleteStatus item = Datas.FirstOrDefault(x => x.MaintenanceDay == cell.Date);
                if (item != null)
                {
                    if (item.MaintenanceDay > ServerDate)
                    {
                        return;
                    }

                    if (item.IsComplete == 0)
                    {
                        if (cell.Date == ServerDate.Date)
                        {
                            return;
                        }
                        // 未完成
                        cell.Image = DevExpress.Images.ImageResourceCache.Default.GetImageById(DevExpress.Images.DXImages.Cancel, DevExpress.Utils.Design.ImageSize.Size16x16, DevExpress.Utils.Design.ImageType.Colored);
                    }
                    else if (item.IsComplete == 1)
                    {
                        // 已完成
                        cell.Image = DevExpress.Images.ImageResourceCache.Default.GetImageById(DevExpress.Images.DXImages.Apply, DevExpress.Utils.Design.ImageSize.Size16x16, DevExpress.Utils.Design.ImageType.Colored);
                    }
                    cell.ImageAlignment = DevExpress.Utils.Drawing.ImageAlignmentMode.TopRight;
                }

            }
        }
    }
}