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;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book
{
    public partial class BookedPnrForm : Form
    {
        private readonly MainForm _mainForm;
        private readonly Refund.WinForms.RetrieveBookingForm _refundRetrieveBookingForm;
        private readonly ChangePassengerInfo.WinForms.RetrieveBookingForm _changePassengerInfoRetrieveBookingForm;

        public BookedPnrForm(
            MainForm mainForm,
            Refund.WinForms.RetrieveBookingForm refundRetrieveBookingForm,
            ChangePassengerInfo.WinForms.RetrieveBookingForm changePassengerInfoRetrieveBookingForm)
        {
            _mainForm = mainForm;
            _refundRetrieveBookingForm = refundRetrieveBookingForm;
            _changePassengerInfoRetrieveBookingForm = changePassengerInfoRetrieveBookingForm;
            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;

            _refundRetrieveBookingForm.txtPnr.Text = currentItem.Pnr;
            _mainForm.tabControlMain.SelectedTab = _mainForm.tbgRefund;
            _mainForm.tabControlRefund.SelectedTab = _mainForm.tbgRefund_RetrievePnr;
        }

        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.V3.GetStatus.RequestModel.Request
                {
                    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.V3.GetHistory.RequestModel.Request
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text,
                    Pnr = currentItem.Pnr,
                });

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

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

            if (currentItem == null) return;

            _changePassengerInfoRetrieveBookingForm.txtPnr.Text = currentItem.Pnr;
            _mainForm.tabControlMain.SelectedTab = _mainForm.tbgChangePassengerInfo;
            _mainForm.tabControlChangePassengerInfo.SelectedTab = _mainForm.tbgChangePassengerInfo_RetrievePnr;
        }
    }
}
