
using parkMonitor.entity;
using parkMonitor.LOG;
using parkMonitor.model;
using parkMonitor.server.uiLogServer;
using parkMonitor.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace parkMonitor.server.NumMachine
{
    class ParkingSimul
    {
        public static ParkingSimul ins;
        private const string hostname = "192.168.0.198";
        private const int port = 9000;
        private const string userId = "18202736439";
        private const string garageId = "1";
        private const string header = "鄂A";
        private const int licInterval = 30000;//30秒
        private Random rnd;
        private Dictionary<int, CarStatusStru> numStatusMap = new Dictionary<int, CarStatusStru>();
        MessageUTF8 msgToWeb = new MessageUTF8();
        IEquipments queue = null;
        //DBOperation oper = new DBOperation();
        private NetworkStream ns { get; set; }

        public ParkingSimul()
        {
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    numStatusMap.Add(i, new CarStatusStru(header + (i * 11111).ToString("D5"), 0));
                }
                while (queue == null)
                {
                    queue = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
                }
            }).Wait();
            Task.Factory.StartNew(() =>
            {
                Run();
            });
        }

        public void Store(string lic)
        {
            msgToWeb = new MessageUTF8();
            msgToWeb.cmd = 's';
            msgToWeb.garageID = 1;
            msgToWeb.context = lic;
            msgToWeb.sender = "1";
            msgToWeb.receiver = hostname;
            TcpClient client = new TcpClient();
            while (true)
            {
                try
                {
                    client = new TcpClient();
                    client.Connect(hostname, port);
                    ns = client.GetStream();

                    byte[] bytes = new byte[256];
                    int bytesRead = ns.Read(bytes, 0, bytes.Length);
                    MessageUTF8 temp = (MessageUTF8)JsonByByteToObjectTools.JsonByteToObj<MessageUTF8>(bytes);
                    ns.Flush();

                    byte[] byteMessage = JsonByByteToObjectTools.ObjToJsonByte(msgToWeb);
                    ns.Write(byteMessage, 0, byteMessage.Length);
                    ns.Flush();
                    ns.Close();
                    client.Close();
                    break;
                }
                catch { }
                Thread.Sleep(5000);
            }
        }

        public void Fetch(string lic)
        {
            msgToWeb = new MessageUTF8();
            msgToWeb.cmd = 'f';
            msgToWeb.garageID = 1;
            msgToWeb.context = lic;
            msgToWeb.sender = "1";
            msgToWeb.receiver = hostname;
            TcpClient client = new TcpClient();
            while (true)
            {
                try
                {
                    client.Connect(hostname, port);
                    ns = client.GetStream();

                    byte[] bytes = new byte[256];
                    int bytesRead = ns.Read(bytes, 0, bytes.Length);
                    MessageUTF8 temp = (MessageUTF8)JsonByByteToObjectTools.JsonByteToObj<MessageUTF8>(bytes);
                    ns.Flush();

                    byte[] byteMessage = JsonByByteToObjectTools.ObjToJsonByte(msgToWeb);
                    ns.Write(byteMessage, 0, byteMessage.Length);
                    ns.Flush();
                    break;
                }
                catch { }
                Thread.Sleep(5000);
            }
        }

        public void Run()
        {
            int cycleCount = 2;
            //while (cycleCount-->0)
            while(true)
            {
                rnd = new Random(DateTime.Now.Millisecond);
                if (numStatusMap.Count > 0)
                {
                    int index = -1, status = -1, count = numStatusMap.Count;
                    do
                    {
                        index = rnd.Next(0, numStatusMap.Count);
                        status = numStatusMap[index].status;
                    } while (count-- > 0 && status != 0 && status != 1);
                    if (status == 0)
                    {
                        Store(numStatusMap[index].licNum);
                        numStatusMap[index].status = 2;
                    }
                    else if (status == 1)
                    {
                        Fetch(numStatusMap[index].licNum);
                        numStatusMap[index].status = 3;
                    }
                }
                Thread.Sleep(licInterval);
            }
        }

        public void Update(int index)
        {
            int status = numStatusMap[index].status;
            if (status == 2)
            {
                numStatusMap[index].status = 1;
            }
            else if (status == 3)
            {
                numStatusMap[index].status = 0;
            }
        }
    }
    class CarStatusStru
    {
        public string licNum { get; set; }
        public int status { get; set; }
        public CarStatusStru()
        {
            licNum = "";
            status = 0;
        }
        public CarStatusStru(string str, int stat)
        {
            licNum = str;
            status = stat;
        }
    }
}
