using System.Collections.Generic;
using System.Linq;
using System.Text;
using SepehrFlightApiSampleClient.Availability.WinForms.ViewModels;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Flight.Availability.V12.DateRange;

namespace SepehrFlightApiSampleClient.Availability.WinForms.DateRange.ViewModels
{
    public class Flight
    {
        public string FlightNumber { get; set; }

        public string Origin { get; set; }

        public string Destination { get; set; }

        public string DepartureDateTime { get; set; }

        public string ShamsiDepartureDateTime { get; set; }

        public string Airline { get; set; }

        public string Aircraft { get; set; }

        public string Type { get; set; }

        public bool Stop1 { get; set; }

        public bool Stop2 { get; set; }

        public string ArrivalDateTime { get; set; }

        public int Duration { get; set; }

        [System.ComponentModel.Browsable(false)]//I used thid attribute to hide this column on grid
        public StopGridViewModel Stop1ViewModel { get; set; }

        [System.ComponentModel.Browsable(false)]//I used thid attribute to hide this column on grid
        public StopGridViewModel Stop2ViewModel { get; set; }

        public string GetStopData()
        {
            var sb = new StringBuilder();

            if (Stop1)
            {
                sb.AppendLine("Stop 1:");
                sb.AppendLine($"Airport: {Stop1ViewModel.AirportIataCode}");
                sb.AppendLine($"Arrival time: {Stop1ViewModel.ArrivalDateTime}");
                sb.AppendLine($"Departure time: {Stop1ViewModel.DepartureDateTime}");
                sb.AppendLine($"FlightDurationUntilThisStopInMinute: {Stop1ViewModel.FlightDurationUntilThisStopInMinute}");
                sb.AppendLine($"StopDurationInMinute: {Stop1ViewModel.StopDurationInMinute}");
                sb.AppendLine();
            }

            if (Stop2)
            {
                sb.AppendLine("Stop 2:");
                sb.AppendLine($"Airport: {Stop2ViewModel.AirportIataCode}");
                sb.AppendLine($"Arrival time: {Stop2ViewModel.ArrivalDateTime}");
                sb.AppendLine($"Departure time: {Stop2ViewModel.DepartureDateTime}");
                sb.AppendLine($"FlightDurationUntilThisStopInMinute: {Stop2ViewModel.FlightDurationUntilThisStopInMinute}");
                sb.AppendLine($"StopDurationInMinute: {Stop2ViewModel.StopDurationInMinute}");
                sb.AppendLine();
            }

            return sb.ToString();
        }

        public List<FlightClass> Classes { get; set; }

        public Flight(ResponseModel.Charter.ResponseModel.FlightSegment flight)
        {
            Origin = flight.Origin.Code;
            Destination = flight.Destination.Code;
            Aircraft = flight.Aircraft;
            DepartureDateTime = flight.DepartureDateTime;
            ShamsiDepartureDateTime = flight.DepartureDateTime.ParseFlightDate().MiladiToShamsi();
            Airline = flight.Airline;
            FlightNumber = flight.FlightNumber;
            Type = "Charter";
            Stop1 = flight.Stop1 != null;
            Stop2 = flight.Stop2 != null;
            ArrivalDateTime = flight.ArrivalDateTime;
            Duration = flight.Duration;
            Stop1ViewModel = flight.Stop1 == null ? null : new StopGridViewModel
            {
                AirportIataCode = flight.Stop1.AirportIataCode,
                FlightDurationUntilThisStopInMinute = flight.Stop1.FlightDurationUntilThisStopInMinute,
                StopDurationInMinute = flight.Stop1.StopDurationInMinute,
                ArrivalDateTime = flight.Stop1.ArrivalDateTime,
                DepartureDateTime = flight.Stop1.DepartureDateTime,
            };
            Stop2ViewModel = flight.Stop2 == null ? null : new StopGridViewModel
            {
                AirportIataCode = flight.Stop2.AirportIataCode,
                FlightDurationUntilThisStopInMinute = flight.Stop2.FlightDurationUntilThisStopInMinute,
                StopDurationInMinute = flight.Stop2.StopDurationInMinute,
                ArrivalDateTime = flight.Stop2.ArrivalDateTime,
                DepartureDateTime = flight.Stop2.DepartureDateTime,
            };
            Classes = flight.Classes?.Select(bc => new FlightClass(bc)).ToList();
        }

        public Flight(ResponseModel.Webservice.ResponseModel.FlightSegment flight)
        {
            Origin = flight.Origin.Code;
            Destination = flight.Destination.Code;
            Aircraft = flight.Aircraft;
            DepartureDateTime = flight.DepartureDateTime;
            ShamsiDepartureDateTime = flight.DepartureDateTime.ParseFlightDate().MiladiToShamsi();
            Airline = flight.Airline;
            FlightNumber = flight.FlightNumber;
            Type = "Webservice";
            Stop1 = flight.Stop1 != null;
            Stop2 = flight.Stop2 != null;
            ArrivalDateTime = flight.ArrivalDateTime;
            Duration = flight.Duration;
            Stop1ViewModel = flight.Stop1 == null ? null : new StopGridViewModel
            {
                AirportIataCode = flight.Stop1.AirportIataCode,
                FlightDurationUntilThisStopInMinute = flight.Stop1.FlightDurationUntilThisStopInMinute,
                StopDurationInMinute = flight.Stop1.StopDurationInMinute,
                ArrivalDateTime = flight.Stop1.ArrivalDateTime,
                DepartureDateTime = flight.Stop1.DepartureDateTime,
            };
            Stop2ViewModel = flight.Stop2 == null ? null : new StopGridViewModel
            {
                AirportIataCode = flight.Stop2.AirportIataCode,
                FlightDurationUntilThisStopInMinute = flight.Stop2.FlightDurationUntilThisStopInMinute,
                StopDurationInMinute = flight.Stop2.StopDurationInMinute,
                ArrivalDateTime = flight.Stop2.ArrivalDateTime,
                DepartureDateTime = flight.Stop2.DepartureDateTime,
            };
            Classes = flight.Classes?.Select(bc => new FlightClass(bc)).ToList();
        }
    }
}
