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

namespace SepehrTourApiSampleClient.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();
        }
    }
}
