<template>
  <div class="cpTripLegRouteSegment">
    <cp-trip-leg-route-layover
      v-if="isLayoverSectionVisible"
      :config="config"
      :duration="stopDuration"
      :locale="locale"
    />
    <cp-trip-leg-route-place
      :city="origin.name"
      class="cpTripLegRouteSegment__place"
      :config="config"
      :day-count-after-arrival="dayCountAfterArrival"
      :iata="origin.iata_code"
      :terminal="segment.origin_terminal"
      :time="departureDate"
      :type="firstAirportType"
    />
    <cp-trip-leg-route-information :config="config" :flight-duration="segmentDuration" :segment="segment" />
    <cp-trip-leg-route-place
      :city="destination.name"
      class="cpTripLegRouteSegment__place"
      :config="config"
      :day-count-after-departure="dayCountAfterDeparture"
      :iata="destination.iata_code"
      :terminal="segment.destination_terminal"
      :time="arrivalDate"
      :type="secondAirportType"
    />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

import type { CpTripDetailsConfig } from '@/constants/tripDetails/CpTripDetailsConfig'
import type { FlightSegment } from '@/constants/tripDetails/FlightSegment'
import type { TripDateTime, TripDuration } from '@/helpers/tripDetails'

import { FlightStopTypes } from '@/constants/tripDetails/FlightStopTypes'
import { fromGMTToLocal, getDayCountBetweenDates, getDuration, getFormattedDuration } from '@/helpers/tripDetails'
import CpTripLegRouteInformation from './CpTripLegRouteInformation.vue'
import CpTripLegRouteLayover from './CpTripLegRouteLayover.vue'
import CpTripLegRoutePlace from './CpTripLegRoutePlace.vue'

interface Props {
  config: CpTripDetailsConfig
  locale?: string
  previousSegmentArrival: TripDateTime
  segment: FlightSegment
  segmentCount: number
  segmentIndex: number
  stopDuration: TripDuration | null
}

const props = withDefaults(defineProps<Props>(), {
  locale: 'en',
})

const isFirstSegment = computed(() => props.segmentIndex === 0)
const isLayoverSectionVisible = computed(() => !isFirstSegment.value)
const origin = computed(() => props.segment.origin)
const destination = computed(() => props.segment.destination)
const isDirectFlight = computed(() => props.segmentCount === 1)
const isLastSegment = computed(() => props.segmentIndex === props.segmentCount - 1)

const firstAirportType = computed(() => {
  if (isFirstSegment.value) return FlightStopTypes.ORIGIN
  return FlightStopTypes.LAYOVER_ORIGIN
})

const secondAirportType = computed(() => {
  if (isDirectFlight.value || isLastSegment.value) return FlightStopTypes.DESTINATION
  return FlightStopTypes.LAYOVER_DESTINATION
})

const departureDate = computed(() =>
  fromGMTToLocal(props.segment.departure_date, props.segment.origin.timezone, props.locale),
)
const arrivalDate = computed(() =>
  fromGMTToLocal(props.segment.arrival_date, props.segment.destination.timezone, props.locale),
)

const dayCountAfterArrival = computed(() => {
  return getDayCountBetweenDates({
    departureDate: departureDate.value,
    arrivalDate: props.previousSegmentArrival,
    isArrival: true,
  })
})

const dayCountAfterDeparture = computed(() => {
  return getDayCountBetweenDates({ departureDate: departureDate.value, arrivalDate: arrivalDate.value })
})

const segmentDuration = computed(() =>
  getFormattedDuration(getDuration(arrivalDate.value, departureDate.value), props.locale),
)
</script>
