using System;
using System.Collections.Generic;
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 List<PassengerForRefund> _passengerForRefundList;
        private readonly RequestModel.RefundReason _refundReason;

        public class PassengerForRefund
        {
            public string TicketNumber { get; set; }

            public decimal PenaltyAmountThatPassengerShouldPay { get; set; }
        }

        public DoRefundForm(
            MainForm mainForm,
            RetrieveBookingForm retrieveBookingForm,
            List<PassengerForRefund> passengerForRefundList,
            RequestModel.RefundReason refundReason)
        {
            _mainForm = mainForm;
            _retrieveBookingForm = retrieveBookingForm;
            _passengerForRefundList = passengerForRefundList;
            _refundReason = refundReason;
            InitializeComponent();

            lblSumOfPenalyAmountThatPassengerShouldPay.Text = passengerForRefundList
                    .Sum(p => p.PenaltyAmountThatPassengerShouldPay)
                .ToString("N2");

            lblRefundReason.Text = refundReason.ToString();

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

        private void btnRefund_Click(object sender, EventArgs e)
        {
            var request = new RequestModel.Request
            {
                Credential = new RequestModel.Credential
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text
                },
                Pnr = _retrieveBookingForm.txtPnr.Text,
                SegmentList = _passengerForRefundList.ConvertAll(p => new RequestModel.Segment
                {
                    TicketNumber = p.TicketNumber,
                    PenaltyAmountThatPassengerShouldPay = p.PenaltyAmountThatPassengerShouldPay,
                }),
                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);
        }
    }
}
