using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DbCommon.Enties.DbModels;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Logger;

namespace ProjectManagementSystem.UI
{
    public partial class StationUI : UserControl
    {
        private StationInfo m_info;

        public StationInfo StationInfo
        {
            get { return m_info; }
        }
        public StationUI()
        {
            InitializeComponent();
        }

        public void Init(StationInfo info)
        {
            m_info = info;
        }

        public void Display()
        {
            if (!Visible) return;

            this.labName.Text = $"{m_info.Name}{(m_info.Traffic ? "(管制)" : null)}{Environment.NewLine}地标{m_info.Mark}(指令{m_info.Command}){Environment.NewLine}{$"倒计时{m_info.CountSec}"}{Environment.NewLine}{m_info.AgvAlarmText}";
            this.labName.ForeColor = m_info.Online ? Color.Black : Color.DarkGray;

            this.labCar.ForeColor = m_info.Online ? Color.Black : Color.DarkGray;
            this.labCar.BackColor = m_info.HasCar ? Color.LightGreen : Color.LightGray;
            this.labCar.Text = $"AGV{m_info.AgvId},R{m_info.CurrentRoute},0x{m_info.AgvStausValue:X2}";

            switch (m_info.Status)
            {
                case LocationStatus.None:
                    labName.BackColor = Color.LightGray;
                    break;
                case LocationStatus.Filled:
                    labName.BackColor = Color.Yellow;
                    break;
                case LocationStatus.Empty:
                    labName.BackColor = Color.Green;
                    break;
                case LocationStatus.Locked:
                    labName.BackColor = Color.Blue;
                    break;
                case LocationStatus.Exception:
                    labName.BackColor = Color.Red;
                    break;
                default:
                    labName.BackColor = Color.DarkGray;
                    break;
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            m_info.HasCar = !m_info.HasCar;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            m_info.Status = LocationStatus.None;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (m_info.Online
                && m_info.HasCar
                && m_info.ReleaseCold)
            {
                if (m_info.Traffic)
                {
                    ShowErrMsg("管制中，不能放行！！！");
                    return;
                }

                string text = $"确定要手动放行？";
                var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dialogResult != DialogResult.OK)
                {
                    return;
                }

                DeviceControl.Instance.ReleaseAgvOnStop(m_info.Mark, m_info.CurrentRoute, m_info.AgvId);
                m_info.LastReleaseTime = DateTime.Now.Ticks;
                CLog.Instance.TaskLog.WriteInfo($"手动放行，地标：{m_info.Mark}，路线：{m_info.CurrentRoute}，AGV：{m_info.AgvId}，{m_info.Name}，0x{m_info.AgvStausValue:X2}");
            }

        }

        private void ShowErrMsg(string text)
        {
            MessageBox.Show(text, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
