from django.utils.translation import gettext_lazy as _

from allianceauth import hooks
from allianceauth.services.hooks import MenuItemHook, UrlHook

from . import urls
from .app_settings import BUYBACKPROGRAM_ENABLE_REVERSE_BUYBACK


class BuybackProgramMenuItem(MenuItemHook):
    def __init__(self):
        MenuItemHook.__init__(
            self,
            _("Buyback Program"),
            "fa-solid fa-store",
            "buybackprogram:index",
            navactive=[
                "buybackprogram:index",
                "buybackprogram:faq",
                "buybackprogram:setup",
                "buybackprogram:my_stats",
                "buybackprogram:contract_details",
                "buybackprogram:program_",
                "buybackprogram:location_",
                "buybackprogram:remove_static_price",
                "buybackprogram:remove_watchlist_item",
                "buybackprogram:manual_contract_sync",
                "buybackprogram:trigger_price_update",
                "buybackprogram:user_settings_edit",
                "buybackprogram:item_autocomplete",
                "buybackprogram:solarsystem_autocomplete",
                "buybackprogram:marketgroup_autocomplete",
                "buybackprogram:wallet_autocomplete",
                "buybackprogram:group_autocomplete",
            ],
        )

    def render(self, request):
        if request.user.has_perm("buybackprogram.basic_access"):
            return MenuItemHook.render(self, request)
        return ""


class ReverseBuybackMenuItem(MenuItemHook):
    def __init__(self):
        MenuItemHook.__init__(
            self,
            _("Reverse Buyback"),
            "fa-solid fa-exchange-alt",
            "buybackprogram:reverse_index",
            navactive=["buybackprogram:reverse_"],
        )

    def render(self, request):
        if request.user.has_perm("buybackprogram.basic_access"):
            return MenuItemHook.render(self, request)
        return ""


@hooks.register("menu_item_hook")
def register_menu():
    return BuybackProgramMenuItem()


@hooks.register("menu_item_hook")
def register_reverse_menu():
    if BUYBACKPROGRAM_ENABLE_REVERSE_BUYBACK:
        return ReverseBuybackMenuItem()


@hooks.register("url_hook")
def register_urls():
    return UrlHook(
        urls,
        "buybackprogram",
        r"^buybackprogram/",
        excluded_views=[
            "buybackprogram.views.common.index",
            "buybackprogram.views.calculate.program_calculate",
            "buybackprogram.views.programs.reverse_index",
            "buybackprogram.views.calculate.reverse_program_calculate",
        ],
    )
