# This file is the list of the available functions for the program. If you need to add a function,
# just write in this file.

from enums.Column import Column
from enums.NavigationalStatus import NavigationalStatus
from polars import *

# Sort the CSV by the given column from the lazyFrame object.
def sort(lazyFrame, column):

    # For every CSV in the lazyFrame object.
    for i in range(len(lazyFrame.lazyFrames)):

        # Sort the CSV i by the given column.
        lazyFrame.lazyFrames[i] = lazyFrame.lazyFrames[i].sort(column)
    return lazyFrame

# Determine the route of ships.
def route(lazyFrame):
    correct_status = str("(col(Column.NAVIGATION_STATUS.value) == NavigationalStatus.UNDERWAY_USING_ENGINE.value) | "
    + "(col(Column.NAVIGATION_STATUS.value) == NavigationalStatus.UNDERWAY_SAILING.value) | "
    + "(col(Column.NAVIGATION_STATUS.value) == NavigationalStatus.MOORED.value)")

    event_changed = str("(col(Column.NAVIGATION_STATUS.value).is_first()) | "
    + "(col(Column.MMSI.value) != col(Column.MMSI.value).shift()) | "
    + "(col(Column.NAVIGATION_STATUS.value) != col(Column.NAVIGATION_STATUS.value).shift())")

    # Sort by MMSI to make the filter easier.
    lazyFrame = sort(lazyFrame, Column.MMSI.value)

    # We filter everytime a ship sends the same navigational status.
    lazyFrame = filter(lazyFrame, eval(event_changed))

    # We only need ship that are moored or exited an harbour (navigating).
    lazyFrame = filter(lazyFrame, eval(correct_status))

    # Sort by the date
    lazyFrame = sort(lazyFrame, Column.COMPLETE_SYS_DATE.value)
    return lazyFrame

# Filter the CSV by a given condition from the lazyFrame object.
def filter(lazyFrame, condition):

    # For every CSV in the lazyFrame object.
    for i in range(len(lazyFrame.lazyFrames)):

        # Filter the CSV i with the given condition.
        lazyFrame.lazyFrames[i] = lazyFrame.lazyFrames[i].filter(condition)
    return lazyFrame