// ReSharper disable InconsistentNaming

using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Availability.V12.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; }

        [System.ComponentModel.Browsable(false)]//I used thid attribute to hide this column on grid
        public FareGroup OnewayFare { get; set; }

        [System.ComponentModel.Browsable(false)]
        public FareGroup RoundtripFare_SameOrigin { get; set; }

        [System.ComponentModel.Browsable(false)]
        public FareGroup RoundtripFare_OppositeOrigin { get; set; }

        [System.ComponentModel.Browsable(false)]
        public string CancelationPolicy { get; set; }

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

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

        [System.ComponentModel.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;
            CancelationPolicy = flightClass.CancelationPolicy;

            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_SameOrigin != null)
            {
                this.RoundtripFare_SameOrigin = new FareGroup
                {
                    Adult_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_SameOrigin.Adult_Fare),
                    Child_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_SameOrigin.Child_Fare),
                    Infant_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_SameOrigin.Infant_Fare),
                };
            }

            if (flightClass.RoundtripFare_OppositeOrigin != null)
            {
                this.RoundtripFare_OppositeOrigin = new FareGroup
                {
                    Adult_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_OppositeOrigin.Adult_Fare),
                    Child_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_OppositeOrigin.Child_Fare),
                    Infant_Fare = ConvertFareApiModelToViewModel(flightClass.RoundtripFare_OppositeOrigin.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;
            CancelationPolicy = flightClass.CancelationPolicy;

            this.OnewayFare = new FareGroup
            {
                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);
        }

        private Fare ConvertFareApiModelToViewModel(ResponseModel.Charter.ResponseModel.Fare apiFare)
        {
            return new Fare
            {
                BaseFare = apiFare.BaseFare,
                Tax = apiFare.Tax,
                TotalFare = apiFare.TotalFare,
                Commision = apiFare.Commision,
                Payable = apiFare.Payable,
            };
        }

        private Fare ConvertFareApiModelToViewModel(ResponseModel.Webservice.ResponseModel.Fare apiFare)
        {
            return new Fare
            {
                BaseFare = apiFare.BaseFare,
                Tax = apiFare.Tax,
                TotalFare = apiFare.TotalFare,
                Commision = apiFare.Commision,
                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,
            };
        }
    }
}
