using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.RetrieveBooking.WebApi;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V3.GetHistory.RequestModel;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V3.GetHistory.ResponseModel;
// ReSharper disable InconsistentNaming

namespace SepehrFlightApiSampleClient.ChangePassengerInfo.WinForms
{
    public partial class RetrieveBookingForm : Form
    {
        private readonly MainForm _mainform;
        private ResponseModel.Response _retrieveBookingResponse;

        public RetrieveBookingForm(MainForm mainform)
        {
            _mainform = mainform;
            InitializeComponent();

            dgvFlights.AutoGenerateColumns = true;
        }


        public List<PassengerGridViewModel> GetPassengerList()
        {
            var selectedPassengers = new List<PassengerGridViewModel>();
            foreach (DataGridViewRow row in dgvFlights.Rows)
            {
                selectedPassengers.Add((PassengerGridViewModel)row.DataBoundItem);
            }

            return selectedPassengers;
        }

        private void btnRetrieveBooking_Click(object sender, EventArgs e)
        {
            var request = new RequestModel.Request
            {
                Username = _mainform.txtUsername.Text,
                Password = _mainform.txtPassword.Text,
                Pnr = txtPnr.Text,
            };

            _retrieveBookingResponse = GetHistoryApi.Post(
                _mainform.txtUrl.Text, 
                request);

            bnsFlights.DataSource = ConvertApiModelToViewModel(_retrieveBookingResponse);
        }

        private static List<PassengerGridViewModel> ConvertApiModelToViewModel(ResponseModel.Response response)
        {
            var result = new List<PassengerGridViewModel>();

            response.LastState.PassengerList.ForEach(p =>
            {
                result.Add(new PassengerGridViewModel
                {
                    Id = p.Id,
                    Gender = p.Gender,
                    FirstName = p.FirstName,
                    LastName = p.LastName,
                    Passport_Number = p.Passport?.Number,
                    Passport_ExpiryDate = p.Passport?.ExpiryDate,
                    Passport_PlaceOfIssueCountryCode = p.Passport?.PlaceOfIssueCountry,
                });
            });

            return result;
        }

        private void btnNextStep_Click(object sender, EventArgs e)
        {
            _mainform.tabControlChangePassengerInfo.SelectedTab = _mainform.tbgChangePassengerInfo_GetPenalty;
        }
    }

    public class PassengerGridViewModel
    {
        public string Id { get; set; }

        public string Gender { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Passport_Number { get; set; }

        public string Passport_ExpiryDate { get; set; }

        public string Passport_PlaceOfIssueCountryCode { get; set; }
    }
}
