using System;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Refund.InMemoryDatabase.ConfirmedRequests.Model;
using SepehrFlightApiSampleClient.Refund.InMemoryDatabase.ConfirmedRequests.Repository;
using SepehrFlightApiSampleClient.Refund.InMemoryDatabase.PendingRequests.Model;
using SepehrFlightApiSampleClient.Refund.InMemoryDatabase.PendingRequests.Repository;
using SepehrFlightApiSampleClient.Refund.InMemoryDatabase.RejectedRequests.Model;
using SepehrFlightApiSampleClient.Refund.InMemoryDatabase.RejectedRequests.Repository;
using SepehrFlightApiSampleClient.Refund.WebApi;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.GetStatus.RequestModel;

namespace SepehrFlightApiSampleClient.Refund.WinForms
{
    public partial class PendingRequestForm : Form
    {
        private readonly MainForm _mainForm;

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

            dgvPendingRequests.AutoGenerateColumns = true;
        }

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

        public void DataBindGrid()
        {
            bnsPendingRequests = new BindingSource();
            bnsPendingRequests.DataSource = PendingRequestRepository.GetAll();
            dgvPendingRequests.DataSource = bnsPendingRequests;

            bnsRejectedRequests = new BindingSource();
            bnsRejectedRequests.DataSource = RejectedRequestRepository.GetAll();
            dgvRejectedRequests.DataSource = bnsRejectedRequests;

            bnsConfirmedRequests = new BindingSource();
            bnsConfirmedRequests.DataSource = ConfirmedRequestRepository.GetAll();
            dgvConfirmedRequests.DataSource = bnsConfirmedRequests;
        }

        private void btnGetStatus_Click(object sender, EventArgs e)
        {
            if (bnsPendingRequests.Current == null) return;
            
            var pendingRequest = (PendingRequest) bnsPendingRequests.Current;


            var request = new RequestModel.Request
            {
                Credential = new RequestModel.Credential
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text
                },
                TrackingCode = pendingRequest.SupplierTrackingCode
            };

            var response = GetStatusV3Api.Post(
                _mainForm.txtUrl.Text,
                request);

            if (response.Status == "Pending")
            {
                MessageBox.Show("Request is still pending. Please check later.");
            }
            else if (response.Status == "Confirmed")
            {
                PendingRequestRepository.Remove(pendingRequest.SupplierTrackingCode);
                ConfirmedRequestRepository.Add(new ConfirmedRequest
                {
                    Pnr = pendingRequest.Pnr,
                    SupplierTrackingCode = pendingRequest.SupplierTrackingCode,
                    Remarks = response.SupplierRemarks
                });

                DataBindGrid();

                MessageBox.Show($"Request is confirmed :){Environment.NewLine}Supplier remarks: {response.SupplierRemarks}");
            }
            else if (response.Status == "Rejected")
            {
                PendingRequestRepository.Remove(pendingRequest.SupplierTrackingCode);
                RejectedRequestRepository.Add(new RejectedRequest
                {
                    Pnr = pendingRequest.Pnr,
                    SupplierTrackingCode = pendingRequest.SupplierTrackingCode,
                    Remarks = response.SupplierRemarks
                });

                DataBindGrid();

                MessageBox.Show($"Request is rejected :({Environment.NewLine}Supplier remarks: {response.SupplierRemarks}");
            }
        }
    }
}
