// ReSharper disable InconsistentNaming

using System;
using System.ComponentModel;

using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Availability.V15.SearchByRouteAndDate.ResponseModel;

namespace SepehrFlightApiSampleClient.Availability.WinForms.SearchByRouteAndDate.ViewModels
{
    public class FlightClass
    {
        public int AvailableSeat { get; set; }

        public string CabinType { get; set; }

        public string BookingCode { get; set; }

        public string FareName { get; set; }

        [Browsable(false)]
        public CharterFare CharterFare { get; set; }

        [Browsable(false)]
        public WebserviceFare WebserviceFare { get; set; }

        [Browsable(false)]
        public string CancelationPolicyList { get; set; }

        [Browsable(false)]
        public FreeBaggage AdultFreeBaggage { get; set; }

        [Browsable(false)]
        public FreeBaggage ChildFreeBaggage { get; set; }

        [Browsable(false)]
        public FreeBaggage InfantFreeBaggage { get; set; }

        public FlightClass(ResponseModel.CharterFlight.FlightClass flightClass)
        {
            AvailableSeat = flightClass.AvailableSeat;
            CabinType = flightClass.CabinType;
            BookingCode = flightClass.BookingCode;
            FareName = flightClass.FareName;
            CancelationPolicyList = string.Join(
                separator: Environment.NewLine,
                values: flightClass
                    .CancelationPolicyList
                    .ConvertAll(x => x.Culture + Environment.NewLine + x.Text + Environment.NewLine + Environment.NewLine));

            CharterFare = new CharterFare
            {
                Adult_Fare = ConvertFareApiModelToViewModel(flightClass.AdultFare),
                Child_Fare = ConvertFareApiModelToViewModel(flightClass.ChildFare),
                Infant_Fare = ConvertFareApiModelToViewModel(flightClass.InfantFare),
            };

            AdultFreeBaggage = ConvertBaggageApiModelToViewModel(flightClass.AdultFreeBaggage);
            ChildFreeBaggage = ConvertBaggageApiModelToViewModel(flightClass.ChildFreeBaggage);
            InfantFreeBaggage = ConvertBaggageApiModelToViewModel(flightClass.InfantFreeBaggage);
        }

        public FlightClass(ResponseModel.WebserviceFlight.FlightClass flightClass)
        {
            AvailableSeat = flightClass.AvailableSeat;
            CabinType = flightClass.CabinType;
            BookingCode = flightClass.BookingCode;
            FareName = flightClass.FareName;
            CancelationPolicyList = string.Join(
                separator: Environment.NewLine,
                values: flightClass
                    .CancelationPolicyList
                    .ConvertAll(x => x.Culture + Environment.NewLine + x.Text + Environment.NewLine + Environment.NewLine));

            WebserviceFare = new WebserviceFare
            {
                OnewayFare = new FareGroup
                {
                    Adult_Fare = ConvertFareApiModelToViewModel(flightClass.OnewayFare.Adult_Fare),
                    Child_Fare = ConvertFareApiModelToViewModel(flightClass.OnewayFare.Child_Fare),
                    Infant_Fare = ConvertFareApiModelToViewModel(flightClass.OnewayFare.Infant_Fare),
                },
            };

            if (flightClass.RoundtripFare_FromOrigin != null)
            {
                WebserviceFare.RoundtripFare_FromOrigin = new FareGroup
                {
                    Adult_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_FromOrigin.Adult_Fare),
                    Child_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_FromOrigin.Child_Fare),
                    Infant_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_FromOrigin.Infant_Fare),
                };
            }

            if (flightClass.RoundtripFare_FromDestination != null)
            {
                WebserviceFare.RoundtripFare_FromDestination = new FareGroup
                {
                    Adult_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_FromDestination.Adult_Fare),
                    Child_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_FromDestination.Child_Fare),
                    Infant_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_FromDestination.Infant_Fare),
                };
            }


            AdultFreeBaggage = ConvertBaggageApiModelToViewModel(flightClass.AdultFreeBaggage);
            ChildFreeBaggage = ConvertBaggageApiModelToViewModel(flightClass.ChildFreeBaggage);
            InfantFreeBaggage = ConvertBaggageApiModelToViewModel(flightClass.InfantFreeBaggage);
        }

        private Fare ConvertFareApiModelToViewModel(ResponseModel.CharterFlight.Fare apiFare)
        {
            return new Fare
            {
                BaseFare = apiFare.BaseFare,
                TotalTax = apiFare.Tax,
                TotalFare = apiFare.TotalFare,
                Commission = apiFare.Commission,
                Markup = 0,//charter flights dont have markup
                Payable = apiFare.Payable,
            };
        }

        private Fare ConvertFareApiModelToViewModel(ResponseModel.WebserviceFlight.Fare apiFare)
        {
            return new Fare
            {
                BaseFare = apiFare.BaseFare,
                TotalTax = apiFare.TotalTax,
                TotalFare = apiFare.TotalFare,
                Commission = apiFare.Commission,
                Markup = apiFare.Markup,
                Payable = apiFare.Payable,
            };
        }


        private FreeBaggage ConvertBaggageApiModelToViewModel(ResponseModel.CharterFlight.FreeBaggage apiFreeBaggage)
        {
            return new FreeBaggage
            {
                CheckedBaggageQuantity = apiFreeBaggage.CheckedBaggageQuantity,
                CheckedBaggageTotalWeight = apiFreeBaggage.CheckedBaggageTotalWeight,
                HandBaggageQuantity = apiFreeBaggage.HandBaggageQuantity,
                HandBaggageTotalWeight = apiFreeBaggage.HandBaggageTotalWeight,
            };
        }

        private FreeBaggage ConvertBaggageApiModelToViewModel(ResponseModel.WebserviceFlight.FreeBaggage apiFreeBaggage)
        {
            return new FreeBaggage
            {
                CheckedBaggageQuantity = apiFreeBaggage.CheckedBaggageQuantity,
                CheckedBaggageTotalWeight = apiFreeBaggage.CheckedBaggageTotalWeight,
                HandBaggageQuantity = apiFreeBaggage.HandBaggageQuantity,
                HandBaggageTotalWeight = apiFreeBaggage.HandBaggageTotalWeight,
            };
        }
    }
}
