using System;
using System.Windows.Forms;
using Sepehr.ApiGateway.Partners.Contracts.Flight.RetrieveBooking.V1.GetStatus.RequestModel;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.InMemoryDatabase.FailedRequests.Model;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.InMemoryDatabase.FailedRequests.Repository;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;
using SepehrFlightApiSampleClient.RetrieveBooking.WebApi;
using SepehrFlightApiSampleClient.RetrieveBooking.WinForms;

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

        public FailedRequestsForm(MainForm mainForm)
        {
            _mainForm = mainForm;
            InitializeComponent();

            dgvPnrList.AutoGenerateColumns = true;
        }

        public void DataBindGrid()
        {
            bnsFailedRequests = new BindingSource();
            bnsFailedRequests.DataSource = FailedRequestRepository.GetAll();
            dgvPnrList.DataSource = bnsFailedRequests;
        }

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

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

            if (currentItem == null) return;

            var response = GetStatusApi.Post(
                _mainForm.txtUrl.Text,
                new Request
                {
                    Credential = new Credential
                    {
                        Username = _mainForm.txtUsername.Text,
                        Password = _mainForm.txtPassword.Text,
                    },
                    YourLocalInventoryPnr = currentItem.Pnr,
                });

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