using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.SDK;
using TEAMModelOS.SDK.Extension;
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.subjectId}";
                List<string> grade = newItem.gradeIds;
                UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
            }
            else if (newItem != null && odlItem != null)
            {
                //更新时 需要保证两个题的 id code 一致
                if (newItem.id == odlItem.id && newItem.code == odlItem.code)
                {
                    //先增加
                    string newKey = $"{newItem.subjectId}";
                    List<string> newGrade = newItem.gradeIds;
                    UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
                    //后变更删除
                    string oldKey = $"{odlItem.subjectId}";
                    List<string> oldGrade = odlItem.gradeIds;
                    UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
                }
            }
            else if (newItem == null && odlItem != null)
            {
                string oldKey = $"{odlItem.subjectId}";
                List<string> oldGrade = odlItem.gradeIds;
                UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0,null);
            }
            else
            {
                // throw new Exception();
            }
        }
        /// <summary>
        /// opt=false  减  true 增
        /// key  科目id
        /// 
        /// </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,ItemInfo newItem)
        {
           
            int count = 0;
            if (opt)
            {
                count = 1;
            }
            else
            {
                //未计入的则默认0
                count = -1;
            }
            foreach (var x in grade) {
                bool none = true;
                if (x == null)
                {

                }
                for (int index = 0; index < cond.grades.Count; index++)
                {

                    if (x == cond.grades[index].id)
                    {
                        cond.grades[index].count = cond.grades[index].count + count;
                        none = false;
                        break;
                    }
                }
                if (none)
                {
                    if (x == null) {
                        Console.WriteLine(newItem.ToJsonString());
                    }
                    cond.grades.Add(new GradeCount { id = x, count = count });
                }
            }
            SubjectCount subject = cond.subjects.Find(x => x.id == key);
            if (subject == null)
            {
                cond.subjects.Add(new SubjectCount() { id=key, count =0});
            }
            var list = cond.subjects.FindAll(x => x.id == key).ToList();
            foreach (var y in list) {
                if (!y.types.ContainsKey(type))
                {
                    var dict = new Dictionary<string, Dictionary<string, int>>
                    {
                        {
                            "level",new Dictionary<string, int>
                            {
                                { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }
                            }
                        },
                        {
                            "field",new Dictionary<string, int>
                            {
                                { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }, { "6", 0 }
                            }
                        }
                    };
                    dict["level"][$"{level}"] = count;
                    dict["field"][$"{field}"] = count;
                    y.types.Add(type, dict);
                }
                else
                {
                    y.types[type]["level"][$"{level}"] = y.types[type]["level"][$"{level}"] + count;
                  
                    y.types[type]["field"][$"{field}"] = y.types[type]["field"][$"{field}"] + count;
                }
                y.count = y.count + count;
            }
            cond.count = cond.subjects.Select(x => x.count).Sum();
        }
    }
}
