using System;
using System.Linq;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;
using SepehrFlightApiSampleClient.ChangePassengerInfo.WebApi;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.ChangePassengerInfo.V1.GetPenalty.RequestModel;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.ChangePassengerInfo.V1.GetPenalty.ResponseModel;
using DoChangeRequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.ChangePassengerInfo.V1.DoChange.RequestModel;

namespace SepehrFlightApiSampleClient.ChangePassengerInfo.WinForms
{
    public partial class GetPenaltyForm : Form
    {
        private readonly MainForm _mainForm;
        private readonly RetrieveBookingForm _retrieveBookingForm;
        private ResponseModel.Response _getPenaltyResponse;
        private BindingSource _bnsPassengers = new BindingSource();

        public GetPenaltyForm(
            MainForm mainForm,
            RetrieveBookingForm retrieveBookingForm)
        {
            _mainForm = mainForm;
            _retrieveBookingForm = retrieveBookingForm;
            InitializeComponent();
        }

        private void btnGetPenalty_Click(object sender, EventArgs e)
        {
            var passengerList = _retrieveBookingForm.GetPassengerList();

            var request = new RequestModel.Request
            {
                Username = _mainForm.txtUsername.Text,
                Password = _mainForm.txtPassword.Text,
                Pnr = _retrieveBookingForm.txtPnr.Text,
                PassengerList = passengerList
                    .ConvertAll(p => new RequestModel.Passenger
                    {
                        Id = p.Id,
                        PassengerNewData = new RequestModel.PassengerNewData
                        {
                            GenderType = p.Gender,
                            FirstName = p.FirstName,
                            LastName = p.LastName,
                            Passport = GetPassport(p),
                        },
                    }),
                Culture = "fa-IR",
            };

            _getPenaltyResponse = GetPenaltyApi.Post(_mainForm.txtUrl.Text, request);

            _bnsPassengers = new BindingSource();
            _bnsPassengers.DataSource = _getPenaltyResponse.PassengerList;

            dgvPenalty.DataSource = _bnsPassengers;
            lblSumOfPenalyAmountThatPassengerShouldPay.Text = _getPenaltyResponse
                .PassengerList.Sum(x => x.Penalty)
                .ToString("N2");
        }


        private void btnNextStep_Click(object sender, EventArgs e)
        {
            _mainForm.tbgChangePassengerInfo_DoChange.Controls.Clear();

            var form = new DoChangeForm(
                mainForm: _mainForm,
                request: new DoChangeRequestModel.Request
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text,
                    Pnr = _retrieveBookingForm.txtPnr.Text,
                    PassengerList = _getPenaltyResponse
                        .PassengerList
                        .ConvertAll(x => new DoChangeRequestModel.Passenger
                        {
                            Id = x.Id,
                            PassengerNewData = new DoChangeRequestModel.PassengerNewData
                            {
                                GenderType = x.NewData.GenderType,
                                FirstName = x.NewData.FirstName,
                                LastName = x.NewData.LastName,
                                Passport = GetPassport(x.NewData.Passport),
                            },
                        }),
                    TotalPenalty = _getPenaltyResponse
                        .PassengerList
                        .Sum(x => x.Penalty),
                    Culture = "fa-IR"
                });
            form.TopLevel = false;
            form.Parent = _mainForm.tbgChangePassengerInfo_DoChange;
            form.Show();

            _mainForm.tabControlChangePassengerInfo.SelectedTab = _mainForm.tbgChangePassengerInfo_DoChange;
        }

        private DoChangeRequestModel.TravelDocument.Passport GetPassport(ResponseModel.TravelDocument.Passport passport)
        {
            if (passport == null)
            {
                return null;
            }

            return new DoChangeRequestModel.TravelDocument.Passport
            {
                Number = passport.Number,
                ExpiryDate = passport.ExpiryDate,
                PlaceOfIssueCountryCode = passport.PlaceOfIssueCountryCode,
            };
        }

        private RequestModel.TravelDocument.Passport GetPassport(PassengerGridViewModel passenger)
        {
            if (string.IsNullOrWhiteSpace(passenger.Passport_Number))
            {
                return null;
            }

            return new RequestModel.TravelDocument.Passport
            {
                Number = passenger.Passport_Number,
                ExpiryDate = passenger.Passport_ExpiryDate,
                PlaceOfIssueCountryCode = passenger.Passport_PlaceOfIssueCountryCode,
            };
        }

        private void dgvPenalty_SelectionChanged(object sender, EventArgs e)
        {
            var currentPassenger = (ResponseModel.Passenger)_bnsPassengers.Current;

            txtOldData.Text = currentPassenger.OldData.SerializeToJsonWithFormatIndented();
            txtNewData.Text = currentPassenger.NewData.SerializeToJsonWithFormatIndented();
        }
    }
}
