using System;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.FakeData;
using BookRequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V11.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;
                txtDocNumber.Text = PassportNumberGenerator.Get();
            }
            else
            {
                cmbDocType.SelectedIndex = 1;
                txtDocNumber.Text = CodeMelliGenerator.Get();
            }

            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(),
                Passport = GetPassport(),
            };
        }

        private string GetIranianNationalId()
        {
            if (cmbDocType.SelectedItem.ToString() != "IranianCartMelli")
            {
                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;
            }
            else
            {
                pnlPassportInfo.Visible = false;
            }
        }

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