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

namespace SepehrFlightApiSampleClient.Booking.WinForms.Book
{
    public class ApiResponseModelToRepositoryModelConvertor
    {
        public static BookedPnr Convert(
            ApiModel.Response response,
            string yourLocalInventoryPnr)
        {
            return new BookedPnr
            {
                Pnr = response.LocalPnr,
                YourLocalInventoryPnr = yourLocalInventoryPnr,
                PassengerList = response.PassengerList
                    .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.LocalTicketNumber,
                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.LocalTicketNumber,
                    FlightNumber = apiModelPassenger.ReturningSegment.FlightNumber,
                    FlightDate = apiModelPassenger.ReturningSegment.FlightDate,
                    OriginIataCode = apiModelPassenger.ReturningSegment.OriginIataCode,
                    DestinationIataCode = apiModelPassenger.ReturningSegment.DestinationIataCode
                });
            }

            return result;
        }
    }
}
