using StuMgmLib.MyNameSpace;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace StuMgmClient
{
    class Utility
    {
        //序列化
        internal static bool BinSerialize<T>(T data, out byte[] buff)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                BinaryFormatter iFormatter = new BinaryFormatter();
                iFormatter.Serialize(ms, data);
                buff = ms.GetBuffer();
                return true;
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                buff = null;
                return false;
            }
        }
        //反序列化
        internal static bool BinDeserialize(byte[] data, out ServerResponse o)
        {
            try
            {
                MemoryStream ms = new MemoryStream(data);
                BinaryFormatter iFormatter = new BinaryFormatter();
                o = (ServerResponse)iFormatter.Deserialize(ms);
                return true;
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                o = null;
                return false;
            }
        }

        //字典转字符串
        internal static string DicParsing(Dictionary<short, CourseStatusEnum> dic)
        {
            string stateText = "";
            foreach (var item in dic)
            {
                stateText = stateText + item.Key.ToString() + ":" + ((int)item.Value).ToString() + ";";
            }
            return stateText;
        }

        internal static string InitState(int state) 
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(0, "未开始"); dic.Add(1, "进行中"); dic.Add(2, "待验收"); dic.Add(3, "验收成功"); dic.Add(4, "验收失败");
            return dic[state];
        }
    }
}
