# -*- coding: utf-8 -*-

from copy import copy
from tools import trace, timer

from Model.Except import NotImplementedMethodeError

from Model.BoundaryCondition.BoundaryConditionTypes import (
    NotDefined,
    PonctualContribution,
    TimeOverZ, TimeOverDebit, ZOverDebit
)

class BoundaryConditionList(object):
    def __init__(self, status = None):
        super(BoundaryConditionList, self).__init__()

        self._status = status

        self._tabs = {
            "liquid" : [],
            "solid" : [],
            "suspenssion" : []
        }

    def len(self, lst):
        return len(self._tabs[lst])

    def get_tab(self, lst):
        return self._tabs[lst].copy()

    def get(self, lst, row):
        return self._tabs[lst][row]

    def set(self, lst, row, new):
        self._tabs[lst][row] = new
        self._status.modified()

    def new(self, lst, index):
        n = NotDefined(status=self._status)
        self._tabs[lst].insert(index, n)
        self._status.modified()
        return n

    def insert(self, lst, index, new):
        self._tabs[lst].insert(index, new)
        self._status.modified()

    def delete(self, lst, bcs):
        for bc in bcs:
            self._tabs[lst].remove(bc)
        self._status.modified()

    def delete_i(self, lst, indexes):
        bcs = list(
            map(
                lambda x: x[1],
                filter(
                    lambda x: x[0] in indexes,
                    enumerate(self._tabs[lst])
                )
            )
        )
        self.delete(lst, bcs)

    def sort(self, lst, reverse=False, key=None):
        self._tabs[lst].sort(reverse=reverse, key=key)
        self._status.modified()

    def move_up(self, lst, index):
        if index < len(self._tabs[lst]):
            next = index - 1

            l = self._tabs[lst]
            l[index], l[next] = l[next], l[index]
            self._status.modified()

    def move_down(self, lst, index):
        if index >= 0:
            prev = index + 1

            l = self._tabs[lst]
            l[index], l[prev] = l[prev], l[index]
            self._status.modified()

    def __copy__(self):
        new = BoundaryConditionList()

        for l in self._tabs:
            new.tabs[l] = self._tabs[l].copy()

        return new

    def __deepcopy__(self):
        new = BoundaryConditionList()

        for l in self._tabs:
            new.tabs[l] = self._tabs[l].deepcopy()

        return new

    def copy(self):
        return copy(self)