using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using Message;
using UnNomalized_node.message;

namespace UnNomalized_node
{
    public partial class FormArrow : Form
    {
       
        public enum Car_statu
        {
            /// <summary>
            /// 车辆正确
            /// </summary>
            eOK = 0,
            /// <summary>
            /// 箭头向下/上边超界
            /// </summary>
            eOutTop = 0x01,
            /// <summary>
            /// 箭头向上/下边超界
            /// </summary>
            eOutDown = 0x02,
            /// <summary>
            /// 箭头向右/左边超界
            /// </summary>
            eOutLeft = 0x04,
            /// <summary>
            /// 箭头向左/右边超界
            /// </summary>
            eOutRight = 0x08,
  
            /// <summary>
            /// 箭头向左上/右下超界
            /// </summary>
            eOutRightDown = eOutRight|eOutDown, //8+2
            /// <summary>
            /// 箭头向右上/左下超界
            /// </summary>
            eOutLeftDown = eOutLeft|eOutDown,   //4+2
            /// <summary>
            /// 箭头向右下/左上超界
            /// </summary>
            eOutRightTop = eOutRight|eOutTop,   //8+1
            /// <summary>
            /// 箭头向左下/右上超界
            /// </summary>
            eOutLeftTop = eOutLeft|eOutTop,     //4+1

            eOutRotateLeft=0x03,
            eOutRotateRight=0x07,

            //未识别到车/噪声、无数据
            eNoise=0x0b,
            //无点云
            eNodata=0x0c,
            //断开连接
            eDisconnect=0x0d,
            eWidthError=0x0e,
            eWheelBaseError=0x0f,
            //数量
            eNum=0x16,
        };
        Timer m_timer;
        PictureBox[] m_arrows_images = new PictureBox[(int)Car_statu.eNum];
        PictureBox m_text_image_warning=new PictureBox();
        PictureBox m_text_image_ok=new PictureBox();
        PictureBox m_text_image_disconnect=new PictureBox();
        PictureBox m_text_image_nocar=new PictureBox();
        PictureBox m_text_image_widtherror = new PictureBox();
        PictureBox m_text_image_wheelbaseerror = new PictureBox();
        Car_statu m_last_lidar_statu = Car_statu.eNodata;

        //语音播报
        //voice_speaker m_speaker = null;
       
        public FormArrow()
        {
            InitializeComponent();
           // m_speaker = new voice_speaker(); 
        }

        private void FormArrow_Load(object sender, EventArgs e)
        {
            m_timer = new System.Windows.Forms.Timer();
            //设置定时器属性
            m_timer.Tick += new EventHandler(HandleTime);
            m_timer.Interval = 500;
            m_timer.Enabled = true;
            //开启定时器
            m_timer.Start();

            tableLayoutPanel2.Dock = DockStyle.Fill;

            for (int i = 0; i < (int)Car_statu.eNum; ++i)
            {
                m_arrows_images[i] = new PictureBox();   
            }
            for (int i = 0; i < (int)Car_statu.eNum; ++i)
            {
                m_arrows_images[i].Visible = false;
                tableLayoutPanel2.Controls.Add(m_arrows_images[i], 0, 1);
                m_arrows_images[i].Dock = DockStyle.Fill;
                m_arrows_images[i].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;  
            }

            m_arrows_images[(int)Car_statu.eOutTop].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutDown].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutLeft].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutRight].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutLeftTop].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutLeftDown].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutRightTop].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutRightDown].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutRotateLeft].BackColor = Color.DarkOrange;
            m_arrows_images[(int)Car_statu.eOutRotateRight].BackColor = Color.DarkOrange;

            tableLayoutPanel2.Controls.Add(m_text_image_warning, 0, 0);
            tableLayoutPanel2.Controls.Add(m_text_image_ok, 0, 0);
            tableLayoutPanel2.Controls.Add(m_text_image_disconnect, 0, 0);
            tableLayoutPanel2.Controls.Add(m_text_image_nocar, 0, 0);
            tableLayoutPanel2.Controls.Add(m_text_image_widtherror, 0, 0);
            tableLayoutPanel2.Controls.Add(m_text_image_wheelbaseerror, 0, 0);

            m_text_image_warning.Dock = DockStyle.Fill;
            m_text_image_ok.Dock = DockStyle.Fill;
            m_text_image_disconnect.Dock = DockStyle.Fill;
            m_text_image_nocar.Dock = DockStyle.Fill;
            m_text_image_widtherror.Dock = DockStyle.Fill;
            m_text_image_wheelbaseerror.Dock = DockStyle.Fill;

            m_text_image_warning.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            m_text_image_ok.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            m_text_image_disconnect.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            m_text_image_nocar.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            m_text_image_widtherror.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            m_text_image_wheelbaseerror.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

            m_text_image_warning.Visible = false;
            m_text_image_ok.Visible = false;
            m_text_image_disconnect.Visible = false;
            m_text_image_nocar.Visible = false;
            m_text_image_widtherror.Visible = false;
            m_text_image_wheelbaseerror.Visible = false;

            m_text_image_warning.BackColor = Color.DarkOrange;

            m_arrows_images[(int)Car_statu.eOK].Image= Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "ok.png");
            m_arrows_images[(int)Car_statu.eOutRight].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_xiangzuo.png");
            m_arrows_images[(int)Car_statu.eOutLeft].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_xiangyou.png");
            m_arrows_images[(int)Car_statu.eOutDown].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_xiangshang.png");
            m_arrows_images[(int)Car_statu.eOutTop].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_xiangxia.png");
            m_arrows_images[(int)Car_statu.eOutRightDown].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_zuoshang.png");
            m_arrows_images[(int)Car_statu.eOutLeftDown].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_youshang.png");
            m_arrows_images[(int)Car_statu.eOutRightTop].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_zuoxia.png");
            m_arrows_images[(int)Car_statu.eOutLeftTop].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "jiantou_youxia.png");

            m_arrows_images[(int)Car_statu.eNodata].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "nocar.png");
            m_arrows_images[(int)Car_statu.eNoise].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "nocar.png");
            m_arrows_images[(int)Car_statu.eDisconnect].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "disconnect.png");
            m_arrows_images[(int)Car_statu.eWidthError].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "error.jpg");
            m_arrows_images[(int)Car_statu.eWheelBaseError].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "error.jpg");
            m_arrows_images[(int)Car_statu.eOutRotateLeft].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "leftRotate.png");
            m_arrows_images[(int)Car_statu.eOutRotateRight].Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "rightRotate.png");

            m_text_image_warning.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "warning.png");
            m_text_image_ok.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "text_ok.png");
            m_text_image_disconnect.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "disconnect.png");
            m_text_image_nocar.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "nocar.png");
            m_text_image_widtherror.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "width_error.png");
            m_text_image_wheelbaseerror.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory +
                "/Resource/arrows/" + "wheelbase_error.png");

            Set_arrow(Car_statu.eNoise);


        }
        public void HandleTime(Object myObject, EventArgs myEventArgs)
        {
            switch (m_last_lidar_statu)
            {
                case Car_statu.eOutTop:                
                case Car_statu.eOutDown:
                case Car_statu.eOutLeft:
                case Car_statu.eOutRight:
                case Car_statu.eOutLeftTop:
                case Car_statu.eOutLeftDown:
                case Car_statu.eOutRightTop:
                case Car_statu.eOutRightDown:
                case Car_statu.eOutRotateLeft:
                case Car_statu.eOutRotateRight:
                    m_arrows_images[(int)m_last_lidar_statu].Visible = (m_arrows_images[(int)m_last_lidar_statu].Visible == false);
                    break;
            }
            
        }

        public void Update_arrow()
        {

            TimedData<Message.Ground_status_msg> lidar_statu = communication_data.Instance.lidar_statu;
            terminal_status_msg terminal_statu = communication_data.Instance.terminal_statu.Value;


            //先判断光电开关
            if (terminal_statu.IdStruct != null)
            {
                if (terminal_statu.IdStruct.TerminalId == communication_data.Instance.TerminalID)
                {
                    //显示超界信息
                    if (terminal_statu.SinglechipData.OverBorderStatus != Message.Over_border_status.Normal)
                    {
                        switch (terminal_statu.SinglechipData.OverBorderStatus)
                        {
                            case Message.Over_border_status.Front:
                                Set_arrow(Car_statu.eOutTop);
                                break;
                            case Message.Over_border_status.Back:
                                Set_arrow(Car_statu.eOutDown);
                                break;
                            case Message.Over_border_status.Left:
                                Set_arrow(Car_statu.eOutLeft);
                                break;
                            case Message.Over_border_status.Right:
                                Set_arrow(Car_statu.eOutRight);
                                break;
                        }
                        return;
                    }
                    //门内光电无信号， 显示无车，返回
                    /*if (terminal_statu.SinglechipData.InsideExistenceFlag != 1)
                    {
                        Set_arrow(FormArrow.Car_statu.eNodata);
                        return;
                    }*/
                }
            }
            //光电正常，显示雷达超界信息
            if (lidar_statu.IsTimeout())
            {
                Set_arrow(FormArrow.Car_statu.eDisconnect);
            }
            else
            {

                int border_statu = lidar_statu.Value.BorderStatus;
                //先判断车辆是否超宽
                if (((border_statu >> 6) & 0x01) > 0)
                {
                    Set_arrow(Car_statu.eWidthError);
                    return;
                }
                //轴距超差
                if (((border_statu >> 7) & 0x01) > 0)
                {
                    Set_arrow(Car_statu.eWheelBaseError);
                    return;
                }
                //左右旋转
                if (((border_statu >> 9) & 0x01) > 0)
                {
                    Set_arrow(Car_statu.eOutRotateLeft);
                    return;
                }
                if (((border_statu >> 8) & 0x01) > 0)
                {
                    Set_arrow(Car_statu.eOutRotateRight);
                    return;
                }

                switch (lidar_statu.Value.GroundStatus)
                {
                    case Ground_statu.Nothing:
                        Set_arrow(FormArrow.Car_statu.eNodata);
                        break;
                    case Ground_statu.Noise:
                        /*if (m_last_lidar_statu != Car_statu.eOK)
                            Set_arrow(FormArrow.Car_statu.eNoise);*/
                        break;
                    case Ground_statu.CarCorrect:
                        Set_arrow(Car_statu.eOK);
                        break;
                    case Ground_statu.CarBorderReached:
                        switch ((Car_statu)border_statu)
                        {
                            case Car_statu.eOK:
                            case Car_statu.eOutTop:
                            case Car_statu.eOutDown:
                            case Car_statu.eOutLeft:
                            case Car_statu.eOutRight:
                            case Car_statu.eOutLeftTop:
                            case Car_statu.eOutLeftDown:
                            case Car_statu.eOutRightTop:
                            case Car_statu.eOutRightDown:
                                Set_arrow((Car_statu)border_statu);
                                break;
                        }
                        break;
                }

            }

        }
        private void Set_arrow(Car_statu statu)
        {
            if (m_last_lidar_statu == statu)
            {
                return;
            }

            switch (statu)
            {
                case Car_statu.eOK:
                    m_text_image_warning.Visible = false;
                    m_text_image_ok.Visible = true;
                    m_text_image_disconnect.Visible = false;
                    m_text_image_nocar.Visible = false;
                    m_text_image_widtherror.Visible = false;
                    m_text_image_wheelbaseerror.Visible = false;
                    m_arrows_images[(int)statu].Visible = true;
                    break;
                case Car_statu.eDisconnect:
                    m_text_image_warning.Visible = false;
                    m_text_image_ok.Visible = false;
                    m_text_image_disconnect.Visible = true;
                    m_text_image_nocar.Visible = false;
                    m_text_image_widtherror.Visible = false;
                    m_text_image_wheelbaseerror.Visible = false;
                    m_arrows_images[(int)statu].Visible = true;
                    break;
                case Car_statu.eNodata:
                    m_text_image_warning.Visible = false;
                    m_text_image_ok.Visible = false;
                    m_text_image_disconnect.Visible = false;
                    m_text_image_nocar.Visible = true;
                    m_text_image_widtherror.Visible = false;
                    m_text_image_wheelbaseerror.Visible = false;
                    m_arrows_images[(int)statu].Visible = true;
                    break;
                case Car_statu.eNoise:
                    m_text_image_warning.Visible = false;
                    m_text_image_ok.Visible = false;
                    m_text_image_disconnect.Visible = false;
                    m_text_image_nocar.Visible = true;
                    m_text_image_widtherror.Visible = false;
                    m_text_image_wheelbaseerror.Visible = false;
                    m_arrows_images[(int)statu].Visible = true;
                    break;
                case Car_statu.eWidthError:
                    m_text_image_warning.Visible = false;
                    m_text_image_ok.Visible = false;
                    m_text_image_disconnect.Visible = false;
                    m_text_image_nocar.Visible = false;
                    m_text_image_widtherror.Visible = true;
                    m_text_image_wheelbaseerror.Visible = false;
                    m_arrows_images[(int)statu].Visible = true;
                    break;
                case Car_statu.eWheelBaseError:
                    m_text_image_warning.Visible = false;
                    m_text_image_ok.Visible = false;
                    m_text_image_disconnect.Visible = false;
                    m_text_image_nocar.Visible = false;
                    m_text_image_widtherror.Visible = false;
                    m_text_image_wheelbaseerror.Visible = true;
                    m_arrows_images[(int)statu].Visible = true;
                    break;
                case Car_statu.eOutRight:
                case Car_statu.eOutLeft:
                case Car_statu.eOutTop:
                case Car_statu.eOutDown:
                case Car_statu.eOutLeftTop:
                case Car_statu.eOutLeftDown:
                case Car_statu.eOutRightTop:
                case Car_statu.eOutRightDown:
                case Car_statu.eOutRotateLeft:
                case Car_statu.eOutRotateRight:
                    m_text_image_warning.Visible = true;
                    m_text_image_ok.Visible = false;
                    m_text_image_disconnect.Visible = false;
                    m_text_image_nocar.Visible = false;
                    m_text_image_widtherror.Visible = false;
                    m_text_image_wheelbaseerror.Visible = false;
                    break;

            }
            m_arrows_images[(int)m_last_lidar_statu].Visible = false;
            m_last_lidar_statu = statu;

        }
        private void pictureBox_arrow_SizeChanged(object sender, EventArgs e)
        {
        }

        private void next_Click(object sender, EventArgs e)
        {
            next.Text = ((int.Parse(next.Text) + 1) % 16).ToString();
            Message.Locate_information locate_info = new Message.Locate_information();
            locate_info.LocateCorrect = true;
            Message.Ground_status_msg msg = new Message.Ground_status_msg();
            msg.GroundStatus = Message.Ground_statu.CarBorderReached;
            msg.BorderStatus = int.Parse(next.Text);
            communication_data.Instance.lidar_statu = msg;

            Console.WriteLine(msg.BorderStatus.ToString());

            
        }


        private void FormArrow_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_timer.Stop();

            //m_speaker.release();
        }
    }
}
