# PlotDKP.py -- Pamhyr
# 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 -*-

from tools import timer
from View.Tools.PamhyrPlot import PamhyrPlot

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate


class PlotDKP(PamhyrPlot):
    @timer
    def draw(self, highlight=None):
        self.canvas.axes.cla()
        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)

        if self.data is None:
            return

        self.canvas.axes.set_ylabel(
            self._trad["elevation"],
            color='green', fontsize=11
        )
        self.canvas.axes.set_xlabel(
            self._trad["kp"],
            color='green', fontsize=11
        )

        kp = self.data.reach.reach.get_kp()
        z_min = self.data.reach.reach.get_z_min()

        self.canvas.axes.set_xlim(
            left=min(kp), right=max(kp)
        )

        self.line_kp_zmin = self.canvas.axes.plot(
            kp, z_min,
            color='grey', lw=1.
        )

        if len(self.data) != 0:
            kp = self.data.get_kp()
            elevation = self.data.get_elevation()

            self.line_kp_elevation = self.canvas.axes.plot(
                kp, elevation,
                color='b', marker='+', lw=1.
            )

        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()
        if self.toolbar is not None:
            self.toolbar.update()

        # self._init = True

    @timer
    def update(self, ind=None):
        if not self._init:
            self.draw()
            return
