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 Lvl role, out int token)
        {
            LoginResponse lr = new LoginResponse();
            role = Lvl.Error;
            token = 0;

            UserInfoLogin cs = new UserInfoLogin(userName, pawssword);
            ClientRequest req = new ClientRequest(ClientFunc.VerifyLogin, cs);

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

            if (!(o.Object is LoginResponse))
                return ErrCode.ErrData;
            if (!o.Final) 
            {
                 Debug.Print(o.ErrMessage);
                 return ErrCode.ErrData;
            }
            lr=(LoginResponse)o.Object;
            role = lr.Level;
            token = lr.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;

            if (!o.Final)
            {
                Debug.Print(o.ErrMessage);
                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;
        }

        internal static ErrCode SendUserCourseInfoOper(UserCourseInfoOper uo)
        {
            //******
            ClientRequest req = new ClientRequest(ClientFunc.SUpdateCourse, uo);

            ServerResponse o = new ServerResponse(null);
            ErrCode err = SystemComm.sendData(req, out o);
            if (err != ErrCode.Success)
                return err;
            //if (!(o.Object is List<CourseInfo>))
            //    return ErrCode.ErrData;
            if (!o.Final)
            {
                Debug.Print(o.ErrMessage);
                return ErrCode.ErrData;
            }
            return ErrCode.Success;
        
        }

        static ErrCode GetUserCourseInfo(short job_id, int token, out UserCourseInfo info)
        {
            info = null;
            UserCourseInfoReq infoRe = new UserCourseInfoReq();
            infoRe.Job_Id = job_id;
            infoRe.Token = token;

            ClientRequest req = new ClientRequest(ClientFunc.GetSelfUserCourseInfo, infoRe);
            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 GetTable() 
        {
           return SystemData.StreeTable();
        }
        
    }
}
