using System;
using System.Windows.Forms;
using SepehrTourApiSampleClient.Booking.Book.WinForms.FakeData;
using BookRequestModel = Sepehr.ApiGateway.Partners.Contracts.Tour.Booking.V3.Book.RequestModel;

namespace SepehrTourApiSampleClient.Booking.Book.WinForms.PassengerUserControls
{
    public partial class AdultUserControl : UserControl
    {
        public AdultUserControl(bool isInternationalRoute)
        {
            InitializeComponent();

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

            var birthDate = DateTime.Now.AddYears(-30);
            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");

            if (isInternationalRoute)
            {
                cmbDocType.SelectedIndex = 0;
                txtDocNumber.Text = PassportNumberGenerator.Get();
            }
            else
            {
                cmbDocType.SelectedIndex = 1;
                txtDocNumber.Text = CodeMelliGenerator.Get();
            }
            cmbDocType.SelectedIndex = 1;
            txtDocNumber.Text = CodeMelliGenerator.Get();

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

        public BookRequestModel.Guest GetGuestInfo()
        {
            return new BookRequestModel.Guest
            {
                Title = cmbTitle.SelectedItem.ToString(),
                FirstName = txtFirstname.Text,
                LastName = txtLastname.Text,
                BirthDate = $"{txtBirthdate_Year.Text}-{txtBirthdate_Month.Text}-{txtBirthdate_Day.Text}",
                IranianNationalId = cmbDocType.SelectedItem.ToString() == "IranianCartMelli" ? 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;
            }
        }
    }
}
