using MetroFramework;
using MetroFramework.Forms;
using System;
using System.Windows.Forms;

namespace HSUCO_Cargo_Garage_Operation_Program.CustomForm
{
    public partial class UpdateUserData : MetroForm
    {
        private UpdateUserInformation _updateUserInformation;

        public UpdateUserData(UpdateUserInformation updateUserInformation)
        {
            InitializeComponent();

            _updateUserInformation = updateUserInformation;
            UpdateControl();
        }

        public void UpdateControl()
        {
            if (_updateUserInformation.UserInformationType == EUserInformationType.ApplicantList)
            {
                metroComboBox_VehicleType.Enabled = true;
            }
            else
            {
                metroComboBox_VehicleType.Enabled = false;
            }
            switch (_updateUserInformation.Owner)
            {
                case EOwner.Individual:
                    metroRadioButton_Individual.Checked = true;
                    break;

                case EOwner.IndividualIntoCorporation:
                    metroRadioButton_IIC.Checked = true;
                    break;

                case EOwner.Corporation:
                    metroRadioButton_Corporation.Checked = true;
                    break;
            }
            metroTextBox_CargoVehicleNumber.Text = _updateUserInformation.CargoVehicleNumber;
            metroTextBox_CargoVehicleName.Text = _updateUserInformation.CargoVehicleName;
            metroTextBox_PassengerVehicleNumber.Text = _updateUserInformation.PassengerVehicleNumber;
            metroTextBox_PassengerVehicleName.Text = _updateUserInformation.PassengerVehicleName;
            metroComboBox_VehicleType.SelectedIndex = (int)_updateUserInformation.VehicleType;
            metroTextBox_Name.Text = _updateUserInformation.Name;
            metroTextBox_PersonalNumber.Text = _updateUserInformation.PersonalNumber;
            maskedTextBox_Phone.Text = _updateUserInformation.Phone;
            metroTextBox_Address.Text = _updateUserInformation.Address;
        }

        private void metroButton_Update_Click(object sender, EventArgs e)
        {
            bool change = false;
            if (_updateUserInformation.CargoVehicleNumber != metroTextBox_CargoVehicleNumber.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.CargoVehicleName != metroTextBox_CargoVehicleName.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.PassengerVehicleNumber != metroTextBox_PassengerVehicleNumber.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.PassengerVehicleName != metroTextBox_PassengerVehicleName.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.VehicleType != (EVehicleType)metroComboBox_VehicleType.SelectedIndex)
            {
                change = true;
            }
            else if (_updateUserInformation.Name != metroTextBox_Name.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.PersonalNumber != metroTextBox_PersonalNumber.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.Phone != maskedTextBox_Phone.Text.Trim())
            {
                change = true;
            }
            else if (_updateUserInformation.Address != metroTextBox_Address.Text.Trim())
            {
                change = true;
            }
            EOwner changeOwner;
            if (metroRadioButton_Individual.Checked)
            {
                changeOwner = EOwner.Individual;
            }
            else if (metroRadioButton_IIC.Checked)
            {
                changeOwner = EOwner.IndividualIntoCorporation;
            }
            else
            {
                changeOwner = EOwner.Corporation;
            }
            if (changeOwner != _updateUserInformation.Owner)
            {
                change = true;
            }
            if (!change)
            {
                MetroMessageBox.Show(this, "변경된 내용이 없습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _updateUserInformation.Owner = changeOwner;
            _updateUserInformation.Name = metroTextBox_Name.Text.Trim();
            _updateUserInformation.PersonalNumber = metroTextBox_PersonalNumber.Text.Trim();
            _updateUserInformation.Phone = maskedTextBox_Phone.Text.Trim();
            _updateUserInformation.Address = metroTextBox_Address.Text.Trim();
            _updateUserInformation.VehicleType = (EVehicleType)metroComboBox_VehicleType.SelectedIndex;
            _updateUserInformation.CargoVehicleNumber = metroTextBox_CargoVehicleNumber.Text.Trim();
            _updateUserInformation.CargoVehicleName = metroTextBox_CargoVehicleName.Text.Trim();
            _updateUserInformation.PassengerVehicleNumber = metroTextBox_PassengerVehicleNumber.Text.Trim();
            _updateUserInformation.PassengerVehicleName = metroTextBox_PassengerVehicleName.Text.Trim();
            var result = Database.UpdateUserData(_updateUserInformation);
            if (result)
            {
                MetroMessageBox.Show(this, "변경이 완료되었습니다..", "이용자 정보 수정", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            DialogResult = DialogResult.OK;
        }

        private void metroButton_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}