using System;
using System.Text;
using StuMgmLib.MyNameSpace;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
namespace StuMgmClient
{
    class SystemData
    {
        static Dictionary<short, CourseInfo> allCourseInfo;
        static Dictionary<short,CourseStatusEnum> allCourseStatus;
        static List<CourseInfo> courseInfo;
        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 = new List<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;
        }

        //internal static void StreeList() 
        //{
        //    allStreeDatas = new Dictionary<short,UserCourseDetail>();
           
        //    foreach (var item in allCourseStatus)
        //    {
        //       UserCourseDetail courseDetail = new UserCourseDetail();
        //       CourseInfo info = allCourseInfo[item.Key];//找到状态对应的课程表信息
        //       courseDetail.Id = info.Id;
        //       courseDetail.Name = info.Name;
        //       courseDetail.Status = (int)item.Value;
        //       courseDetail.Content = info.Content;
        //       allStreeDatas.Add(info.Pid,courseDetail);
        //    }
         
        //}


        //internal static UserCourseDetail r(CourseInfo info, ref Dictionary<short, UserCourseDetail> allCourseStatus)
        //{
        //    UserCourseDetail sel;
     
        //    if (allCourseStatus.TryGetValue(info.Id, out sel))
        //        return sel;
        
        //    UserCourseDetail m = new UserCourseDetail();
        //    //info->m                                                           

        //    allCourseStatus.Add(info.Id, m);
        //    if (info.Pid != 0)
        //    {
        //        UserCourseDetail father = r(allCourseInfo[info.Pid], ref allCourseStatus);
        //        father.Children.Add(m);
        //    }
        //    return m;
        //}

        //internal static void rr(UserCourseDetail u, TreeNode n)
        //{
        //    foreach(UserCourseDetail child in u.Children)
        //    {
        //        TreeNode node = new TreeNode(child);
        //        n.add(node);
        //        rr(child, node);
        //    }
        //}

        //internal static void aa (List<string> keys,List<string> values)
        //{
        //    Dictionary<short, UserCourseDetail> allCourseStatus=new Dictionary<short,UserCourseDetail>();
        //    for (short i = 0; i < keys.Count; i++)
        //        r(allCourseInfo[i], ref allCourseStatus);
        //    List<UserCourseDetail> lst = new List<UserCourseDetail>();
        //    //
        //    //TreeNode all;
        //    List<TreeNode> all;
        //    foreach(UserCourseDetail u in lst)
        //    {
        //       TreeNode n = new TreeNode();
        //       rr(u, n);
        //       all.add(n);
        //    }
        //}

        static DataTable ListToDb(List<CourseInfo> 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));
            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;
                dt.Rows.Add(dr);
            }
            return dt;
        }
        internal static DataTable StreeTable() 
        {
          return ListToDb(courseInfo);
        }
        internal static Dictionary<short, CourseStatusEnum> StreeCourseStatus() 
        {
            return allCourseStatus;
        }
    }
}
