using Performance.EntityModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Performance.DtoModels
{
    public class HandsonTable
    {
        private IEnumerable<collect_permission> _permissions;
        private List<Dictionary<string, string>> _data;
        public HandsonTable(int sheetType, string[] cols, List<collect_permission> permissions)
        {
            _permissions = permissions;
            _data = new List<Dictionary<string, string>>();


            InitColHeaders(sheetType, cols);
            InitColumns(permissions);
        }

        public string[] ColHeaders { get; private set; }
        public List<Dictionary<string, string>> Data => _data;
        public HandsonColumn[] Columns { get; private set; }

        public void SetRowData(IEnumerable<HandsonRowData> datas)
        {
            foreach (var dt in datas)
            {
                var dic = CreateDataRow("编号", dt.Row.ToString());
                foreach (var item in dt.CellData)
                {
                    if (dic.ContainsKey(item.Name) && _permissions.Any(w => w.HeadName == item.Name && w.AttachLast > 0))
                        dic[item.Name] = item.Value?.ToString();
                }
                _data.Add(dic);
            }
        }

        private void InitColHeaders(int sheetType, string[] cols)
        {
            var defaluts = new DefalutHandsonHeader[]
              {
                new DefalutHandsonHeader{ SheetType = SheetType.OtherIncome, Necessity = new[] { "核算单元（医技组）","核算单元（医生组）","核算单元（护理组）","科室名称" } },
                new DefalutHandsonHeader{ SheetType = SheetType.Expend, Necessity = new[] { "核算单元（医技组）","核算单元（医生组）","核算单元（护理组）","科室名称" } },
                new DefalutHandsonHeader{ SheetType = SheetType.Workload, Necessity = new[] { "核算单元","科室名称" } },
                //new DefalutHandsonHeader{ SheetType = SheetType.AccountExtra, Necessity = new[] { "核算单元","科室名称" } },
                //new DefalutHandsonHeader{ SheetType = SheetType.PersonExtra, Necessity = new[] { "核算单元","科室名称" } },
              };
            var necessitys = defaluts.FirstOrDefault(w => sheetType == (int)w.SheetType)?.Necessity.ToList();
            necessitys = necessitys ?? new List<string>();

            foreach (var item in cols)
            {
                if (!necessitys.Contains(item))
                    necessitys.Add(item);
                if (!_permissions.Any(w => w.HeadName == item && w.Visible == 1))
                    necessitys.Remove(item);
            }
            ColHeaders = necessitys.ToArray();
        }

        private void InitColumns(List<collect_permission> permissions)
        {
            List<HandsonColumn> columns = new List<HandsonColumn>();
            foreach (var item in ColHeaders)
            {
                var readnoly = _permissions.FirstOrDefault(f => f.HeadName == item)?.Readnoly == 1;
                columns.Add(new HandsonColumn(item, readnoly));
            }

            Columns = columns.ToArray();
        }

        private Dictionary<string, string> CreateDataRow(string key, string value)
        {
            var temp = new Dictionary<string, string>() { { key, value } };
            foreach (var item in ColHeaders)
            {
                if (!temp.ContainsKey(item))
                    temp.Add(item, "");
            }
            return temp;
        }
    }

    public class HandsonColumn
    {
        public HandsonColumn(string data, bool readOnly = false, DataFormat format = DataFormat.普通格式)
        {
            Data = data;
            ReadOnly = readOnly;

            switch (format)
            {
                case DataFormat.普通格式:
                    Type = "text";
                    break;
                case DataFormat.小数:
                    Type = "numeric";
                    NumericFormat = new NumericFormat { Pattern = "0,00.00" };
                    break;
                case DataFormat.百分比:
                    Type = "numeric";
                    NumericFormat = new NumericFormat { Pattern = "0,00.00%" };
                    break;

            }
        }
        public string Data { get; set; }
        public bool ReadOnly { get; set; }
        public string Type { get; set; }
        public NumericFormat NumericFormat { get; set; }
    }

    public class NumericFormat
    {
        public string Pattern { get; set; }
    }

    public class DefalutHandsonHeader
    {
        public SheetType SheetType { get; set; }
        public string[] Necessity { get; set; }
    }

    public class HandsonRowData
    {
        public HandsonRowData(int row, IEnumerable<HandsonCellData> cellData)
        {
            Row = row;
            CellData = cellData;
        }

        public int Row { get; set; }
        public IEnumerable<HandsonCellData> CellData { get; set; }
    }

    public class HandsonCellData
    {
        public HandsonCellData(string name, object value)
        {
            Name = name;
            Value = value;
        }

        public string Name { get; set; }
        public object Value { get; set; }
    }
}
