# Edge.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 -*-

from Model.Tools.PamhyrDB import SQLSubModel
from Model.Network.Node import Node


class Edge(SQLSubModel):
    def __init__(self, id: int = -1, name: str = "",
                 node1: Node = None,
                 node2: Node = None,
                 **kwargs):
        super(Edge, self).__init__(id=id, **kwargs)

        self._name = name
        self.node1 = node1
        self.node2 = node2

        self._enable = True

    def __getitem__(self, name):
        ret = None

        if name == "name":
            ret = self.name
        elif name == "id":
            ret = self.pamhyr_id
        elif name == "node1":
            ret = self.node1
        elif name == "node2":
            ret = self.node2
        elif name == "enable":
            ret = self._enable

        return ret

    def __setitem__(self, name, value):
        if name == "name":
            self._name = value
        elif name == "id":
            self.pamhyr_id = value
        elif name == "node1":
            self.node1 = value
        elif name == "node2":
            self.node2 = value
        elif name == "enable":
            self._enable = value

        self.modified()

    @property
    def id(self):
        return self.pamhyr_id

    @property
    def name(self):
        if self._name == "":
            return f"R{self.id}"

        return self._name

    def is_enable(self):
        return self._enable

    def enable(self, enable=True):
        self._enable = enable
        self.modified()

    def disable(self):
        self._enable = False
        self.modified()

    def reverse(self):
        tmp = self.node1
        self.node1 = self.node2
        self.node2 = tmp
        self.modified()
