# The MIT License (MIT)

# Copyright (c) 2021-2024 Krux contributors

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
about_screen.py
"""
from functools import partial
from kivy.clock import Clock
from src.app.screens.base_screen import BaseScreen


class WarningBetaScreen(BaseScreen):
    """WarningBetaScreen warns user about krux beta versions"""

    def __init__(self, **kwargs):
        super().__init__(wid="warning_beta_screen", name="WarningBetaScreen", **kwargs)
        self.make_grid(wid="warning_beta_screen_grid", rows=2, resize_canvas=True)

        self.make_image(
            wid=f"{self.id}_warn",
            source=self.warn_img,
            root_widget=f"{self.id}_grid",
        )

        # START of on_press buttons
        def on_ref_press(*args):
            if args[1] == "MainScreen":
                self.set_screen(name="MainScreen", direction="right")

            if args[1] == "SelectVersion":
                self.set_screen(name="SelectVersionScreen", direction="right")

        self.make_button(
            row=0,
            wid=f"{self.id}_label",
            text=self.make_label_text(),
            font_factor=36,
            halign=None,
            root_widget=f"{self.id}_grid",
            on_press=None,
            on_release=None,
            on_ref_press=on_ref_press,
        )
        self.ids[f"{self.id}_label"].halign = "justify"

        # load canvas
        fn = partial(self.update, name=self.name, key="canvas")
        Clock.schedule_once(fn, 0)

    # pylint: disable=unused-argument
    def update(self, *args, **kwargs):
        """Update buttons on related screen"""
        name = str(kwargs.get("name"))
        key = str(kwargs.get("key"))
        value = kwargs.get("value")

        def on_update():
            # Check locale
            if key == "locale":
                setattr(self, "locale", value)
                self.ids[f"{self.id}_label"].text = self.make_label_text()

        setattr(WarningBetaScreen, "on_update", on_update)
        self.update_screen(
            name=name,
            key=key,
            value=value,
            allowed_screens=("ConfigKruxInstaller", "WarningBetaScreen"),
            on_update=getattr(WarningBetaScreen, "on_update"),
        )

    def make_label_text(self):
        """
        Create a warning message where it's content is about
        the beta (and unsigned) firmware
        """
        test_repo = self.translate("This is our test repository")
        unsg_bin = self.translate(
            "These are unsigned binaries for the latest and most experimental features"
        )
        just_try = self.translate(
            "and it's just for trying new things and providing feedback."
        )
        proceed = self.translate("Proceed")
        back = self.translate("Back")

        return "".join(
            [
                f"[color=#efcc00]{test_repo}[/color]",
                "\n",
                unsg_bin,
                "\n",
                just_try,
                "\n",
                "\n",
                "[color=#ff0000]",
                f"[ref=SelectVersion]{back}[/ref]",
                "[/color]",
                "        ",
                "[color=#00ff00]",
                f"[ref=MainScreen]{proceed}[/ref]",
                "[/color]",
            ]
        )
