using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text;
using System.Threading.Tasks;
using UnNomalized_node.message;
using Google.Protobuf;

namespace UnNomalized_node
{
    class communication_data
    {
        class Nested
        {
            // 创建模板类实例，参数2设为true表示支持私有构造函数
            internal static readonly communication_data instance = Activator.CreateInstance(typeof(communication_data), true) as communication_data;
        }
        private int  m_terminal_id;
        private int m_unit_id;
        private static communication_data instance = null;
        //地面雷达数据
        private TimedData<Message.Ground_status_msg> m_timed_lidar_statu_msg =
            new TimedData<Message.Ground_status_msg>(new Message.Ground_status_msg());
        //数据库状态数据
        private TimedData<Message.Parkspace_allocation_status_msg> m_timed_parkspace_statu_msg =
            new TimedData<Message.Parkspace_allocation_status_msg>(new Message.Parkspace_allocation_status_msg());
        //号牌机数据
        private TimedData<string> m_timed_car_license=new TimedData<string>("");
        //终端发来的状态数据，包括单片机数据
        private TimedData<Message.terminal_status_msg> m_timed_terminal_statu =
            new TimedData<Message.terminal_status_msg>(new Message.terminal_status_msg());
        //获取号牌线程
        NumMachine.NumMachineLinker m_car_number;
        private bool m_car_number_condition = false;
        private Thread m_car_number_thread = null;
        //发布消息线程
        private bool m_publish_condition = false;
        private Thread m_pubilish_thread = null;
        public static communication_data Instance
        {
            get
            {
                return Nested.instance;
            }
        }
        private communication_data()
        {
            m_terminal_id = int.Parse(tool.json_file_operation.Instance.read_json_string("ternimalNumber"));
            m_unit_id= int.Parse(tool.json_file_operation.Instance.read_json_string("unit_id"));
            m_timed_car_license.Set_timeout_ms(10*60*1000);
            m_car_number = new NumMachine.NumMachineLinker();
            m_car_number.Start();
            //启动发布线程和号牌机线程
            m_publish_condition = true;
            m_pubilish_thread = new Thread(publish);
            m_pubilish_thread.Start();
            m_car_number_condition = true;
            m_car_number_thread = new Thread(get_car_number);
            m_car_number_thread.Start();
        }
        public void Release()
        {
            m_car_number.Stop();
            m_car_number_condition = false;
            if (m_car_number_thread.IsAlive)
            {
                m_car_number_thread.Abort();
            }
            m_publish_condition = false;
            if(m_pubilish_thread.IsAlive)
            {
                m_pubilish_thread.Abort();
            }
        }
        public TimedData<Message.Ground_status_msg> lidar_statu
        {
            get
            {
                return m_timed_lidar_statu_msg;
            }
            set
            {
                if(value.Value.IdStruct.TerminalId==m_terminal_id)
                    m_timed_lidar_statu_msg = value;
            }
        }
        public TimedData<Message.Parkspace_allocation_status_msg> parkspace_statu
        {
            get
            {
                return m_timed_parkspace_statu_msg;
            }
            set
            {
                if(value.Value.UnitId==m_unit_id)
                    m_timed_parkspace_statu_msg = value;
            }
        }

        public TimedData<string> car_license
        {
            get
            {
                return m_timed_car_license;
            }
            set
            {
                m_timed_car_license = value;
            }

        }

        public TimedData<Message.terminal_status_msg> terminal_statu
        {
            get
            {
                return m_timed_terminal_statu;
            }
            set
            {
                if (value.Value.IdStruct != null)
                {
                    if (value.Value.IdStruct.TerminalId == m_terminal_id)
                        m_timed_terminal_statu = value;
                }
            }

        }
        public int TerminalID
        {
            get
            {
                return m_terminal_id;
            }
        }
        public int UnitID
        {
            get
            {
                return m_unit_id;
            }
        }

        void publish()
        {
            Message.Base_info base_Info = new Message.Base_info();
            base_Info.MsgType = Message.Message_type.EUnNormalizedModuleStatuMsg;
            base_Info.Sender = Message.Communicator.EEmpty;
            base_Info.Receiver = Message.Communicator.ETerminor;
            while(m_publish_condition==true)
            {
                Message.UnNormalized_module_statu_msg msg = new Message.UnNormalized_module_statu_msg();
                msg.BaseInfo = base_Info;
                Message.Id_struct id_struct = new Message.Id_struct();
                id_struct.TerminalId = communication_data.Instance.TerminalID;
                msg.IdStruct=id_struct;
                Message.Error_manager error = new Message.Error_manager();
                error.ErrorCode = 0;
                if (car_license.IsTimeout())
                {
                    error.ErrorCode = -1;
                    error.ErrorDescription += "号牌机断开\n";
                }
                else
                {
                    msg.CarLicense = car_license.Value;
                }
                if(lidar_statu.IsTimeout())
                {
                    error.ErrorCode = -1;
                    error.ErrorDescription += "雷达断开\n";
                }
                if(terminal_statu.IsTimeout())
                {
                    error.ErrorCode = -1;
                    error.ErrorDescription += "单片机断开\n";
                }
                msg.StatuCode = error;
                communication.unnormalized_communication.Instance.encapsulate_msg(msg.ToByteArray());
                Thread.Sleep(100);
            }
        }

        void get_car_number()
        {
            while (m_car_number_condition==true)
            {
                string number = m_car_number.GetLicensePlate(0);
                if(number!=null&&number!="")
                {
                    m_timed_car_license = number;
                    m_timed_car_license.Set_timeout_ms(10 * 60 * 1000);
                }
                Thread.Sleep(200);
            }
        }



    }
}
