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

from tools import timer, trace
from View.Plot.APlot import APlot

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate

class PlotXY(APlot):
    def __init__(self, canvas=None, data=None, toolbar=None):
        super(PlotXY, self).__init__(
            canvas=canvas,
            data=data,
            toolbar=toolbar
        )

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

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

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

        # Axes
        self.canvas.axes.set_xlabel(
            _translate("MainWindow_reach", "X (m)"),
            color='green', fontsize=12
        )
        self.canvas.axes.set_ylabel(
            _translate("MainWindow_reach", "Y (m)"),
            color='green', fontsize=12
        )

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

        # 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)
        ]

        # 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.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()
        self.toolbar.update()

    @timer
    def update(self, ind=None):
        if ind is not None:
            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)
        else:
            for ind in range(self.data.number_profiles):
                self.line_xy[ind][0].set_data(
                    self.data.profile(ind).x(),
                    self.data.profile(ind).y()
                )

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

            for ind in range(len(self.line_gl)):
                self.line_gl[ind][0].set_data(
                    x_complete[ind],
                    y_complete[ind]
                )

        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()