using System;
using System.Globalization;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Booking.WebApi;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;
using LockRequestModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V11.Lock.RequestModel;
using LockResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V11.Lock.ResponseModel;

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;
            }

            CalculateTotalPayable();
        }


        private LockResponseModel.Response _lockResponse;
        private void btn_AirlockV6_Lock_Click(object sender, EventArgs e)
        {
            txtLockResult.Clear();

            var request = new LockRequestModel.Request
            {
                Username = _mainForm.txtUsername.Text,
                Password = _mainForm.txtPassword.Text,
                AdultCount = Convert.ToByte(nudLockAdultCount.Value),
                ChildCount = Convert.ToByte(nudLockChildCount.Value),
                InfantCount = Convert.ToByte(nudLockInfantCount.Value),
                DepartureSegment = new LockRequestModel.FlightSegment
                {
                    FlightNumber = txtDepartureFlightNumber.Text,
                    FlightDate = dtlDepartureFlightDate.Value.ToString("yyyy-MM-dd HH:mm"),
                    OriginIataCode = txtDepartureOrigin.Text,
                    DestinationIataCode = txtDepartureDestination.Text,
                    FareName = txtDepartureFareName.Text,
                },
                TotalPayable = decimal.Parse(txtTotalPayable.Text, CultureInfo.InvariantCulture),
            };

            if (_command.Returning != null)
            {
                request.ReturningSegment =
                    new LockRequestModel.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.SerializeToJsonWithFormatIndented());

            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.V11.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.V11.ReleaseLock.RequestModel.Request
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text,
                    LockId = GetReturningLockId().Value
                };

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


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

        private void CalculateTotalPayable()
        {
            var adultCount = Convert.ToByte(nudLockAdultCount.Value);
            var childCount = Convert.ToByte(nudLockChildCount.Value);
            var infantCount = Convert.ToByte(nudLockInfantCount.Value);
            decimal totalPayable;

            if (
                _command.Departure != null
                &&
                _command.Returning != null)
            {
                totalPayable =
                    _command.Departure.AdultPayable * adultCount
                    +
                    _command.Departure.ChildPayable * childCount
                    +
                    _command.Departure.InfantPayable * infantCount
                    +
                    _command.Returning.AdultPayable * adultCount
                    +
                    _command.Returning.ChildPayable * childCount
                    +
                    _command.Returning.InfantPayable * infantCount;
            }
            else
            {
                totalPayable =
                    _command.Departure.AdultPayable * adultCount
                    +
                    _command.Departure.ChildPayable * childCount
                    +
                    _command.Departure.InfantPayable * infantCount;
            }

            txtTotalPayable.Text = totalPayable.FormattedNumber();
        }

        private void nudLockAdultCount_ValueChanged(object sender, EventArgs e)
        {
            CalculateTotalPayable();
        }

        private void nudLockChildCount_ValueChanged(object sender, EventArgs e)
        {
            CalculateTotalPayable();
        }

        private void nudLockInfantCount_ValueChanged(object sender, EventArgs e)
        {
            CalculateTotalPayable();
        }
    }
}
