using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.WebChatServer
{
    class Program
    {
        static void Main(string[] args)
        {
            ChatServer sr = new ChatServer();

            if (sr.Setup())
            {
                bind();
                string type = string.Empty;
                while (type != "0")
                {
                    type = Console.ReadLine();
                    switch (type)
                    {
                        case "1":
                            switch (sr.Start())
                            {
                                case 1: Console.WriteLine("服务开启成功"); break;
                                case 2: Console.WriteLine("服务已开启，无需再开启"); break;
                                case 3: Console.WriteLine("服务开启失败"); break;
                            }
                            break;
                        case "2":
                            switch (sr.Stop())
                            {
                                case 1: Console.WriteLine("服务关闭成功"); break;
                                case 2: Console.WriteLine("服务已关闭，无需再关闭"); break;
                            }
                            break;
                        case "3":
                            Console.WriteLine("-----在线客户列表-----");
                            foreach (var cus in sr.Customers)
                            {
                                var ser = sr.Servicers.Where(p => p.SessionID == cus.ServicerSessionID).FirstOrDefault();
                                Console.WriteLine(cus.Name + "(" + cus.Code + ")--" + (ser == null ? "未分配" : ser.Name + "(" + ser.Code + ")"));
                            }
                            break;
                        case "4":
                            Console.WriteLine("-----在线客服列表-----");
                            foreach (var ser in sr.Servicers)
                            {
                                Console.WriteLine(ser.Name + "--" + ser.Code);
                            }
                            break;
                        case "5": Console.WriteLine("当前服务状态：" + sr.State().ToString()); break;
                        case "9": Console.Clear(); bind(); break;
                        case "0": sr.Stop(); break;
                        default:
                            Console.WriteLine("-------无效指令--------");
                            break;
                    }
                }
            }
            else
            {
                Console.WriteLine("-----服务初始化失败-----");
                Console.ReadLine();
            }
        }

        static void bind()
        {
            Console.WriteLine("-----服务初始化成功-----");
            Console.WriteLine("-[1] 开启服务");
            Console.WriteLine("-[2] 关闭服务");
            Console.WriteLine("-[3] 显示当前在线客户");
            Console.WriteLine("-[4] 显示当前在线客服");
            Console.WriteLine("-[5] 服务状态");
            Console.WriteLine("-[9] 清除");
            Console.WriteLine("-[0] 退出");
            Console.WriteLine("---------------------");
        }
    }
}
