#!/usr/bin/env python

from datetime import datetime as dt

import pytz
import time

from aman.config.RunwaySequencing import RunwaySequencing

class AirportSequencing:
    def __init__(self, icao : str):
        self.Airport = icao
        self.ActiveArrivalRunways = []
        self.RunwayDependencies = []
        self.LastUpdateTimestamp = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
        self.UseShallShouldMay = True

    def clearData(self):
        self.ActiveArrivalRunways.clear()
        self.RunwayDependencies.clear()

    def activateRunway(self, runway : RunwaySequencing):
        for active in self.ActiveArrivalRunways:
            if active.Runway.Name == runway.Runway.Name:
                self.ActiveArrivalRunways[runway.Runway.Name] = runway
                return
        self.ActiveArrivalRunways.append(runway)

    def runway(self, index : int):
        if index >= len(self.ActiveArrivalRunways):
            return None
        return self.ActiveArrivalRunways[index].Runway

    def runwayIndex(self, identifier : str):
        for i in range(0, len(self.ActiveArrivalRunways)):
            if self.ActiveArrivalRunways[i].Runway.Name == identifier:
                return i
        return -1

    def deactivateRunway(self, identifier : str):
        index = self.runwayIndex(identifier)
        if 0 <= index:
            self.ActiveArrivalRunways.pop(index)

            # remove the dependencies
            for i in range(self.RunwayDependencies - 1, -1, -1):
                if index == self.RunwayDependencies[i][0] or index == self.RunwayDependencies[i][1]:
                    self.RunwayDependencies.pop(i)

    def addDependency(self, first : str, second : str):
        idxFirst = self.runwayIndex(first)
        idxSecond = self.runwayIndex(second)
        if 0 > idxFirst or 0 > idxSecond:
            return False

        foundFirst = False
        foundSecond = False
        for dependency in self.RunwayDependencies:
            if idxFirst == dependency[0] and idxSecond == dependency[1]:
                foundFirst = True
            elif idxFirst == dependency[1] and idxSecond == dependency[0]:
                foundSecond = True

        if False == foundFirst:
            self.RunwayDependencies.append([ idxFirst, idxSecond ])
        if False == foundSecond:
            self.RunwayDependencies.append([ idxSecond, idxFirst ])

        return True

    def removeDependency(self, first : str, second : str):
        idxFirst = self.runwayIndex(first)
        idxSecond = self.runwayIndex(second)
        if 0 > idxFirst or 0 > idxSecond:
            return

        for i in range(self.RunwayDependencies - 1, -1, -1):
            dependency = self.RunwayDependencies[i]

            # check for all the pairs
            if idxFirst == dependency[0] and idxSecond == dependency[1]:
                self.RunwayDependencies.pop(i)
            elif idxSecond == dependency[0] and idxSecond == dependency[0]:
                self.RunwayDependencies.pop(i)

    def findRunway(self, identifier : str):
        for runway in self.ActiveArrivalRunways:
            if runway.Runway.Name == identifier:
                return runway
        return None

    def findDependentRunways(self, identifier : str):
        # runway is unknown
        index = self.runwayIndex(identifier)
        if 0 > index:
            return []

        # search the dependency pair
        dependencies = [self.ActiveArrivalRunways[self.RunwayDependencies[i][1]] for i in range(0, len(self.RunwayDependencies)) if index == self.RunwayDependencies[i][0]]

        return dependencies
