using System;
using System.Linq;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Refund.WebApi;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.GetPenalty.RequestModel;

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

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

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

            var request = new RequestModel.Request
            {
                Credential = new RequestModel.Credential
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text
                },
                Pnr = _retrieveBookingForm.txtPnr.Text,
                TicketNumberList = selectedSegments
                    .Select(c => c.TicketNumber)
                    .ToList()
            };

            var response = GetPenaltyV3Api.Post(_mainForm.txtUrl.Text, request);

            dgvPenalty.DataSource = response.RecentlyRequestedPassengers;
            lblSumOfPenalyAmountThatPassengerShouldPay.Text = response
                .SumOfPenaltyAmountThatPassengerShouldPay
                .ToString("N2");
        }

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

            var form = new DoRefundForm(
                mainForm: _mainForm,
                retrieveBookingForm: _retrieveBookingForm,
                sumOfPenalyAmountThatPassengerShouldPay: Convert.ToDecimal(lblSumOfPenalyAmountThatPassengerShouldPay.Text),
                refundReason: Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.DoRefund.RequestModel.RefundReason.PassengerPersonalReason);
            form.TopLevel = false;
            form.Parent = _mainForm.tbgDoRefund;
            form.Show();

            _mainForm.tabControlRefund.SelectedTab = _mainForm.tbgDoRefund;
        }
    }
}
