using DevExpress.Spreadsheet;
using DevExpress.Utils.Menu;
using DevExpress.XtraSpreadsheet.Menu;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using TsSFCDevice.Client.Biz.Base.Utils;

namespace TsSFCDevice.Client.Launch.PreserveTemplate
{
    [JsonObject(MemberSerialization.OptIn)]
    public class BaseTemplate
    {
        internal delegate void WriteValueEventHandler(EnumMaintenanceCellType CellType, string value, EnumMaintenanceOperationType operationType, bool IsException = false);
        internal virtual event WriteValueEventHandler OnWriteValue;

        internal Worksheet worksheet;

        /// <summary>
        /// 设备名称
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> EquipmentName { get; set; }

        /// <summary>
        /// 设备安装地点
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> InstallationSite { get; set; }

        /// <summary>
        /// 设备型号规格
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> DeviceSpecification { get; set; }

        /// <summary>
        /// 设备编号
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> EquipmentID { get; set; }

        /// <summary>
        /// 年
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> Year { get; set; }


        [JsonProperty]
        internal virtual List<SheetDataItem> YearAndMonth { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> Content { get; set; }

        /// <summary>
        /// 其他异常处理
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> ExceptionDescription { get; set; }

        /// <summary>
        /// 操作员
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> Operation { get; set; }

        /// <summary>
        /// 操作日期
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> OperationDate { get; set; }

        /// <summary>
        /// 最大行下标
        /// </summary>
        internal virtual int LastUsedRowIndex
        {
            get
            {
                return worksheet.Rows.LastUsedIndex;
            }
        }

        /// <summary>
        /// 最大列下标
        /// </summary>
        internal virtual int LastUsedColumnIndex
        {
            get
            {
                return worksheet.GetUsedRange().ColumnCount - 1;
            }
        }

        internal virtual string ExceptionFreezeString { get; }

        /// <summary>
        ///  签名
        /// </summary>
        internal SpreadsheetPopupMenu _popupOperation;
        internal virtual SpreadsheetPopupMenu popupOperation
        {
            get
            {
                if (_popupOperation == null)
                {
                    _popupOperation = new SpreadsheetPopupMenu()
                    {
                        Caption = "签名",
                        Items =
                        {
                             new DXMenuItem("签名",(s,e)=> { WriteValue(EnumMaintenanceCellType.Operation,Utility.SystemRuntimeInfo.CurrentUser.UserName); } ),
                             new DXMenuItem("清空",(s,e)=> { WriteValue(EnumMaintenanceCellType.Operation,"", EnumMaintenanceOperationType.Clear); } ),
                        }
                    };
                }
                return _popupOperation;
            }
        }

        /// <summary>
        /// 正文
        /// </summary>
        internal SpreadsheetPopupMenu _popupContent;
        internal virtual SpreadsheetPopupMenu popupContent
        {
            get
            {
                if (_popupContent == null)
                {
                    _popupContent = new SpreadsheetPopupMenu()
                    {
                        Caption = "正文",
                        Items =
                        {
                            new DXMenuItem("正常",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "√"); }),
                            new DXMenuItem("紧固",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "N"); }),
                            new DXMenuItem("给油",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "U"); }),
                            new DXMenuItem("清洁",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "C"); }),
                            new DXMenuItem("修理",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "R"); }),
                            new DXMenuItem("调整",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "T"); }),
                            new DXMenuItem("替换",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "H"); }),
                            new DXMenuItem("有问题/异常",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "△", EnumMaintenanceOperationType.Write,true);} ),
                            new DXMenuItem("不适用或无生产",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "N/A"); }),
                            new DXMenuItem("清空",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "", EnumMaintenanceOperationType.Clear); }),
                        }
                    };
                }
                return _popupContent;
            }
        }

        /// <summary>
        /// 操作时间
        /// </summary>
        internal SpreadsheetPopupMenu _popupDate;
        internal virtual SpreadsheetPopupMenu popupDate
        {
            get
            {
                if (_popupDate == null)
                {
                    _popupDate = new SpreadsheetPopupMenu()
                    {
                        Caption = "操作时间",
                        Items =
                        {
                              new DXMenuItem("时间",(a,aa)=> {
                                  WriteValue(EnumMaintenanceCellType.OperationDate, DateTime.Now.ToString("yyyy-MM-dd"));
                              }),
                              new DXMenuItem("清空",(a,aa)=>
                              {
                                  WriteValue(EnumMaintenanceCellType.OperationDate, "", EnumMaintenanceOperationType.Clear);
                              } ),
                        }
                    };
                }
                return _popupDate;
            }
        }

        /// <summary>
        /// 其他异常处理
        /// </summary>
        internal SpreadsheetPopupMenu _popupException;
        internal virtual SpreadsheetPopupMenu popupException
        {
            get
            {
                if (_popupException == null)
                {
                    _popupException = new SpreadsheetPopupMenu()
                    {
                        Caption = "其他异常处理",
                        Items =
                        {
                             new DXMenuItem("编辑",(a,aa)=>
                             {
                                 WriteValue(EnumMaintenanceCellType.ExceptionDescription, "", EnumMaintenanceOperationType.Write,true);
                             } ),
                             new DXMenuItem("清空", (a, aa) =>
                             {
                                 WriteValue(EnumMaintenanceCellType.ExceptionDescription, "", EnumMaintenanceOperationType.Clear,false);
                             }),
                        },

                    };
                }
                return _popupException;
            }
        }

        public BaseTemplate()
        {
            InstallationSite = new List<SheetDataItem>();
            EquipmentName = new List<SheetDataItem>();
            DeviceSpecification = new List<SheetDataItem>();
            EquipmentID = new List<SheetDataItem>();
            Year = new List<SheetDataItem>();
            Content = new List<SheetDataItem>();
            ExceptionDescription = new List<SheetDataItem>();
            Operation = new List<SheetDataItem>();
            OperationDate = new List<SheetDataItem>();
        }

        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="ws">Worksheet 控件对象</param>
        /// <param name="CurrentType">当前计划类型</param>
        /// <param name="EditColValue">当前编辑列</param>
        public BaseTemplate(Worksheet ws)
        {
            InstallationSite = new List<SheetDataItem>();
            EquipmentName = new List<SheetDataItem>();
            DeviceSpecification = new List<SheetDataItem>();
            EquipmentID = new List<SheetDataItem>();
            Year = new List<SheetDataItem>();
            Content = new List<SheetDataItem>();
            ExceptionDescription = new List<SheetDataItem>();
            Operation = new List<SheetDataItem>();
            OperationDate = new List<SheetDataItem>();
            YearAndMonth = new List<SheetDataItem>();
            worksheet = ws;
        }

        public virtual void WriteValue(EnumMaintenanceCellType CellType, string value, EnumMaintenanceOperationType operationType = EnumMaintenanceOperationType.Write, bool IsException = false)
        {
            OnWriteValue?.Invoke(CellType, value, operationType, IsException);
        }

        public virtual void ReplaceData(BaseTemplate Data)
        {
            foreach (SheetDataItem item in Data.EquipmentName)
            {
                SheetDataItem sdi = EquipmentName.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.InstallationSite)
            {
                SheetDataItem sdi = InstallationSite.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.DeviceSpecification)
            {
                SheetDataItem sdi = DeviceSpecification.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.EquipmentID)
            {
                SheetDataItem sdi = EquipmentID.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.Year)
            {
                SheetDataItem sdi = Year.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.YearAndMonth)
            {
                SheetDataItem sdi = YearAndMonth.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.Content)
            {
                SheetDataItem sdi = Content.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.ExceptionDescription)
            {
                SheetDataItem sdi = ExceptionDescription.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.Operation)
            {
                SheetDataItem sdi = Operation.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }

            foreach (SheetDataItem item in Data.OperationDate)
            {
                SheetDataItem sdi = OperationDate.FirstOrDefault(x => x.ColumnIndex == item.ColumnIndex && x.RowIndex == item.RowIndex);
                sdi.Value = item.Value;
            }
        }

        /// <summary>
        /// 表单验证
        /// </summary>
        /// <returns></returns>
        public virtual TemplateValidateResultModel FormValidate(Guid Sign, EnumMaintenanceType OperationType, string OperationTypeValue, EnumMaintenanceBanciType Banci = EnumMaintenanceBanciType.None)
        {
            return new TemplateValidateResultModel();
        }
    }


    [JsonObject(MemberSerialization.OptIn)]
    public class SubTemplateData
    {
        internal BaseTemplate Datas;
        public SubTemplateData(BaseTemplate m_Data)
        {
            Datas = m_Data;
        }

        /// <summary>
        /// 设备名称
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> EquipmentName { get { return Datas.EquipmentName; } }

        /// <summary>
        /// 设备安装地点
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> InstallationSite { get { return Datas.InstallationSite; } }

        /// <summary>
        /// 设备型号规格
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> DeviceSpecification
        {
            get
            {
                return Datas.DeviceSpecification.Where(x => x.Value != null && x.Sign != Guid.Empty).ToList();
            }
        }

        /// <summary>
        /// 设备编号
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> EquipmentID { get { return Datas.EquipmentID; } }

        /// <summary>
        /// 年
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> Year { get { return Datas.Year; } }


        [JsonProperty]
        internal virtual List<SheetDataItem> YearAndMonth { get { return Datas.YearAndMonth; } }

        /// <summary>
        /// 内容
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> Content
        {
            get
            {
                return Datas.Content.Where(x => x.Value != null && x.Sign != Guid.Empty).ToList();
            }
        }

        /// <summary>
        /// 其他异常处理
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> ExceptionDescription
        {
            get
            {
                return Datas.ExceptionDescription.Where(x => x.Value != null && x.Sign != Guid.Empty).ToList();
            }
        }

        /// <summary>
        /// 操作员
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> Operation
        {
            get
            {
                return Datas.Operation.Where(x => x.Value != null && x.Sign != Guid.Empty).ToList();
            }
        }
        /// <summary>
        /// 操作日期
        /// </summary>
        [JsonProperty]
        internal virtual List<SheetDataItem> OperationDate
        {
            get
            {
                return Datas.OperationDate.Where(x => x.Value != null && x.Sign != Guid.Empty).ToList();
            }
        }
    }
}
