using System;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.InMemoryDatabase.BookedPnrs.Model;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.InMemoryDatabase.BookedPnrs.Repository;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;
using SepehrFlightApiSampleClient.Refund.WinForms;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book
{
    public partial class BookedPnrForm : Form
    {
        private readonly MainForm _mainForm;
        private readonly RetrieveBookingForm _retrieveBookingForm;

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

            dgvPnrList.AutoGenerateColumns = true;
        }

        public void DataBindGrid()
        {
            bnsBookedPnrs = new BindingSource();
            bnsBookedPnrs.DataSource = BookedPnrRepository.GetAll();
            dgvPnrList.DataSource = bnsBookedPnrs;
        }

        private void BookedPnrForm_Load(object sender, EventArgs e)
        {
            DataBindGrid();
        }

        private void btnRefund_Click(object sender, EventArgs e)
        {
            var currentItem = (BookedPnr)bnsBookedPnrs.Current;

            if (currentItem == null) return;

            _retrieveBookingForm.txtPnr.Text = currentItem.Pnr;
            _mainForm.tabControlMain.SelectedTab = _mainForm.tbgRefund;
            _mainForm.tabControlRefund.SelectedTab = _mainForm.tbgRefundRetrievePnr;
        }

        private void btnGetStatus_Click(object sender, EventArgs e)
        {
            var currentItem = (BookedPnr)bnsBookedPnrs.Current;

            if (currentItem == null) return;

            var response = RetrieveBooking.WebApi.GetStatusApi.Post(
                _mainForm.txtUrl.Text,
                new Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V1.GetStatus.RequestModel.Request
                {
                    Credential = new Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V1.GetStatus.RequestModel.Credential
                    {
                        Username = _mainForm.txtUsername.Text,
                        Password = _mainForm.txtPassword.Text,
                    },
                    YourLocalInventoryPnr = currentItem.YourLocalInventoryPnr,
                });

            var showResponseForm = new RetrieveBooking.WinForms.ShowResponseForm();
            showResponseForm.txtResponse.Text = response.SerializeToJsonWithFormatIndented();
            showResponseForm.Show();
        }

        private void btnGetHistory_Click(object sender, EventArgs e)
        {
            var currentItem = (BookedPnr)bnsBookedPnrs.Current;

            if (currentItem == null) return;

            var response = RetrieveBooking.WebApi.GetHistoryApi.Post(
                _mainForm.txtUrl.Text,
                new Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V1.GetHistory.RequestModel.Request
                {
                    Credential = new Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V1.GetHistory.RequestModel.Credential
                    {
                        Username = _mainForm.txtUsername.Text,
                        Password = _mainForm.txtPassword.Text,
                    },
                    Pnr = currentItem.Pnr,
                });

            var showResponseForm = new RetrieveBooking.WinForms.ShowResponseForm();
            showResponseForm.txtResponse.Text = response.SerializeToJsonWithFormatIndented();
            showResponseForm.Show();
        }
    }
}
