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

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

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

            dgvPendingRequests.AutoGenerateColumns = true;
        }

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

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

        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.Tour.RetrieveBooking.V1.GetStatus.RequestModel.Request
                {
                    Credential = new Sepehr.ApiGateway.Partners.Contracts.Tour.RetrieveBooking.V1.GetStatus.RequestModel.Credential
                    {
                        Username = _mainForm.txtUsername.Text,
                        Password = _mainForm.txtPassword.Text,
                    },
                    YourLocalInventoryPnr = currentItem.YourLocalInventoryPnr,
                });

            var statusResponseForm = new RetrieveBooking.WinForms.StatusResponseForm();
            statusResponseForm.txtResponse.Text = response.SerializeToJsonWithFormatIndented();
            statusResponseForm.Show();
        }
    }
}
