using System;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Booking.WebApi;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Lock
{
    public partial class LockForm : Form
    {
        private readonly MainForm _mainForm;
        private readonly LockCommand.Command _command;

        public LockForm(
            MainForm mainForm,
            LockCommand.Command command)
        {
            _mainForm = mainForm;
            _command = command;
            InitializeComponent();

            SyncSelectedFlight();
        }

        private void SyncSelectedFlight()
        {
            //departure flight
            txtDepartureFlightNumber.Text = _command.Departure.FlightNumber;
            dtlDepartureFlightDate.Value = _command.Departure.DepartureDateTime.ParseFlightDate();
            txtDepartureOrigin.Text = _command.Departure.OriginIataCode;
            txtDepartureDestination.Text = _command.Departure.DestinationIataCode;
            txtDepartureFareName.Text = _command.Departure.FareName;

            //returning flight
            if (_command.Returning != null)
            {
                txtReturningFlightNumber.Text = _command.Returning.FlightNumber;
                dtlReturningFlightDate.Value = _command.Returning.DepartureDateTime.ParseFlightDate();
                txtReturningOrigin.Text = _command.Returning.OriginIataCode;
                txtReturningDestination.Text = _command.Returning.DestinationIataCode;
                txtReturningFareName.Text = _command.Returning.FareName;
            }
            else
            {
                txtReturningFlightNumber.Visible = false;
                dtlReturningFlightDate.Visible = false;
                txtReturningOrigin.Visible = false;
                txtReturningDestination.Visible = false;
                txtReturningFareName.Visible = false;
            }
        }


        private Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.Lock.ResponseModel.Response _lockResponse;
        private void btn_AirlockV6_Lock_Click(object sender, EventArgs e)
        {
            txtLockResult.Clear();

            var request = new Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.Lock.RequestModel.Request
            {
                Username = _mainForm.txtUsername.Text,
                Password = _mainForm.txtPassword.Text,
                AdultCount = Convert.ToByte(nudLockAdultCount.Value),
                ChildCount = Convert.ToByte(nudLockChildCount.Value),
                DepartureSegment = new Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.Lock.RequestModel.FlightSegment
                {
                    FlightNumber = txtDepartureFlightNumber.Text,
                    FlightDate = dtlDepartureFlightDate.Value.ToString("yyyy-MM-dd HH:mm"),
                    OriginIataCode = txtDepartureOrigin.Text,
                    DestinationIataCode = txtDepartureDestination.Text,
                    FareName = txtDepartureFareName.Text,
                }
            };

            if (_command.Returning != null)
            {
                request.ReturningSegment =
                    new Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.Lock.RequestModel.FlightSegment
                    {
                        FlightNumber = txtReturningFlightNumber.Text,
                        FlightDate = dtlReturningFlightDate.Value.ToString("yyyy-MM-dd HH:mm"),
                        OriginIataCode = txtReturningOrigin.Text,
                        DestinationIataCode = txtReturningDestination.Text,
                        FareName = txtReturningFareName.Text,
                    };
            }

            _lockResponse = LockApi.Post(_mainForm.txtUrl.Text, request);

            txtLockResult.AppendText(_lockResponse.SerializeToJson());

            MessageBox.Show("Done :)");
        }

        public int? GetDepartureLockId()
        {
            return _lockResponse?.DepartureSegmentLockId;
        }

        public int? GetReturningLockId()
        {
            return _lockResponse?.ReturningSegmentLockId;
        }

        private void btnReleaseLock_Click(object sender, EventArgs e)
        {
            if (GetDepartureLockId() != null)
            {
                var request = new Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.ReleaseLock.RequestModel.Request
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text,
                    LockId = GetDepartureLockId().Value
                };

                ReleaseLockApi.Post(_mainForm.txtUrl.Text, request);
            }

            if (GetReturningLockId() != null)
            {
                var request = new Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.ReleaseLock.RequestModel.Request
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text,
                    LockId = GetReturningLockId().Value
                };

                ReleaseLockApi.Post(_mainForm.txtUrl.Text, request);
            }


            MessageBox.Show("Done :)");
        }
    }
}
