# Reservoir.py -- Pamhyr
# Copyright (C) 2023-2026  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 Model.Tools.PamhyrDB import SQLSubModel
from Model.Scenario import Scenario

logger = logging.getLogger()


class Data(SQLSubModel):
    _sub_classes = []

    def __init__(self, id: int = -1,
                 elevation: float = 0.,
                 surface: float = 0.,
                 status=None, owner_scenario=-1):
        super(Data, self).__init__(
            id=id, status=status,
            owner_scenario=owner_scenario
        )

        self._data = [elevation, surface]

    @classmethod
    def _db_create(cls, execute, ext=""):
        execute(f"""
          CREATE TABLE reservoir_data{ext}(
            {cls.create_db_add_pamhyr_id()},
            deleted BOOLEAN NOT NULL DEFAULT FALSE,
            ind INTEGER NOT NULL,
            elevation REAL NOT NULL,
            surface REAL NOT NULL,
            reservoir INTEGER,
            {Scenario.create_db_add_scenario()},
            {Scenario.create_db_add_scenario_fk()},
            FOREIGN KEY(reservoir) REFERENCES reservoir(pamhyr_id),
            PRIMARY KEY(pamhyr_id, scenario)
          )
        """)

        if ext == "_tmp":
            return True

        return cls._create_submodel(execute)

    @classmethod
    def _db_update(cls, execute, version, data=None):
        major, minor, release = version.strip().split(".")

        if major == minor == "0":
            if int(release) > 5:
                cls._db_update_to_0_2_0(execute, data)

        if major == "0" and int(minor) <= 1:
            if int(release) < 2:
                execute(
                    "ALTER TABLE reservoir_data " +
                    "ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE"
                )

        return cls._update_submodel(execute, version, data)

    @classmethod
    def _db_update_to_0_2_0(cls, execute, data):
        table = "reservoir_data"
        id2pid = data['id2pid']

        cls.update_db_add_pamhyr_id(
            execute, table,
            data=data
        )
        Scenario.update_db_add_scenario(execute, table)

        cls._db_create(execute, ext="_tmp")
        execute(
            f"INSERT INTO {table}_tmp " +
            "(pamhyr_id, ind, elevation, surface, reservoir, scenario) " +
            "SELECT pamhyr_id, ind, elevation, surface, reservoir, scenario " +
            f"FROM {table}"
        )

        execute(f"DROP TABLE {table}")
        execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")

        if 'reservoir' in id2pid:
            reservoirs = id2pid['reservoir']
            cls._db_update_to_0_2_0_set_reservoir_pid(
                execute, table, reservoirs
            )

    @classmethod
    def _db_update_to_0_2_0_set_reservoir_pid(cls, execute, table, reservoirs):
        bcs = execute(
            f"SELECT pamhyr_id, reservoir FROM {table}"
        )

        for row in bcs:
            it = iter(row)
            pid = next(it)
            reservoir_id = next(it)

            execute(
                f"UPDATE {table} " +
                f"SET reservoir = {reservoirs[reservoir_id]} " +
                f"WHERE pamhyr_id = {pid}"
            )

    @classmethod
    def _db_load(cls, execute, data=None):
        new = []
        scenario = data["scenario"]
        if scenario is None:
            return new

        loaded = data['loaded_pid']
        reseroire = data["reservoir"]

        values = execute(
            "SELECT pamhyr_id, deleted, ind, " +
            "elevation, surface, scenario " +
            "FROM reservoir_data " +
            f"WHERE reservoir = {reseroire.pamhyr_id} " +
            f"AND scenario = {scenario.id} " +
            f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) " +
            "ORDER BY ind ASC"
        )

        for v in values:
            it = iter(v)

            pid = next(it)
            deleted = (next(it) == 1)
            ind = next(it)
            elevation = next(it)
            surface = next(it)
            owner_scenario = next(it)

            nd = cls(
                id=pid, status=data['status'],
                owner_scenario=owner_scenario
            )
            if deleted:
                nd.set_as_deleted()

            nd._data = [elevation, surface]

            loaded.add(pid)
            new.append(nd)

        data["scenario"] = scenario.parent
        new += cls._db_load(execute, data)
        data["scenario"] = scenario

        return new

    def _db_save(self, execute, data=None):
        if not self.must_be_saved():
            return True

        pid = self._pamhyr_id
        ind = data["ind"]
        elevation = self._db_format(str(self[0]))
        surface = self._db_format(str(self[1]))
        reservoir = data["reservoir"]

        execute(
            "INSERT INTO reservoir_data " +
            "(pamhyr_id, deleted, ind, " +
            "elevation, surface, reservoir, scenario) " +
            "VALUES (" +
            f"{pid}, " +
            f"{self._db_format(self.is_deleted())}, " +
            f"{ind}, '{elevation}', {surface}, " +
            f"{reservoir._pamhyr_id}, " +
            f"{self._status.scenario_id}" +
            ")"
        )

    def __getitem__(self, key):
        return self._data[key]

    def __setitem__(self, key, value):
        self._data[key] = float(value)


class Reservoir(SQLSubModel):
    _sub_classes = [Data]

    def __init__(self, id: int = -1,
                 name: str = "",
                 status=None, owner_scenario=-1):
        super(Reservoir, self).__init__(
            id=id, status=status,
            owner_scenario=owner_scenario
        )

        self._name = name
        self._node = None
        self._data = []

    @classmethod
    def _db_create(cls, execute, ext=""):
        execute(f"""
          CREATE TABLE reservoir{ext} (
            {cls.create_db_add_pamhyr_id()},
            deleted BOOLEAN NOT NULL DEFAULT FALSE,
            name TEXT NOT NULL,
            node INTEGER,
            {Scenario.create_db_add_scenario()},
            {Scenario.create_db_add_scenario_fk()},
            FOREIGN KEY(node) REFERENCES river_node(pamhyr_id),
            PRIMARY KEY(pamhyr_id, scenario)
          )
        """)

        if ext == "_tmp":
            return True

        return cls._create_submodel(execute)

    @classmethod
    def _db_update(cls, execute, version, data=None):
        major, minor, release = version.strip().split(".")
        if major == minor == "0":
            if int(release) < 5:
                cls._db_create(execute)
                return True

            cls._db_update_to_0_2_0(execute, data)

        if major == "0" and minor == "1":
            if int(release) < 2:
                execute(
                    "ALTER TABLE additional_files " +
                    "ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE"
                )

        return cls._update_submodel(execute, version, data)

    @classmethod
    def _db_update_to_0_2_0(cls, execute, data):
        table = "reservoir"
        nodes = data['id2pid']['river_node']

        cls.update_db_add_pamhyr_id(execute, table, data=data)
        cls._db_update_to_0_2_0_set_node_pid(execute, table, nodes)
        Scenario.update_db_add_scenario(execute, table)

        cls._db_create(execute, ext="_tmp")
        execute(
            f"INSERT INTO {table}_tmp " +
            "(pamhyr_id, name, node, scenario) " +
            "SELECT pamhyr_id, name, node, scenario " +
            f"FROM {table}"
        )

        execute(f"DROP TABLE {table}")
        execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")

    @classmethod
    def _db_load(cls, execute, data=None):
        new = []
        scenario = data["scenario"]
        loaded = data['loaded_pid']

        if scenario is None:
            return new

        table = execute(
            "SELECT pamhyr_id, deleted, name, node, scenario " +
            "FROM reservoir " +
            f"WHERE scenario = {scenario.id} " +
            f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
        )

        for row in table:
            it = iter(row)

            id = next(it)
            deleted = (next(it) == 1)
            name = next(it)
            node_id = next(it)
            owner_scenario = next(it)

            new_reservoir = cls(
                id, name,
                status=data["status"],
                owner_scenario=owner_scenario
            )
            if deleted:
                new_reservoir.set_as_deleted()

            new_reservoir._node = None
            if node_id != -1 and node_id is not None:
                new_reservoir._node = next(
                    filter(
                        lambda n: n.id == node_id, data["nodes"]
                    ), None
                )

            data["reservoir"] = new_reservoir
            new_reservoir._data = Data._db_load(
                execute, data=data
            )

            loaded.add(id)
            new.append(new_reservoir)

        data["scenario"] = scenario.parent
        new += cls._db_load(execute, data)
        data["scenario"] = scenario

        return new

    def _db_save(self, execute, data=None):
        if not self.must_be_saved():
            return True

        execute(
            "DELETE FROM reservoir " +
            f"WHERE pamhyr_id = {self.pamhyr_id} " +
            f"AND scenario = {self._status.scenario_id}"
        )
        execute(
            "DELETE FROM reservoir_data " +
            f"WHERE reservoir = {self.pamhyr_id} " +
            f"AND scenario = {self._status.scenario_id}"
        )

        node_id = 'NULL'
        if self._node is not None:
            node_id = self._node.pamhyr_id

        sql = (
            "INSERT INTO " +
            "reservoir(pamhyr_id, deleted, name, node, scenario) " +
            "VALUES (" +
            f"{self.pamhyr_id}, " +
            f"{self._db_format(self.is_deleted())}, " +
            f"'{self._db_format(self._name)}', " +
            f"{node_id}, " +
            f"{self._status.scenario_id}" +
            ")"
        )
        execute(sql)

        data["reservoir"] = self

        ind = 0
        for d in self._data:
            data["ind"] = ind

            d._db_save(execute, data)

            ind += 1

        return True

    def _data_traversal(self,
                        predicate=lambda obj, data: True,
                        modifier=lambda obj, data: None,
                        data={}):
        if predicate(self, data):
            modifier(self, data)

        for d in self._data:
            d._data_traversal(predicate, modifier, data)

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

    @property
    def name(self):
        if self._name == "":
            return f"Reservoir #{self.pamhyr_id}"

        return self._name

    @name.setter
    def name(self, name):
        self._name = name
        self.modified()

    @property
    def node(self):
        return self._node

    @node.setter
    def node(self, node):
        self._node = node
        self.modified()

    def has_node(self):
        return self._node is not None

    @property
    def data(self):
        return self._data.copy()

    @property
    def _default_elevation(self):
        return 0.0

    @property
    def _default_surface(self):
        return 0.0

    def is_define(self):
        return len(self._data) != 0

    def new_from_data(self, data):

        try:
            new_0 = float(data[0])
            new_1 = float(data[1])
        except Exception as e:
            logger.error(e)
            new_0 = None
            new_1 = None

        return (new_0, new_1)

    def add(self, index: int):
        data = Data(
            elevation=self._default_elevation,
            surface=self._default_surface,
            status=self._status,
        )

        self._data.insert(index, data)
        self.modified()
        return data

    def insert(self, index: int, value):
        if type(value) is tuple or type(value) is list:
            value = Data(
                elevation=value[0],
                surface=value[1]
            )

        self._data.insert(index, value)
        self.modified()

    def hard_delete_i(self, indexes):
        self._data = list(
            map(
                lambda e: e[1],
                filter(
                    lambda e: e[0] not in indexes,
                    enumerate(self._data)
                )
            )
        )
        self.modified()

    def delete_i(self, indexes):
        list(
            map(
                lambda e: e[1].set_as_deleted(),
                filter(
                    lambda e: e[0] in indexes,
                    enumerate(self.lst)
                )
            )
        )
        self.modified()

    def delete(self, els):
        for el in els:
            el.set_as_deleted()

        self.modified()

    def sort(self, _reverse=False, key=None):
        if key is None:
            self._data.sort(reverse=_reverse, key=lambda d: d[0])
        else:
            self._data.sort(reverse=_reverse, key=key)

        self.modified()

    def get_i(self, index):
        return self.data[index]

    def get_range(self, _range):
        lst = []
        for r in _range:
            lst.append(r)

        return lst

    def _set_i_c_v(self, index, column, value):
        self._data[index][column] = value
        self.modified()

    def set_i_elevation(self, index: int, value):
        self._set_i_c_v(index, 0, value)

    def set_i_surface(self, index: int, value):
        self._set_i_c_v(index, 1, value)

    def move_up(self, index):
        if index < len(self):
            next = index - 1
            d = self._data
            d[index], d[next] = d[next], d[index]
            self.modified()

    def move_down(self, index):
        if index >= 0:
            prev = index + 1
            d = self._data
            d[index], d[prev] = d[prev], d[index]
            self.modified()
