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 parkMonitor.entity;
using parkMonitor.server.CoreThread;
using parkMonitor.DBLocation;
using parkMonitor.model;

namespace parkMonitor.manualParking
{
    public partial class ManualParking : Form
    {
        Command storeCmd = new Command();
        IEquipments qt = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
        DBLocationOperator locationOper = new DBLocationOperator();

        string numberRecord = "";
        public ManualParking()
        {
            InitializeComponent();
        }

        private void store_Click(object sender, EventArgs e)
        {
            storeCmd.manual = true;
            storeCmd.commandType = 's';
            storeCmd.userID = locationOper.GetUserID(tel.Text).ToString();
            storeCmd.LicenseNum = numberPlate.Text;
            storeCmd.garageID = Convert.ToInt32(selectGarage.Text);
            int garageFreeSpace = 0;
            bool isTelRegister = locationOper.IsTelRegister(tel.Text);
            if (isTelRegister == false)
            {
                MessageBox.Show("该号码未被注册，请先注册");
            }
            else if (storeCmd.garageID != 0)
            {
                garageFreeSpace = locationOper.getGarageFreeSpace(storeCmd.garageID);
                if (garageFreeSpace > 0)
                {
                    if (storeCmd.userID != "" && storeCmd.LicenseNum != numberRecord)
                    {
                        qt.SetMessage((Command)storeCmd.Clone());
                        MessageBox.Show("发送成功");
                        numberRecord = storeCmd.LicenseNum;
                    }
                    else
                    {
                        MessageBox.Show("不要重复发送指令");
                    }
                }
                else
                {
                    MessageBox.Show("该车库没有剩余车位");
                }
            }
        }

        private void fetch_Click(object sender, EventArgs e)
        {
            storeCmd.manual = true;
            storeCmd.commandType = 'f';
            storeCmd.userID = locationOper.GetUserID(tel.Text).ToString();
            storeCmd.LicenseNum = numberPlate.Text;
            storeCmd.TimeRecord = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            storeCmd.garageID = Convert.ToInt32(selectGarage.Text);
            bool isTelRegister = locationOper.IsTelRegister(tel.Text);
            bool isNumberPlate = locationOper.IsNumberPlate(storeCmd.LicenseNum, storeCmd.garageID);
            if (isTelRegister == false)
            {
                MessageBox.Show("该号码未被注册，请先注册");
            }
            else if (isNumberPlate == false)
            {
                MessageBox.Show("车库中查无此车");
            }
            else
            {
                storeCmd.parkingRecordsID = locationOper.GetParkingRecordsID(storeCmd.LicenseNum);
                if (storeCmd.userID != "" && storeCmd.LicenseNum != "" && storeCmd.parkingRecordsID != 0)
                {
                    qt.SetMessage((Command)storeCmd.Clone());
                    MessageBox.Show("发送成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("电话号码或号牌错误");
                }
            }

        }
    }
}
