using SepehrFlightApiSampleClient.Availability.WinForms.DateRange.ViewModels;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book.BookCommand.Factory
{
    public class FactoryBasedOnDateRangeResult
    {
        public static Command Create(
            Flight selectedDepartureFlight,
            FlightClass selectedDepartureFlightClass,
            Flight selectedReturningFlight,
            FlightClass selectedReturningFlightClass)
        {
            var isJourneyRoundtrip = GetIsJourneyRoundtrip(
                selectedDepartureFlight, 
                selectedReturningFlight);

            var journeyOriginIataCode = selectedDepartureFlight.Origin;

            var command = new Command
            {
                Departure = Convert(
                    selectedDepartureFlight, 
                    selectedDepartureFlightClass, 
                    isJourneyRoundtrip,
                    journeyOriginIataCode),
                Returning = Convert(
                    selectedReturningFlight, 
                    selectedReturningFlightClass, 
                    isJourneyRoundtrip,
                    journeyOriginIataCode),
            };

            return command;
        }

        private static bool GetIsJourneyRoundtrip(
            Flight selectedDepartureFlight, 
            Flight selectedReturningFlight)
        {
            return selectedDepartureFlight != null && selectedReturningFlight != null;
        }

        private static FlightSegment Convert(
            Flight selectedFlight,
            FlightClass selectedFlightClass,
            bool isJourneyRoundtrip,
            string journeyOriginIataCode)
        {
            if (selectedFlight == null) return null;

            var commandFlightSegment = new FlightSegment
            {
                FlightNumber = selectedFlight.FlightNumber,
                DepartureDateTime = selectedFlight.DepartureDateTime,
                OriginIataCode = selectedFlight.Origin,
                DestinationIataCode = selectedFlight.Destination,
                FareName = selectedFlightClass.FareName,
            };

            if (isJourneyRoundtrip)
            {
                if (journeyOriginIataCode == selectedFlight.Origin)
                {
                    commandFlightSegment.AdultPayable = selectedFlightClass.RoundtripFare_SameOrigin.Adult_Fare.Payable;
                    commandFlightSegment.ChildPayable = selectedFlightClass.RoundtripFare_SameOrigin.Child_Fare.Payable;
                    commandFlightSegment.InfantPayable = selectedFlightClass.RoundtripFare_SameOrigin.Infant_Fare.Payable;
                }
                else
                {
                    commandFlightSegment.AdultPayable = selectedFlightClass.RoundtripFare_OppositeOrigin.Adult_Fare.Payable;
                    commandFlightSegment.ChildPayable = selectedFlightClass.RoundtripFare_OppositeOrigin.Child_Fare.Payable;
                    commandFlightSegment.InfantPayable = selectedFlightClass.RoundtripFare_OppositeOrigin.Infant_Fare.Payable;
                }
            }
            else
            {
                commandFlightSegment.AdultPayable = selectedFlightClass.OnewayFare.Adult_Fare.Payable;
                commandFlightSegment.ChildPayable = selectedFlightClass.OnewayFare.Child_Fare.Payable;
                commandFlightSegment.InfantPayable = selectedFlightClass.OnewayFare.Infant_Fare.Payable;
            }

            return commandFlightSegment;
        }
    }
}
