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

namespace SepehrHotelApiSampleClient.Booking.Book.WinForms
{
    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 statusResponseForm = new StatusResponseForm();
            statusResponseForm.txtResponse.Text = response.SerializeToJsonWithFormatIndented();
            statusResponseForm.Show();
        }
    }
}
