using System.Collections.Generic;
using System.Linq;
using SepehrFlightApiSampleClient.Booking.WinForms.Book.InMemoryDatabase.BookedPnrs.Model;
using ApiModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Booking.V9.Book.ResponseModel;

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book
{
    public class ApiResponseModelToRepositoryModelConvertor
    {
        public static BookedPnr Convert(ApiModel.Response response)
        {
            return new BookedPnr
            {
                Pnr = response.Pnr,
                PassengerList = response.Passengers
                    .Select(p => ConvertPassenger(p))
                    .ToList()
            };
        }

        private static Passenger ConvertPassenger(ApiModel.Passenger apiModelPassenger)
        {
            var result = new Passenger
            {
                FirstName = apiModelPassenger.FirstName,
                LastName = apiModelPassenger.LastName,
                SegmentList = new List<Segment>()
            };

            result.SegmentList.Add(new Segment
            {
                TicketNumber = apiModelPassenger.DepartureSegment.TicketNumber,
                FlightNumber = apiModelPassenger.DepartureSegment.FlightNumber,
                FlightDate = apiModelPassenger.DepartureSegment.FlightDate,
                OriginIataCode = apiModelPassenger.DepartureSegment.OriginIataCode,
                DestinationIataCode = apiModelPassenger.DepartureSegment.DestinationIataCode
            });

            if (apiModelPassenger.ReturningSegment != null)
            {
                result.SegmentList.Add(new Segment
                {
                    TicketNumber = apiModelPassenger.ReturningSegment.TicketNumber,
                    FlightNumber = apiModelPassenger.ReturningSegment.FlightNumber,
                    FlightDate = apiModelPassenger.ReturningSegment.FlightDate,
                    OriginIataCode = apiModelPassenger.ReturningSegment.OriginIataCode,
                    DestinationIataCode = apiModelPassenger.ReturningSegment.DestinationIataCode
                });
            }

            return result;
        }
    }
}
