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

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

        public InfantUserControl(
            decimal payable,
            bool isInternationalRoute,
            Panel passengersPanel)
        {
            _passengersPanel = passengersPanel;
            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}",
                IranianCartMelli = cmbDocType.SelectedItem.ToString() == "IranianCartMelli" ? new BookRequestModel.TravelDocument.IranianCartMelli
                {
                    CodeMelli = txtDocNumber.Text
                } : null,
                Passport = cmbDocType.SelectedItem.ToString() == "Passport" ? new BookRequestModel.TravelDocument.Passport
                {
                    Number = txtDocNumber.Text,
                    ExpiryDate = $"{txtPassportExpiryDate_Year.Text}-{txtPassportExpiryDate_Month.Text}-{txtPassportExpiryDate_Day.Text}",
                    NationalityCountryCode = txtPassportNationalityCountryCode.Text,
                    PlaceOfIssueCountryCode = txtPassportPlaceOfIssueCountryCode.Text,
                } : null
            };
        }

        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);
        }
    }
}
