using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Refund.WebApi;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.RetrieveBooking.RequestModel;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.RetrieveBooking.ResponseModel;

namespace SepehrFlightApiSampleClient.Refund.WinForms
{
    public partial class RetrieveBookingForm : Form
    {
        private readonly MainForm _mainform;

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

            dgvFlights.AutoGenerateColumns = true;

            var chkSelectSegment = new DataGridViewCheckBoxColumn
            {
                HeaderText = "",
                Name = "chkSelectSegment"
            };
            dgvFlights.Columns.Add(chkSelectSegment);
        }


        public List<SegmentGridViewModel> GetSelectedSegments()
        {
            var selectedPassengers = new List<SegmentGridViewModel>();
            foreach (DataGridViewRow row in dgvFlights.Rows)
            {
                var chk = (DataGridViewCheckBoxCell)row.Cells["chkSelectSegment"];

                if (Convert.ToBoolean(chk.Value) == false) continue;

                selectedPassengers.Add((SegmentGridViewModel)row.DataBoundItem);
            }

            return selectedPassengers;
        }

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

            var response = RetrieveBookingV3Api.Post(_mainform.txtUrl.Text, request);

            bnsFlights.DataSource = ConvertApiModelToViewModel(response);

            SelectAllPassengersInGrid();
        }

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

            response.Passengers.ForEach(p =>
            {
                p.SegemntList.ForEach(s =>
                {
                    result.Add(new SegmentGridViewModel
                    {
                        FirstName = p.FirstName,
                        LastName = p.LastName,
                        TicketNumber = s.TicketNumber,
                        FlightNumber = s.FlightNumber,
                        FlightDate = s.FlightDate,
                        Origin = s.OriginIataCode,
                        Destination = s.DestinationIataCode
                    });
                });
            });

            return result;
        }

        private void SelectAllPassengersInGrid()
        {
            foreach (DataGridViewRow row in dgvFlights.Rows)
            {
                var chk = (DataGridViewCheckBoxCell)row.Cells["chkSelectSegment"];
                chk.Value = true;
            }
        }

        //TODO: uncomment
        private void bnsFlights_CurrentChanged(object sender, EventArgs e)
        {
            /*
            var currentItem = GetSelectedFlight();
            dgvPassengers.DataSource = currentItem?.Passengers;
            */
        }

        private void btnNextStep_Click(object sender, EventArgs e)
        {
            if (rdoPassengerPersonalReason.Checked)
            {
                _mainform.tabControlRefund.SelectedTab = _mainform.tbgRefundGetPenalty;
            }
            else if (rdoChangeFlightScheduleByAirline.Checked)
            {
                _mainform.tbgDoRefund.Controls.Clear();

                var form = new DoRefundForm(
                    mainForm: _mainform,
                    retrieveBookingForm: this,
                    sumOfPenalyAmountThatPassengerShouldPay: 0,
                    refundReason: Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.DoRefund.RequestModel.RefundReason.FlightScheduleChangedByAirline);
                form.TopLevel = false;
                form.Parent = _mainform.tbgDoRefund;
                form.Show();

                _mainform.tabControlRefund.SelectedTab = _mainform.tbgDoRefund;
            }
            else
            {
                MessageBox.Show($"Please selected refund reason");
            }
        }
    }

    public class SegmentGridViewModel
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public long TicketNumber { get; set; }

        public string FlightNumber { get; set; }

        public string FlightDate { get; set; }

        public string Origin { get; set; }

        public string Destination { get; set; }
    }
}
