using System;
using System.ComponentModel;
// ReSharper disable InconsistentNaming

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

namespace SepehrFlightApiSampleClient.Availability.WinForms.DateRange.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)]//I used thid attribute to hide this column on grid
        public FareGroup OnewayFare { get; set; }

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

        [Browsable(false)]
        public FareGroup RoundtripFare_FromDestination { 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.Charter.ResponseModel.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));

            this.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)
            {
                this.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)
            {
                this.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);
        }

        public FlightClass(ResponseModel.Webservice.ResponseModel.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));

            this.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)
            {
                this.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)
            {
                this.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.Charter.ResponseModel.Fare apiFare)
        {
            return new Fare
            {
                BaseFare = apiFare.BaseFare,
                TotalTax = apiFare.Tax,
                TotalFare = apiFare.TotalFare,
                Commission = apiFare.Commision,
                Markup = 0,//charter flights dont have markup
                Payable = apiFare.Payable,
            };
        }

        private Fare ConvertFareApiModelToViewModel(ResponseModel.Webservice.ResponseModel.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.Charter.ResponseModel.FreeBaggage apiFreeBaggage)
        {
            return new FreeBaggage
            {
                CheckedBaggageQuantity = apiFreeBaggage.CheckedBaggageQuantity,
                CheckedBaggageTotalWeight = apiFreeBaggage.CheckedBaggageTotalWeight,
                HandBaggageQuantity = apiFreeBaggage.HandBaggageQuantity,
                HandBaggageTotalWeight = apiFreeBaggage.HandBaggageTotalWeight,
            };
        }

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