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

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

        public AdultUserControl(
            decimal payable, 
            bool isInternationalRoute,
            Panel passengersPanel)
        {
            _passengersPanel = passengersPanel;
            InitializeComponent();

            cmbDocType.SelectedIndex = 0;
            cmbTitle.SelectedIndex = 0;
            dtpBirthdate.Value = DateTime.Now.AddYears(-30);
            dtpPassportExpireDate.Value = DateTime.Now.AddYears(2);

            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 = dtpBirthdate.Value.ToString("yyyy-MM-dd"),
                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 = dtpPassportExpireDate.Value.ToString("yyyy-MM-dd"),
                    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);
        }
    }
}
