using System;
using System.IO;
using System.Linq;
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.WebApi;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Refund.V3.DoRefund.RequestModel;


namespace SepehrFlightApiSampleClient.Refund.WinForms
{
    public partial class DoRefundForm : Form
    {
        private readonly MainForm _mainForm;
        private readonly RetrieveBookingForm _retrieveBookingForm;
        private readonly decimal _sumOfPenalyAmountThatPassengerShouldPay;
        private readonly RequestModel.RefundReason _refundReason;

        public DoRefundForm(
            MainForm mainForm,
            RetrieveBookingForm retrieveBookingForm,
            decimal sumOfPenalyAmountThatPassengerShouldPay,
            RequestModel.RefundReason refundReason)
        {
            _mainForm = mainForm;
            _retrieveBookingForm = retrieveBookingForm;
            _sumOfPenalyAmountThatPassengerShouldPay = sumOfPenalyAmountThatPassengerShouldPay;
            _refundReason = refundReason;
            InitializeComponent();

            lblSumOfPenalyAmountThatPassengerShouldPay.Text = sumOfPenalyAmountThatPassengerShouldPay.ToString("N2");
            lblRefundReason.Text = refundReason.ToString();

            if (refundReason != RequestModel.RefundReason.FlightScheduleChangedByAirline)
            {
                btnSelecteTicketFile.Visible = false;
            }
        }

        private void btnRefund_Click(object sender, EventArgs e)
        {
            var selectedSegments = _retrieveBookingForm.GetSelectedSegments();

            var request = new RequestModel.Request
            {
                Credential = new RequestModel.Credential
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text
                },
                Pnr = _retrieveBookingForm.txtPnr.Text,
                TicketNumberList = selectedSegments.Select(c => c.TicketNumber).ToList(),
                SumOfPenaltyAmountThatPassengerShouldPay = _sumOfPenalyAmountThatPassengerShouldPay,
                ETicketFileContent = _fileContent,
                ETicketFileName = _fileName,
                Reason = _refundReason
            };

            var trackingCode = DoRefundV3Api.Post(_mainForm.txtUrl.Text, request);

            if (trackingCode == null)
            {
                ConfirmedRequestRepository.Add(new ConfirmedRequest
                {
                    Pnr = _retrieveBookingForm.txtPnr.Text,
                });

                MessageBox.Show("Done :)");    
            }
            else
            {
                PendingRequestRepository.Add(new PendingRequest
                {
                    Pnr = _retrieveBookingForm.txtPnr.Text,
                    SupplierTrackingCode = trackingCode.Value,
                });

                MessageBox.Show("Refund request sent. You should wait until supplier either accept or reject the request.");    
            }
        }

        private byte[] _fileContent;
        private string _fileName;
        private void btnSelecteTicketFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;

            _fileContent = ReadFile(openFileDialog1.FileName);
            _fileName = Path.GetFileName(openFileDialog1.FileName);
        }

        private static byte[] ReadFile(string path)
        {
            return File.ReadAllBytes(path);
        }
    }
}
