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

namespace SepehrFlightApiSampleClient.Availability.WinForms.SearchByRouteAndDate.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 bool Stop1 { get; set; }

        public bool Stop2 { get; set; }

        public string ArrivalDateTime { get; set; }

        public int Duration { get; set; }

        public bool IsFlightOwnedBySupplier { get; set; }

        public string Remarks { get; set; }

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

        [Browsable(false)]
        public StopGridViewModel Stop2ViewModel { get; set; }

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

        [Browsable(false)]//I used thid attribute to hide this column on grid
        public List<string> PermittedNationalityList { get; set; }

        [Browsable(false)]//I used thid attribute to hide this column on grid
        public List<string> ProhibitedNationalityList { get; set; }

        public Flight(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;
            Stop1 = flight.Stop1 != null;
            Stop2 = flight.Stop2 != null;
            ArrivalDateTime = flight.ArrivalDateTime;
            Duration = flight.Duration;
            IsFlightOwnedBySupplier = flight.IsFlightOwnedBySupplier;
            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,
            };
            FlightClass = new FlightClass(flight.FlightClass);
            Remarks = flight.Remarks;
            PermittedNationalityList = flight.PermittedNationalityList;
            ProhibitedNationalityList = flight.ProhibitedNationalityList;
        }

        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 Flight(ResponseModel.ClosedFlight closedFlight)
        {
            Origin = closedFlight.Origin.Code;
            Destination = closedFlight.Destination.Code;
            Aircraft = closedFlight.Aircraft;
            DepartureDateTime = closedFlight.DepartureDateTime;
            ShamsiDepartureDateTime = closedFlight.DepartureDateTime.ParseFlightDate().MiladiToShamsi();
            Airline = closedFlight.Airline;
            FlightNumber = closedFlight.FlightNumber;
            Stop1 = closedFlight.Stop1 != null;
            Stop2 = closedFlight.Stop2 != null;
            ArrivalDateTime = closedFlight.ArrivalDateTime;
            Duration = closedFlight.Duration;
            IsFlightOwnedBySupplier = closedFlight.IsFlightOwnedBySupplier;
            Stop1ViewModel = closedFlight.Stop1 == null ? null : new StopGridViewModel
            {
                AirportIataCode = closedFlight.Stop1.AirportIataCode,
                FlightDurationUntilThisStopInMinute = closedFlight.Stop1.FlightDurationUntilThisStopInMinute,
                StopDurationInMinute = closedFlight.Stop1.StopDurationInMinute,
                ArrivalDateTime = closedFlight.Stop1.ArrivalDateTime,
                DepartureDateTime = closedFlight.Stop1.DepartureDateTime,
            };
            Stop2ViewModel = closedFlight.Stop2 == null ? null : new StopGridViewModel
            {
                AirportIataCode = closedFlight.Stop2.AirportIataCode,
                FlightDurationUntilThisStopInMinute = closedFlight.Stop2.FlightDurationUntilThisStopInMinute,
                StopDurationInMinute = closedFlight.Stop2.StopDurationInMinute,
                ArrivalDateTime = closedFlight.Stop2.ArrivalDateTime,
                DepartureDateTime = closedFlight.Stop2.DepartureDateTime,
            };
            Remarks = closedFlight.Remarks;
        }
    }
}
