# PlotXY.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, trace
from View.Tools.PamhyrPlot import PamhyrPlot

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate


class PlotXY(PamhyrPlot):
    def __init__(self, canvas=None, trad=None, data=None, toolbar=None,
                 display_current=True, parent=None):
        super(PlotXY, self).__init__(
            canvas=canvas,
            trad=trad,
            data=data,
            toolbar=toolbar,
            parent=parent
        )

        self.display_current = display_current

        self.line_xy = []
        self.line_gl = []

        self.before_plot_selected = None
        self.plot_selected = None
        self.after_plot_selected = None

    @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

        if self.data.number_profiles == 0:
            self._init = False
            return

        kp_min, kp_max = (-1, -1)
        if highlight is not None:
            kp_min, kp_max = highlight

        # Axes
        self.canvas.axes.set_xlabel(
            _translate("Geometry", "X (m)"),
            color='green', fontsize=10
        )
        self.canvas.axes.set_ylabel(
            _translate("Geometry", "Y (m)"),
            color='green', fontsize=10
        )
        self.canvas.axes.axis("equal")

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

        # Draw line for each profile
        self.line_xy = [
            self.canvas.axes.plot(
                x, y, lw=1.,
                color='b' if kp_min <= kp <= kp_max else 'r',
                markersize=3, marker='+'
            )
            for x, y, kp in zip(
                self.data.get_x(), self.data.get_y(),
                kp
            )
        ]

        # Guide lines
        x_complete = self.data.get_guidelines_x()
        y_complete = self.data.get_guidelines_y()

        self.line_gl = [
            self.canvas.axes.plot(
                x, y,
            )
            for x, y in zip(x_complete, y_complete)
        ]

        if self.display_current:
            # Previous profile
            self.before_plot_selected, = self.canvas.axes.plot(
                self.data.profile(0).x(),
                self.data.profile(0).y(),
                lw=1., markersize=3,
                marker='+', color="k", linestyle="--"
            )
            self.before_plot_selected.set_visible(False)

            # Current profile
            self.plot_selected, = self.canvas.axes.plot(
                self.data.profile(0).x(),
                self.data.profile(0).y(),
                lw=1., markersize=3,
                marker='+', color="b"
            )
            self.plot_selected.set_visible(False)

            # Next profile
            self.after_plot_selected, = self.canvas.axes.plot(
                self.data.profile(0).x(),
                self.data.profile(0).y(),
                lw=1., markersize=3,
                marker='+', color="m", linestyle='--'
            )
            self.after_plot_selected.set_visible(False)

        self.canvas.axes.autoscale_view(True, True, True)
        self.canvas.axes.autoscale()
        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

        if self.data is None:
            return

        self.data.compute_guidelines()
        x_complete = self.data.get_guidelines_x()
        y_complete = self.data.get_guidelines_y()

        for i in range(self.data.number_profiles):
            if i < len(self.line_xy):
                self.line_xy[i][0].set_data(
                    self.data.profile(i).x(),
                    self.data.profile(i).y()
                )
            else:
                self.line_xy.append(
                    self.canvas.axes.plot(
                        self.data.profile(i).x(),
                        self.data.profile(i).y(),
                        lw=1., color='r',
                        markersize=3, marker='+'
                    )
                )

        for i in range(len(x_complete)):
            if i < len(self.line_gl):
                self.line_gl[i][0].set_data(
                    x_complete[i],
                    y_complete[i]
                )
            else:
                self.line_gl.append(
                    self.canvas.axes.plot(
                        x_complete[i],
                        y_complete[i]
                    )
                )

        if ind is not None and self.display_current:
            before = ind - 1
            after = ind + 1

            self.before_plot_selected.set_visible(False)
            self.plot_selected.set_visible(False)
            self.after_plot_selected.set_visible(False)

            if 0 <= before < self.data.number_profiles:
                self.before_plot_selected.set_data(
                    self.data.profile(before).x(),
                    self.data.profile(before).y()
                )
                self.before_plot_selected.set_visible(True)

            if 0 <= ind < self.data.number_profiles:
                self.plot_selected.set_data(self.data.profile(ind).x(),
                                            self.data.profile(ind).y())
                self.plot_selected.set_visible(True)

            if 0 <= after < self.data.number_profiles:
                self.after_plot_selected.set_data(
                    self.data.profile(after).x(),
                    self.data.profile(after).y()
                )
                self.after_plot_selected.set_visible(True)

        self.canvas.axes.relim()
        self.canvas.axes.autoscale()
        self.canvas.axes.autoscale_view()
        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()
