using System;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.FakeData;
using BookRequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V12.Book.RequestModel;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book.PassengerUserControls
{
    public partial class InfantUserControl : UserControl
    {
        private readonly Panel _passengersPanel;
        private readonly Action _calculateTotalPayableFunction;

        public InfantUserControl(
            decimal payable,
            bool isInternationalRoute,
            Panel passengersPanel,
            Action calculateTotalPayableFunction)
        {
            _passengersPanel = passengersPanel;
            _calculateTotalPayableFunction = calculateTotalPayableFunction;
            InitializeComponent();

            cmbDocType.SelectedIndex = 0;
            cmbTitle.SelectedIndex = 0;

            var birthDate = DateTime.Now.AddYears(-1);
            txtBirthdate_Year.Text = birthDate.ToString("yyyy");
            txtBirthdate_Month.Text = birthDate.ToString("MM");
            txtBirthdate_Day.Text = birthDate.ToString("dd");

            var passportExpiryDate = DateTime.Now.AddYears(2);
            txtPassportExpiryDate_Year.Text = passportExpiryDate.ToString("yyyy");
            txtPassportExpiryDate_Month.Text = passportExpiryDate.ToString("MM");
            txtPassportExpiryDate_Day.Text = passportExpiryDate.ToString("dd");

            txtPayable.Text = payable.ToString("N2");

            if (isInternationalRoute)
            {
                cmbDocType.SelectedIndex = 0;
            }
            else
            {
                cmbDocType.SelectedIndex = 1;
            }

            txtFirstname.Text = FirstNameGenerator.Get();
            txtLastname.Text = LastNameGenerator.Get();
        }

        public BookRequestModel.Passenger GetPassengerInfo()
        {
            return new BookRequestModel.Passenger
            {
                Title = cmbTitle.SelectedItem.ToString(),
                FirstName = txtFirstname.Text,
                LastName = txtLastname.Text,
                BirthDate = $"{txtBirthdate_Year.Text}-{txtBirthdate_Month.Text}-{txtBirthdate_Day.Text}",
                IranianNationalId = GetIranianNationalId(),
                IraqiNationalId = GetIraqiNationalId(),
                Passport = GetPassport(),
            };
        }

        private string GetIranianNationalId()
        {
            if (cmbDocType.SelectedItem.ToString() != "IranianNationalId")
            {
                return null;
            }

            return txtDocNumber.Text;
        }

        private string GetIraqiNationalId()
        {
            if (cmbDocType.SelectedItem.ToString() != "IraqiNationalId")
            {
                return null;
            }

            return txtDocNumber.Text;
        }

        private BookRequestModel.TravelDocument.Passport GetPassport()
        {
            if (cmbDocType.SelectedItem.ToString() != "Passport")
            {
                return null;
            }

            return new BookRequestModel.TravelDocument.Passport
            {
                Number = txtDocNumber.Text,
                ExpiryDate = $"{txtPassportExpiryDate_Year.Text}-{txtPassportExpiryDate_Month.Text}-{txtPassportExpiryDate_Day.Text}",
                NationalityCountryCode = txtPassportNationalityCountryCode.Text,
                PlaceOfIssueCountryCode = txtPassportPlaceOfIssueCountryCode.Text,
            };
        }

        private void cmbDocType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (cmbDocType.SelectedIndex == 0)
            {
                pnlPassportInfo.Visible = true;
                txtDocNumber.Text = PassportNumberGenerator.Get();
            }
            else if (cmbDocType.SelectedIndex == 1)
            {
                pnlPassportInfo.Visible = false;
                txtDocNumber.Text = IranianNationalIdGenerator.Get();
            }
            else if (cmbDocType.SelectedIndex == 2)
            {
                pnlPassportInfo.Visible = false;
                txtDocNumber.Text = IraqiNationalIdGenerator.Get();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            _passengersPanel.Controls.Remove(this);
            _calculateTotalPayableFunction.Invoke();
        }
    }
}
