using System;
using System.Collections.Generic;
using System.Text;
using StuMgmLib.MyNameSpace;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Collections;
using System.Reflection;
namespace StuMgmClient
{
    class SystemData
    {
        static Dictionary<short, CourseInfo> allCourseInfo;
        static Dictionary<short,CourseStatusEnum> allCourseStatus;
        static List<StreeDatas> allStreeDatas;
        internal static ErrCode InitSystemData()
        {
            ErrCode err;
            err = InitCourseInfo();
            if (err != ErrCode.Success)
                return err;
            return ErrCode.Success;
        }
        static ErrCode InitCourseInfo()
        {
            ErrCode err;
            List<CourseInfo> courseInfo;
            err = SystemCtrl.GetCourseInfo(out courseInfo);
            if (err != ErrCode.Success)
                return err;
            //将List<CourseInfo>转为Dictionary<short, CourseInfo>
           allCourseInfo = courseInfo.ToDictionary(key => key.Id, value => value);
            return ErrCode.Success;
        }

        internal static ErrCode RefreshUserCourseInfo(UserCourseInfo info)
        {
            //将UserCourseInfo转为Dictionary<short,CourseStatusEnum>
            allCourseStatus = new Dictionary<short, CourseStatusEnum>();
            int num = 0;
            short oldTem = 0;
            //切割字符串
            string[] sArray = info.Status.Split(new char[2] { ':', ';' });
            foreach (string i in sArray)
            {
                if (i.Equals("")) { break; }
                short tem = Convert.ToInt16(i);
                num++;
                if (num % 2 != 0)
                {
                    allCourseStatus.Add(tem, CourseStatusEnum.NoStart);
                    oldTem = tem;
                }
                else
                {
                    allCourseStatus[oldTem] = (CourseStatusEnum)tem;
                }
            }
            return ErrCode.Success;
        }

        static void StreeList() 
        {
            allStreeDatas = new List<StreeDatas>();
            StreeDatas sd=null;
            foreach (var item in allCourseStatus)
            {
               sd = new StreeDatas();
               CourseInfo infoTwo = allCourseInfo[item.Key];//找到状态对应的课程表信息
               sd.SecondNode = infoTwo.Pid;//二级节点9
               CourseInfo infoOne = allCourseInfo[infoTwo.Pid];
               CourseInfo infoThree = allCourseInfo[infoOne.Pid];
               sd.FirstNode = infoThree.Pid;
               sd.Id = infoTwo.Id;
               sd.Name = infoTwo.Name;
               sd.Pid = infoTwo.Pid;
               sd.Statu = (int)item.Value;
               sd.Content = infoTwo.Content;
               allStreeDatas.Add(sd);
            }
        }
        static DataTable ListToDb(List<StreeDatas> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Pid", typeof(int));
            dt.Columns.Add("Content", typeof(string));
            dt.Columns.Add("Statu", typeof(int));
            dt.Columns.Add("FirstNode", typeof(int));
            dt.Columns.Add("SecondNode", typeof(int));
            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = list[i].Id;
                dr[1] = list[i].Name;
                dr[2] = list[i].Pid;
                dr[3] = list[i].Content;
                dr[4] = list[i].Statu;
                dr[5] = list[i].FirstNode;
                dr[6] = list[i].SecondNode;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        internal static DataTable StreeTable() 
        {
          StreeList();
          return ListToDb(allStreeDatas);
        }
    }
}
