using Message;
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;

namespace parkspace_manager
{
    public partial class FormStatusSelect : Form
    {
        /// <summary>
        /// 状态修改回调格式声明
        /// </summary>
        /// <param name="status"></param>
        public delegate void StatusCallback(Parkspace_status status, Button btn);
        /// <summary>
        /// 修改状态回调
        /// </summary>
        public StatusCallback m_set_status_callback = null;
        /// <summary>
        /// 已选择的状态
        /// </summary>
        public Parkspace_status m_selected_status { get; set; }
        /// <summary>
        /// 待修改按钮
        /// </summary>
        public Button m_linked_btn;
        /// <summary>
        /// 构造
        /// </summary>
        public FormStatusSelect(Button btn)
        {
            InitializeComponent();
            m_selected_status = Parkspace_status.EParkspaceError;
            m_linked_btn = btn;
        }

        /// <summary>
        /// 设置回调
        /// </summary>
        public void SetCallback(StatusCallback callback)
        {
            m_set_status_callback = callback;
        }

        /// <summary>
        /// 确认状态修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_confirm_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() => {
                this.Invoke(new Action(()=> {
                    Index2status(listBox_status.SelectedIndex);
                    m_set_status_callback?.Invoke(m_selected_status, m_linked_btn);
                    Close();
                }));
            });
            btn_confirm.Enabled = false;
            btn_cancel.Enabled = false;
        }

        /// <summary>
        /// 选项index转状态枚举
        /// </summary>
        /// <param name="index"></param>
        private void Index2status(int index)
        {
            switch(index)
            {
                case 0:
                    m_selected_status = Parkspace_status.EParkspaceEmpty;
                    break;
                case 1:
                    m_selected_status = Parkspace_status.EParkspaceOccupied;
                    break;
                case 2:
                    m_selected_status = Parkspace_status.EParkspaceReserved;
                    break;
                case 3:
                    m_selected_status = Parkspace_status.EParkspaceLocked;
                    break;
                case 4:
                    m_selected_status = Parkspace_status.EParkspaceError;
                    break;
            }
        }

        /// <summary>
        /// 取消状态修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_cancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
