from core import command_request
from core.command_param_types import Options, Int, Character, Any
from core.db import DB
from core.decorators import instance, command
from core.event_service import EventService
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 PointsController:
    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 = 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=[Options(["give", "take"]), Int("points"), Character("char"), Any("reason")],
             access_level="admin",
             description="Give or take points from an Account",
             sub_command="modify")
    def account_add_mod_pts(self, request: command_request, option, amount, user, reason):
        if option == "take":
            amount = -amount
        self.account_service.add_pts(user.char_id, amount, reason, request.sender.char_id)

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