# PamhyrList.py -- Pamhyr Abstract List object for the Model
# Copyright (C) 2023  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

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

import logging

from copy import copy
from tools import trace, timer

from Model.DB import SQLSubModel

logger = logging.getLogger()

class PamhyrModelList(SQLSubModel):
    _sub_classes = [
    ]

    def __init__(self, status = None):
        super(PamhyrModelList, self).__init__()

        self._status = status
        self._lst = []

    #######
    # SQL #
    #######

    @classmethod
    def _sql_create(cls, execute):
        return cls._create_submodel(execute)

    @classmethod
    def _sql_update(cls, execute, version):
        raise NotImplementedMethodeError(cls, cls._sql_update)

    @classmethod
    def _sql_load(cls, execute, data = None):
        raise NotImplementedMethodeError(cls, cls._sql_load)

    def _sql_save(self, execute, data = None):
        raise NotImplementedMethodeError(self, self._sql_save)

    ################
    # MODEL METHOD #
    ################

    def __len__(self):
        return len(self._lst)

    @property
    def lst(self):
        return self._lst.copy()

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

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

    def new(self, index):
        """Create new elements and add it to list

        Args:
            index: The index of new elements

        Returns:
            The new elements
        """
        raise NotImplementedMethodeError(self, self._sql_save)

    def insert(self, index, new):
        self._lst.insert(index, new)
        self._status.modified()

    def delete(self, lst):
        for el in lst:
            self._lst.remove(el)
        self._status.modified()

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

    def sort(self, reverse=False, key=None):
        self._lst.sort(reverse=reverse, key=key)
        self._status.modified()

    def move_up(self, index):
        if index < len(self._lst):
            next = index - 1

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

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

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