from core import command_request
from core.aochat.BaseModule import BaseModule
from core.chat_blob import ChatBlob
from core.command_param_types import Const, Options, Int, Character, Any
from core.db import DB
from core.decorators import instance, command
from core.event_service import EventService
from core.lookup.character_service import CharacterService
from core.lookup.pork_service import PorkService
from core.text import Text
from core.util import Util
from modules.core.accounting.preference_controller import PreferenceController
from modules.core.accounting.services.account_service import AccountService


@instance()
class AccountLogController(BaseModule):

    # noinspection DuplicatedCode
    def inject(self, registry):
        self.bot = registry.get_instance("bot")
        self.buddy_service = registry.get_instance("buddy_service")
        self.util: Util = registry.get_instance("util")
        self.db: DB = registry.get_instance("db")
        self.character_service: CharacterService = registry.get_instance("character_service")
        self.account_service: AccountService = registry.get_instance("account_service")
        self.event_service: EventService = registry.get_instance("event_service")
        self.pork_service: PorkService = registry.get_instance("pork_service")
        self.text: Text = registry.get_instance("text")
        self.preferences: PreferenceController = registry.get_instance("preference_controller")

    @command(command="account", params=[Const("log"), Options(["public", "raid", "loot"]),
                                        Character("char"), Int("count", is_optional=True)], access_level="member",
             description="Lookup logentries of a Account",
             sub_command="public")
    def account_log_1(self, _, _1, log_type, user, count):
        main = self.account_service.get_main(user.char_id).char_id
        entries = self.account_service.get_logs(main, log_type=log_type, limit=count or 25)
        return ChatBlob(f"Logentries for {user.name} in the category {log_type}", self.display_logs(entries))

    @command(command="account",
             params=[Const("log"), Options(["admin", "system", "points"]),
                     Character("char"), Int("count", is_optional=True)],
             access_level="moderator",
             description="Lookup logentries of a Account",
             sub_command="moderate")
    def account_log_2(self, _, _1, log_type, user, count):
        main = self.account_service.get_main(user.char_id).char_id
        entries = self.account_service.get_logs(main, log_type=log_type, limit=count or 25)
        return ChatBlob(f"Logentries for {user.name} in the category {log_type}", self.display_logs(entries))

    @command(command="account", params=[Const("addlog"), Options(["admin", "public"]),
                                        Character("char"), Any("text")], access_level="moderator",
             description="Add a log entry to an Account",
             sub_command="moderate")
    def account_add_log(self, request: command_request, _, log_type, user, text):
        main = self.account_service.get_main(user.char_id).char_id
        if not main:
            return f"The main of <highlight>{user}</highlight> could not be found. Are you sure there is an account?"
        self.account_service.add_log(main, log_type, text, request.sender.char_id)
        return f"The log entry for <highlight>{self.character_service.resolve_char_to_name(main)}</highlight> has been added successfully."

    # noinspection LongLine
    @command(command="account", params=[Const("log"), Const("id"), Int("id")], access_level="moderator",
             description="View a logentry of an user",
             sub_command="moderate")
    def account_view_log(self, _, _1, _2, log_id):
        entry = self.account_service.get_log_by_id(log_id)
        if entry:
            return ChatBlob(f"Information about Logentry {log_id}",
                            f"ID: <highlight>{entry.log_id}</highlight>\n"
                            f"Type: <highlight>{entry.type.capitalize()}</highlight>\n"
                            f"Affected Main: <highlight>{self.character_service.resolve_char_to_name(entry.char_id)}</highlight>\n"
                            f"Responsible Leader: <highlight>{self.character_service.resolve_char_to_name(entry.leader_id)}</highlight>\n"
                            f"Points: <highlight>{f'<green>+{entry.delta}</green>' if entry.delta > 0 else f'<red>{entry.delta}</red>'}</highlight>\n"
                            f"Message: <grey>{entry.reason}</grey>\n"
                            f"Logging Time: <highlight>{self.util.format_datetime(entry.created_at)}</highlight>")
        else:
            return f"No Logentry with ID {log_id} found."

    @command(command="account", params=[Const("log"), Const("recent")], access_level="moderator",
             description="View the 20 most recent log entries",
             sub_command="moderate")
    def account_view_recent(self, _, _1, _2):
        entries = self.account_service.get_logs(limit=20)
        return ChatBlob(f"Most Recent logentries", self.display_logs(entries))

    def display_logs(self, entries) -> str:
        blob = ""
        for entry in entries:
            blob += self.account_service.format_entry(entry)
        return blob
