import time

from core.aochat.BaseModule import BaseModule
from core.chat_blob import ChatBlob
from core.command_alias_service import CommandAliasService
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


@instance()
class PlantController(BaseModule):
    # 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="penalty", params=[], access_level="member", description="Shows planttimers")
    def penalty(self, _):
        blob = "<font color=CCInfoText>"
        self.towercache.attack_hot.sort(key=lambda k: k['org_name'])
        rem = []
        for key, value in enumerate(sorted(self.towercache.attack_hot, key=lambda k: k['org_name'])):
            if value["hot"] < time.time():
                rem.append(key)
                continue

            lca = self.towercache.get_towers_by_org_name(value["org_name"])
            for site in lca:
                blob += self.towercache.format_entry(site, 10)

        blob += "</font>"
        for i in reversed(rem):
            self.towercache.attack_hot.pop(i)
        if len(self.towercache.attack_hot) == 0:
            blob = ""
        return ChatBlob(f"Warhot tower sites", blob.strip("\n")) if blob != "" else f"There are no orgs in penalty."

    @command(command="plant", params=[], access_level="member", description="Shows planttimers")
    def plant(self, _):
        blob = "<font color=CCInfoText>"
        rem = []
        for key, value in enumerate(sorted(self.towercache.plant, key=lambda k: k['pf'])):
            print(key, value)
            if value["plant"] < time.time():
                rem.append(key)
                continue

            lca = self.playfield_controller.get_playfield_by_id(value['pf'])
            blob += f"[<cyan>{lca.short_name}</cyan>] <cyan>x{value['site']}</cyan> in " \
                    f"<white>{self.util.format_time(value['plant'] - time.time())}</white>\n"

        blob += "</font>"
        for i in reversed(rem):
            self.towercache.plant.pop(i)
        if len(self.towercache.plant) == 0:
            blob = ""
        if blob != "":
            return ChatBlob(f"Awaiting plants ({len(self.towercache.plant)})", blob.strip("\n"))
        else:
            return f"There are no sites awaiting plants."
