using System;
using System.Collections.Generic;
using System.Text;
using StuMgmClient;
using StuMgmLib.MyNameSpace;
using System.Diagnostics;
using System.Data;

namespace StuMgmClient
{
    class SystemCtrl
    {
        internal static ErrCode VerifLogin(short userName, string pawssword, out LvErr role, out int token)
        {
            role = LvErr.Error;
            token = 0;

            UserInfo cs = new UserInfo(userName, pawssword);
            ClientRequest req = new ClientRequest(ClientFunc.VerifLogin, cs);

            ServerResponse o = new ServerResponse(null);
            ErrCode err = SystemComm.GetData(req, out o);
            if (err != ErrCode.Success)
                return err;

            //if (!(o.Object is UserInfo))
            //    return ErrCode.ErrData;

            role = o.Lev;
            token = o.Token;
            return ErrCode.Success;
        }

        internal static ErrCode GetCourseInfo(out List<CourseInfo> courseInfo)
        {
            courseInfo = null;
            //******
            ClientRequest req = new ClientRequest(ClientFunc.GetCourseInfo, null);

            ServerResponse o = new ServerResponse(null);
            ErrCode err = SystemComm.GetData(req, out o);
            if (err != ErrCode.Success)
                return err;

            if (!(o.Object is List<CourseInfo>))
                return ErrCode.ErrData;

            courseInfo = (List<CourseInfo>)o.Object;
            return ErrCode.Success;
        }

        internal static ErrCode RefreshUserCourseInfo(short job_id, int token)
        {
            UserCourseInfo info;
            ErrCode err;

            err = GetUserCourseInfo(job_id, token, out info);
            if (err != ErrCode.Success)
                return err;

            err = SystemData.RefreshUserCourseInfo(info);
            if (err != ErrCode.Success)
                return err;

            return ErrCode.Success;
        }

        static ErrCode GetUserCourseInfo(short job_id, int token, out UserCourseInfo info)
        {
            info = null;
            Dictionary<short, int> dic = new Dictionary<short, int>();
            dic.Add(job_id, token);
            ClientRequest req = new ClientRequest(ClientFunc.GetUserCourseInfo, dic);

            ServerResponse o = new ServerResponse(null);
            ErrCode err = SystemComm.GetData(req, out o);
            if (err != ErrCode.Success)
                return err;

            if (!(o.Object is UserCourseInfo))
                return ErrCode.ErrData;


            //info = (UserCourseInfo)o;
            info = (UserCourseInfo)o.Object;
            return ErrCode.Success;
        }

        internal static DataTable GetStreeTable() 
        {
            return SystemData.StreeTable();
        }
    }
}
