# import asyncio
# import datetime
# import html
#
# # noinspection PyPackageRequirements
# from discord import Embed
#
# from core.command_service import CommandService
# from core.db import DB
# from core.decorators import instance
# from core.logger import Logger
# from core.lookup.character_service import CharacterService
# from core.lookup.pork_service import PorkService
# from core.message_hub_service import MessageHubService
# from core.registry import Registry
# from core.setting_service import SettingService
# from core.setting_types import NumberSettingType
# from core.text import Text
# from core.igncore import IgnCore
# from modules.core.accounting.services.access_service import AccessService
# from modules.core.discord.discord_controller import DiscordController
#
#
# # noinspection DuplicatedCode,PyAttributeOutsideInit
# @instance()
# class MessageDistributor:
#     MESSAGE_SOURCE = "discord"
#
#     def __init__(self):
#         self.logger = Logger(__name__)
#
#     def inject(self, registry):
#         self.bot: IgnCore = registry.get_instance("bot")
#         self.db: DB = registry.get_instance("db")
#         self.character_service: CharacterService = registry.get_instance("character_service")
#         self.access_service: AccessService = registry.get_instance("access_service")
#         self.pork: PorkService = registry.get_instance("pork_service")
#         self.relay_hub_service: MessageHubService = registry.get_instance("message_hub_service")
#         self.discord: DiscordController = registry.get_instance("discord_controller")
#         self.cmd_service: CommandService = registry.get_instance("command_service")
#         self.setting_service: SettingService = registry.get_instance("setting_service")
#         self.text: Text = registry.get_instance("text")
#
#     def pre_start(self):
#         self.relay_hub_service.register_message_source("public_relay")
#         self.relay_hub_service.register_message_source("tell_logger")
#         self.relay_hub_service.register_message_source("system_logger")
#         self.relay_hub_service.register_message_source("access_denied_logger")
#         self.relay_hub_service.register_message_source("member_logger")
#
#     def start(self):
#         self.relay_hub_service.register_message_destination("tell_log", self.handle_logging_tell, ["tell_logger"],
#                                                             ["tell_log"])
#         self.setting_service.register(self.module_name, "dc_tell_log", 0, NumberSettingType(allow_empty=True),
#                                           "ChannelID for the Tell Log")
#
#         self.relay_hub_service.register_message_destination("access_denied_log", self.handle_logging_denied,
#                                                             ["access_denied_logger"], ["access_denied_log"])
#         self.setting_service.register(self.module_name, "dc_denied_log", 0, NumberSettingType(allow_empty=True),
#                                           "ChannelID for the Access Denied Log")
#
#         self.relay_hub_service.register_message_destination("relay_log", self.handle_logging_relay,
#                                                             ["alliance", "public_relay"], ["relay_log"])
#         self.setting_service.register(self.module_name, "dc_relay_log", 0, NumberSettingType(allow_empty=True),
#                                           "ChannelID for the Relay Log")
#
#         self.relay_hub_service.register_message_destination("public_relay", self.handle_public_relay, ["alliance"],
#                                                             ["public_relay"])
#         self.setting_service.register(self.module_name, "dc_relay_public", 0, NumberSettingType(allow_empty=True),
#                                           "ChannelID for the Public Relay Channel")
#
#         self.relay_hub_service.register_message_destination("system", self.handle_logging_system, ["system_logger"],
#                                                             ["system"])
#         self.setting_service.register(self.module_name, "dc_system_log", 0, NumberSettingType(allow_empty=True),
#                                           "ChannelID for the System Channel")
#
#         self.relay_hub_service.register_message_destination("dc_member_log", self.handle_logging_members,
#                                                             ["member_logger"], ["dc_member_log"])
#         self.setting_service.register(self.module_name, "dc_member_log", 0, NumberSettingType(allow_empty=True),
#                                           "ChannelID for the Member Log")
#
#         if Registry.get_instance("darknet", is_optional=True):
#             self.relay_hub_service.register_message_destination("dc_darknet_log", self.handle_darknet_log, ["darknet"],
#                                                                 [""])
#             self.setting_service.register(self.module_name, "dc_darknet_log", 0, NumberSettingType(allow_empty=True),
#                                               "ChannelID for the Darknet Log")
#
#     def handle_public_relay(self, ctx):
#         if self.setting_service.get_value("dc_relay_public") != "0":
#             message = self.format(ctx.formatted_message)
#             self.discord.send_message(self.setting_service.get_value("dc_relay_public"), html.unescape(message),
#                                       delete=3600)  # delete after 1h
#
#     def handle_logging_tell(self, ctx):
#         if self.setting_service.get_value("dc_tell_log") != "0":
#             message = self.format(ctx.message)
#             self.discord.send_message(self.setting_service.get_value("dc_tell_log"),
#                                       f"[FROM] {self.character_service.get_char_name(ctx.sender)}: {message}")
#
#     def handle_logging_denied(self, ctx):
#         if self.setting_service.get_value("dc_denied_log") != "0":
#             message = self.format(ctx.message)
#             self.discord.send_message(self.setting_service.get_value("dc_denied_log"), message)
#
#     def handle_darknet_log(self, ctx):
#         if self.setting_service.get_value("dc_darknet_log") != "0":
#             message = self.format(ctx.message)
#             self.discord.send_message(self.setting_service.get_value("dc_darknet_log"), message)
#
#     def handle_logging_relay(self, ctx):
#         if self.setting_service.get_value("dc_relay_log") != "0":
#             message = self.format(ctx.formatted_message)
#             self.discord.send_message(self.setting_service.get_value("dc_relay_log"), f"{html.unescape(message)}")
#
#     def handle_logging_system(self, ctx):
#         if self.setting_service.get_value("dc_system_log") != "0":
#             message = self.format(ctx.message)
#             self.discord.send_message(self.setting_service.get_value("dc_system_log"), f"{html.unescape(message)}")
#
#     def handle_logging_members(self, ctx):
#         if self.setting_service.get_value("dc_member_log") != "0":
#             channel = self.discord.client.get_channel(int(self.setting_service.get_value("dc_member_log")))
#             spam = []
#             current = 0
#             for message in ctx.message:
#                 current += 1
#                 spam.append(Embed(title=f"Recent changes - {datetime.date.today()} - ({current}/{len(ctx.message)})",
#                                   description=message, color=3066993))
#             asyncio.run_coroutine_threadsafe(self.send_spam(channel, spam), self.discord.loop)
#
#     async def send_spam(self, channel, message):
#         if self.discord.client.is_ready():
#             for i in message:
#                 await channel.send(embed=i)
#
#     def format(self, message):
#         return self.text.strip_html_tags(message)
