using System.Collections.Generic;
using System.Linq;

namespace DeviceRepair.Models.Preserve.JsonData
{
    public class ContentData
    {
        /// <summary>
        /// 安装地点
        /// </summary>
        public List<SheetDataItem> InstallationSite { get; set; }

        /// <summary>
        /// 设备名称
        /// </summary>
        public List<SheetDataItem> EquipmentName { get; set; }

        /// <summary>
        /// 设备型号规格
        /// </summary>
        public List<SheetDataItem> DeviceSpecification { get; set; }

        /// <summary>
        /// 设备编号
        /// </summary>
        public List<SheetDataItem> EquipmentID { get; set; }

        /// <summary>
        /// 年
        /// </summary>
        public List<SheetDataItem> Year { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public List<SheetDataItem> Content { get; set; }

        /// <summary>
        /// 其他异常处理
        /// </summary>
        public List<SheetDataItem> ExceptionDescription { get; set; }

        /// <summary>
        /// 操作员
        /// </summary>
        public List<SheetDataItem> Operation { get; set; }

        /// <summary>
        /// 操作日期
        /// </summary>
        public List<SheetDataItem> OperationDate { get; set; }

        /// <summary>
        /// 年/月
        /// </summary>
        public List<SheetDataItem> YearAndMonth { get; set; }

        public void MergeData(ContentData News)
        {
            MergeExceptionDescription(News.ExceptionDescription);
            MergeOperationDate(News.OperationDate);
            MergeOperation(News.Operation);
            MergeContent(News.Content);
        }

        private void MergeExceptionDescription(List<SheetDataItem> News)
        {
            /* 传入的对象为空 */
            if (News == null && News.Count == 0)
                return;

            /* 原数据对象为空 */
            if (ExceptionDescription == null || ExceptionDescription.Count == 0)
            {
                ExceptionDescription = new List<SheetDataItem>();
                ExceptionDescription.AddRange(News);
            }
            else
            {
                // 当前存在数据
                foreach (SheetDataItem item in News)
                {
                    /* 旧数据 */
                    SheetDataItem oldValue = ExceptionDescription.FirstOrDefault(x => x.RowIndex == item.RowIndex
                   && x.ColumnIndex == item.ColumnIndex);
                    if (oldValue != null)
                    {
                        oldValue.Value += System.Environment.NewLine + item.Value;
                    }
                    else
                    {
                        OperationDate.Add(item);
                    }
                }
            }
        }

        private void MergeOperationDate(List<SheetDataItem> News)
        {
            /* 传入的对象为空 */
            if (News == null && News.Count == 0)
                return;

            /* 原数据对象为空 */
            if (OperationDate == null || OperationDate.Count == 0)
            {
                OperationDate = new List<SheetDataItem>();
                OperationDate.AddRange(News);
            }
            else
            {
                // 当前存在数据
                foreach (SheetDataItem item in News)
                {
                    /* 旧数据 */
                    SheetDataItem oldValue = OperationDate.FirstOrDefault(x => x.RowIndex == item.RowIndex
                   && x.ColumnIndex == item.ColumnIndex);
                    if (oldValue != null)
                    {
                        oldValue.Value = item.Value;
                    }
                    else
                    {
                        OperationDate.Add(item);
                    }
                }
            }
        }

        private void MergeOperation(List<SheetDataItem> News)
        {
            /* 传入的对象为空 */
            if (News == null && News.Count == 0)
                return;

            /* 原数据对象为空 */
            if (Operation == null || Operation.Count == 0)
            {
                Operation = new List<SheetDataItem>();
                Operation.AddRange(News);
            }
            else
            {
                // 当前存在数据
                foreach (SheetDataItem item in News)
                {
                    /* 旧数据 */
                    SheetDataItem oldValue = Operation.FirstOrDefault(x => x.RowIndex == item.RowIndex
                   && x.ColumnIndex == item.ColumnIndex);
                    if (oldValue != null)
                    {
                        oldValue.Value = item.Value;
                    }
                    else
                    {
                        Operation.Add(item);
                    }
                }
            }
        }

        private void MergeContent(List<SheetDataItem> News)
        {
            /* 传入的对象为空 */
            if (News == null && News.Count == 0)
                return;

            /* 原数据对象为空 */
            if (Content == null || Content.Count == 0)
            {
                Content = new List<SheetDataItem>();
                Content.AddRange(News);
            }
            else
            {
                // 当前存在数据
                foreach (SheetDataItem item in News)
                {
                    /* 旧数据 */
                    SheetDataItem oldValue = Content.FirstOrDefault(x => x.RowIndex == item.RowIndex
                   && x.ColumnIndex == item.ColumnIndex);
                    if (oldValue != null)
                    {
                        oldValue.Value = item.Value;
                    }
                    else
                    {
                        Content.Add(item);
                    }
                }
            }
        }
    }
}
