using SepehrFlightApiSampleClient.Availability.WinForms.SearchByRouteAndDate.ViewModels;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book.BookCommand.Factory
{
    public class FactoryBasedOnSearchByRouteAndDateResult
    {
        public static Command Create(
            Flight selectedDepartureFlight,
            FlightClass selectedDepartureFlightClass,
            int? departureLockId,
            Flight selectedReturningFlight,
            FlightClass selectedReturningFlightClass,
            int? returningLockId)
        {
            return new Command
            {
                Departure = Convert(
                    selectedDepartureFlight, 
                    selectedDepartureFlightClass,
                    departureLockId),
                Returning = Convert(
                    selectedReturningFlight, 
                    selectedReturningFlightClass,
                    returningLockId),
            };
        }

        private static FlightSegment Convert(
            Flight selectedFlight,
            FlightClass selectedFlightClass,
            int? lockId)
        {
            if (selectedFlight == null) return null;

            return new FlightSegment
            {
                FlightNumber = selectedFlight.FlightNumber,
                DepartureDateTime = selectedFlight.DepartureDateTime,
                OriginIataCode = selectedFlight.Origin,
                DestinationIataCode = selectedFlight.Destination,
                FareName = selectedFlightClass.FareName,
                AdultPayable = selectedFlightClass.Adult_Fare.Payable,
                ChildPayable = selectedFlightClass.Child_Fare.Payable,
                InfantPayable = selectedFlightClass.Infant_Fare.Payable,
                LockId = lockId,
            };
        }
    }
}
