using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.SDK.Models;

namespace TEAMModelOS.Services.Common
{
    public static class ItemService
    {
        /// <summary>
        /// 计算题目的条件数量变化
        /// </summary>
        /// <param name="newItem"></param>
        /// <param name="odlItem"></param>
        /// <param name="cond"></param>
        public static void CountItemCond(ItemInfo newItem, ItemInfo odlItem, ItemCond cond) {
            //检查两个对象是否是同一条记录
            if (newItem != null && odlItem == null)
            {
                string newKey = $"{newItem.periodId}-{newItem.subjectId}";
                List<string> grade = newItem.gradeIds;
                UpdateItemCond(cond, true,newKey, grade, newItem.type, newItem.level, newItem.field.HasValue?newItem.field.Value:0);
            }
            else if (newItem != null && odlItem != null)
            {
                //更新时 需要保证两个题的 id code 一致
                if (newItem.id == odlItem.id && newItem.code == odlItem.code)
                {
                    //先增加
                    string newKey = $"{newItem.periodId}-{newItem.subjectId}";
                    List<string> newGrade = newItem.gradeIds;
                    UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0);
                    //后变更删除
                    string oldKey = $"{odlItem.periodId}-{odlItem.subjectId}";
                    List<string> oldGrade = odlItem.gradeIds;
                    UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0);
                }
            }
            else if (newItem == null && odlItem != null)
            {
                string oldKey = $"{odlItem.periodId}-{odlItem.subjectId}";
                List<string> oldGrade = odlItem.gradeIds;
                UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0);
            }
            else {
               // throw new Exception();
            }
        }
        /// <summary>
        /// opt=false  减  true 增
        /// </summary>
        /// <param name="cond"></param>
        /// <param name="opt"></param>
        public static void UpdateItemCond(ItemCond cond, bool opt,string key,List<string> grade,string type,int level,int field)
        {
            int count = 0;
            if (opt)
            {
                count = 1;
            }
            else
            {
                //未计入的则默认0
                count = -1;
            }
            if (cond.conds.ContainsKey(key))
            {
                foreach (var x in grade)
                {
                    var exCondCount= cond.conds[key].Where(x => x.grade.Key.Equals(x)).FirstOrDefault();
                    if (exCondCount != null)
                    {
                        exCondCount.grade= new KeyValuePair<string, int> (x, exCondCount.grade.Value + count);
                        if (exCondCount.type.ContainsKey(type))
                        {
                            exCondCount.type[type] = exCondCount.type[type] + count;
                        }
                        else {
                            exCondCount.type.Add(type, count);
                        }
                        if (exCondCount.level.ContainsKey(level))
                        {
                            exCondCount.level[level] = exCondCount.level[level] + count;
                        }
                        else
                        {
                            exCondCount.level.Add(level, count);
                        }
                        if (exCondCount.field.ContainsKey(field))
                        {
                            exCondCount.field[field] = exCondCount.field[field] + count;
                        }
                        else
                        {
                            exCondCount.field.Add(field, count);
                        }
                    }
                    else {
                        CondCount condCount = new CondCount
                        {
                            grade = new KeyValuePair<string, int>(x, count),
                            type = new Dictionary<string, int> { { type, count } },
                            field = new Dictionary<int, int> { { field, count } },
                            level = new Dictionary<int, int> { { level, count } }
                        };
                        cond.conds[key].Add(condCount);
                    }
                }
            }
            else {
                List<CondCount> conds = new List<CondCount>();
                foreach (var x in grade) {
                    CondCount condCount = new CondCount {
                        grade = new KeyValuePair<string, int>(x, count),
                        type = new Dictionary<string, int> { { type, count } },
                        field = new Dictionary<int, int> { { field, count } },
                        level= new Dictionary<int, int> { { level, count } }
                    };
                    conds.Add(condCount);
                }
                cond.conds.Add(key, conds);
            }
        }

    }
}
