import math

from core.aochat.BaseModule import BaseModule
from core.chat_blob import ChatBlob
from core.command_alias_service import CommandAliasService
from core.command_param_types import Int, NamedParameters
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.public_channel_service import PublicChannelService
from core.text import Text
from core.igncore import IgnCore
from core.util import Util
from modules.raidbot.tower.tower_service import TowerService
from modules.standard.helpbot.playfield_controller import PlayfieldController


# noinspection DuplicatedCode
@instance()
class ContractController(BaseModule):
    PAGE_SIZE = 40

    # noinspection DuplicatedCode
    def inject(self, registry):
        self.bot: IgnCore = registry.get_instance("bot")
        self.db: DB = registry.get_instance("db")
        self.util: Util = registry.get_instance("util")
        self.text: Text = registry.get_instance("text")
        self.event_service: EventService = registry.get_instance("event_service")
        self.pork_service: PorkService = registry.get_instance("pork_service")
        self.playfield_controller: PlayfieldController = registry.get_instance("playfield_controller")
        self.public_channel_service: PublicChannelService = registry.get_instance("public_channel_service")
        self.towercache: TowerService = registry.get_instance("tower_service")
        self.command_alias_service: CommandAliasService = registry.get_instance("command_alias_service")

    @command(command="contracts",
             params=[Int('mininum', is_optional=True), NamedParameters(['page'])],
             access_level="member",
             description="Shows contracts")
    def cotracts(self, _, min_ql, named_params):
        if not min_ql:
            min_ql = 200
        data = self.db.query("SELECT CAST(SUM(ql)*2 AS INTEGER) AS contracts, "
                             "COUNT(*) as sites, org_name, org_id, faction FROM towers "
                             "where org_name IS NOT NULL GROUP BY org_name ORDER BY contracts desc", [])
        page = int(named_params.page or "1")
        offset = (page - 1) * self.PAGE_SIZE
        data = [x for x in data if x.contracts > min_ql]
        return self.format_pagination(data, offset, page, f"Tower Contracts ({len(data)})",
                                      f"There are no orgs with more than "
                                      f"<highlight>{min_ql}</highlight> contract points.",
                                      f'contracts {min_ql}')

    def format_pagination(self, data, offset, page, title, nullmsg, cmd):
        selected = data[offset:offset + self.PAGE_SIZE]
        count = len(selected)
        pages = ""
        if page > 1:
            pages += "Pages:   " + self.text.make_tellcmd("«« Page %d" % (page - 1), f'{cmd} --page={page - 1}')
        if offset + self.PAGE_SIZE < len(data):
            pages += f"   Page {page}/{math.ceil(len(data) / self.PAGE_SIZE)}"
            pages += "   " + self.text.make_tellcmd("Page %d »»" % (page + 1), f'{cmd} --page={page + 1}')
        pages += "\n"
        if count == 0:
            return nullmsg
        else:
            blob = "<font color=CCInfoText>"
            blob += "" + pages + "\n"
            index = offset
            for entry in selected:
                index += 1
                blob += self.row_formatter(entry, index, data)
            blob += "\n" + pages
            blob += "</font>"
            return ChatBlob(title, blob)

    def row_formatter(self, entry, index, data):
        return f"{self.text.zfill(index, len(data))}. " \
               f"<white>{self.text.zfill(entry.contracts, data[0].contracts)}</white> " \
               f"<{entry.faction.lower()}>{entry.org_name}</{entry.faction.lower()}>\n"
