using MetroFramework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace HSUCO_Cargo_Garage_Operation_Program.CustomUserControl
{
    public partial class ApplicantControl : UserControl
    {

        public ApplicantControl()
        {
            InitializeComponent();

        }

        public void GarageName(string name)
        {
            label1.Text = $"현 프로그램의 차고지는 {Global.GlobalSettings.GarageName}입니다.\r\n입력시 필히 확인 바랍니다.";
        }

        void metroButton_Register_Click(object sender, EventArgs e)
        {
            if (metroTextBox_CargoVehicleNumber.Text.Trim() == string.Empty)
            {
                MetroMessageBox.Show(this, "차량번호(화물)가 입력되지 않았습니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                metroTextBox_CargoVehicleNumber.Text = string.Empty;
                metroTextBox_CargoVehicleNumber.Focus();
                return;
            }
            if (metroTextBox_CargoVehicleName.Text.Trim() == string.Empty)
            {
                MetroMessageBox.Show(this, "차　　종(화물)이 입력되지 않았습니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                metroTextBox_CargoVehicleName.Text = string.Empty;
                metroTextBox_CargoVehicleName.Focus();
                return;
            }
            if (metroTextBox_Name.Text.Trim() == string.Empty)
            {
                MetroMessageBox.Show(this, "이름이 입력되지 않았습니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                metroTextBox_Name.Text = string.Empty;
                metroTextBox_Name.Focus();
                return;
            }
            if (metroTextBox_PersonalNumber.Text.Trim() == string.Empty)
            {
                MetroMessageBox.Show(this, "생년월일(법인등록번호)이 입력되지 않았습니다..", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                metroTextBox_PersonalNumber.Text = string.Empty;
                metroTextBox_PersonalNumber.Focus();
                return;
            }
            if (maskedTextBox_Phone.Text.Trim() == string.Empty)
            {
                MetroMessageBox.Show(this, "연락처가 입력되지 않았습니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                maskedTextBox_Phone.Text = string.Empty;
                maskedTextBox_Phone.Focus();
                return;
            }

            if (metroTextBox_Address.Text.Trim() == string.Empty)
            {
                MetroMessageBox.Show(this, "주소가 입력되지 않았습니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                metroTextBox_Address.Text = string.Empty;
                metroTextBox_Address.Focus();
                return;
            }

            bool already = Database.ApplicantCheckAlready(metroTextBox_CargoVehicleNumber.Text.Trim());

            if (already)
            {
                MetroMessageBox.Show(this, metroTextBox_CargoVehicleNumber.Text.Trim() + "차량은 이미 신청된 차량입니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ResetInput();
                metroTextBox_CargoVehicleNumber.Focus();
                return;
            }

            already = Database.UserListCheckAlready(metroTextBox_CargoVehicleNumber.Text.Trim());
            if (already)
            {
                MetroMessageBox.Show(this, metroTextBox_CargoVehicleNumber.Text.Trim() + "현재 사용중인 차량입니다.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ResetInput();
                metroTextBox_CargoVehicleNumber.Focus();
                return;
            }
            var applicant = new ApplicantInformation();
            applicant.ApplicantType = metroRadioButton_Draw.Checked ? EApplicantType.Lots : EApplicantType.Wait; // 추첨이 true, 대기가 false
            if (metroRadioButton_Individual.Checked)
            {
                applicant.Owenr = EOwner.Individual;
            }
            else if (metroRadioButton_IIC.Checked)
            {
                applicant.Owenr = EOwner.IndividualIntoCorporation;
            }
            else
            {
                applicant.Owenr = EOwner.Corporation;
            }


            applicant.Name = metroTextBox_Name.Text.Trim();
            applicant.PersonalNumber = metroTextBox_PersonalNumber.Text.Trim();
            applicant.Phone = maskedTextBox_Phone.Text.Trim();
            applicant.Address = metroTextBox_Address.Text.Trim();
            applicant.VehicleType = (EVehicleType)metroComboBox_VehicleType.SelectedIndex;
            applicant.CargoVehicleNumber = metroTextBox_CargoVehicleNumber.Text.Trim();
            applicant.CargoVehicleName = metroTextBox_CargoVehicleName.Text.Trim();
            applicant.PassengerVehicleNumber = metroTextBox_PassengerVehicleNumber.Text.Trim();
            applicant.PassengerVehicleName = metroTextBox_PassengerVehicleName.Text.Trim();
            applicant.Date = DateTime.Now;


            var resultInsert = Database.SetApplicant(applicant);
            if (resultInsert != string.Empty)
            {
                ResetInput();
                MetroMessageBox.Show(this, "정상적으로 등록되었습니다.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                PrintFilingReceiptInformation printFilingReceiptInformation = new PrintFilingReceiptInformation();
                printFilingReceiptInformation.No = resultInsert;
                printFilingReceiptInformation.Name = applicant.Name;
                printFilingReceiptInformation.PersonalNumber = applicant.PersonalNumber;

                printFilingReceiptInformation.Address = applicant.Address;
                printFilingReceiptInformation.CargoVehicleNumber = applicant.CargoVehicleNumber;
                printFilingReceiptInformation.PassengerVehicleNumber = applicant.PassengerVehicleNumber;
                printFilingReceiptInformation.DateReception = applicant.Date;
                try
                {
                    PrintFilingReceipt printFilingReceipt = new PrintFilingReceipt(new List<PrintFilingReceiptInformation>() { printFilingReceiptInformation });
                    printFilingReceipt.Print();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

            }
            else
            {
                MetroMessageBox.Show(this, "등록에 실패하였습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void metroButton_Cancel_Click(object sender, EventArgs e)
        {
            ResetInput();
        }
        void ResetInput()
        {
            metroTextBox_CargoVehicleNumber.Text = string.Empty;
            metroTextBox_CargoVehicleName.Text = string.Empty;

            metroTextBox_PassengerVehicleNumber.Text = string.Empty;
            metroTextBox_PassengerVehicleName.Text = string.Empty;

            metroTextBox_Name.Text = string.Empty;
            metroTextBox_PersonalNumber.Text = string.Empty;
            maskedTextBox_Phone.Text = string.Empty;
            metroTextBox_Address.Text = string.Empty;

            metroTextBox_CargoVehicleNumber.Focus();
        }

        private void ApplicantControl_Load(object sender, EventArgs e)
        {
            metroComboBox_VehicleType.SelectedIndex = 0;
        }
    }
}