from __future__ import annotations

import matplotlib.widgets as widgets


class tkStopButton:
    def __init__(self, plt, button_region, plot_region, label, color, hovercolor, callback=None):
        self.plt = plt; self.plot_region = plot_region; self._status = "running"
        self.ax = plt.axes(button_region)
        self.button = widgets.Button(self.ax, label, color=color, hovercolor=hovercolor)
        self.button.on_clicked(callback or self._on_click)

    def _on_click(self, event):
        self.status = "stop"; self.set_text("terminating")
        print("\ntkplotevent: Stop button was pressed. Terminating...\n")

    def set_text(self, text): self.button.label.set_text(text)
    def adjust_plot(self, plot_region=None):
        region = plot_region or self.plot_region
        self.plt.subplots_adjust(top=region[0], bottom=region[1])
    @property
    def status(self): return self._status
    @status.setter
    def status(self, value): self._status = value


class tkButtonController:
    def __init__(self, plt): self.plt = plt
    def add_button(self, button_region=(0.15, 0.95, 0.10, 0.03), text="stop",
                   color="#f8e58c", hovercolor="#38b48b", callback=None):
        ax = self.plt.axes(button_region)
        button = widgets.Button(ax, text, color=color, hovercolor=hovercolor)
        if callback is not None: button.on_clicked(callback)
        return button
    def add_stop_button(self, button_region=(0.15, 0.95, 0.10, 0.03), plot_region=(0.92, 0.15),
                        label="stop", color="#f8e58c", hovercolor="#38b48b", on_stop_clicked=None):
        return tkStopButton(self.plt, button_region, plot_region, label, color, hovercolor, on_stop_clicked)
