using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Availability.V17.SearchByRouteAndDate.ResponseModel;

namespace SepehrFlightApiSampleClient.Availability.WinForms.SearchByRouteAndDate.ViewModels
{
    public class Itinerary
    {
        public int Row { get; set; }

        [Browsable(false)]//I used thid attribute to hide this column on grid
        public Flight DepartureFlight { get; set; }

        public string DepartureFlightNumber { get; set; }

        public string DepartureCabin { get; set; }

        public string DepartureFareName { get; set; }


        [Browsable(false)]//I used thid attribute to hide this column on grid
        public Flight ReturningFlight { get; set; }

        public string ReturningFlightNumber { get; set; }

        public string ReturningCabin { get; set; }

        public string ReturningFareName { get; set; }

        public Itinerary(
            ResponseModel.Itinerary itinerary,
            int rowIndex)
        {
            Row = rowIndex;

            //departure
            var departureFlight = itinerary
                .FlightSegmentList
                .First();

            DepartureFlight = new Flight(departureFlight);

            DepartureFlightNumber = departureFlight
                .FlightNumber;

            DepartureCabin = departureFlight
                .FlightClass
                .CabinType;

            DepartureFareName = departureFlight
                .FlightClass
                .FareName;

            //returning
            var returningFlight = itinerary
                .FlightSegmentList
                .Skip(1)
                .FirstOrDefault();

            if (returningFlight != null)
            {
                ReturningFlight = new Flight(returningFlight);

                ReturningFlightNumber = itinerary
                    .FlightSegmentList
                    .Skip(1)
                    .FirstOrDefault()?
                    .FlightNumber;

                ReturningCabin = itinerary
                    .FlightSegmentList
                    .Skip(1)
                    .FirstOrDefault()?
                    .FlightClass
                    .CabinType;

                ReturningFareName = itinerary
                    .FlightSegmentList
                    .Skip(1)
                    .FirstOrDefault()?
                    .FlightClass
                    .FareName;
            }
        }


        public Itinerary(
            ResponseModel.ClosedFlight closedFlight,
            int rowIndex)
        {
            Row = rowIndex;

            DepartureFlight = new Flight(closedFlight);

            DepartureFlightNumber = closedFlight.FlightNumber;
            DepartureCabin = @"Closed";
            DepartureFareName = @"Closed";
        }
    }
}
