import { CommandHandler } from '../services/commandHandler';
import { AutoModService } from '../services/automodService';
import { LoggingService, setLoggingAdmin } from '../services/loggingService';
import { MusicService } from '../services/musicService';
import { TicketService } from '../services/ticketService';
import { LevelService } from '../services/levelService';
import { Client } from 'discord.js';
import { BotModuleService } from '../services/moduleService';
import { DynamicVoiceService } from '../services/dynamicVoiceService';
import { AdminService } from '../services/adminService';
import { StatuspageService } from '../services/statuspageService';
import { BirthdayService } from '../services/birthdayService';
import { ReactionRoleService } from '../services/reactionRoleService';
import { EventService } from '../services/eventService';
import { TicketAutomationService } from '../services/ticketAutomationService';
import { KnowledgeBaseService } from '../services/knowledgeBaseService';
import { RegisterService } from '../services/registerService';
import { StatsService } from '../services/statsService';

const logging = new LoggingService();

export const context = {
  client: null as Client | null,
  commandHandler: null as CommandHandler | null,
  logging,
  automod: new AutoModService(logging, true, true),
  music: new MusicService(),
  tickets: new TicketService(),
  leveling: new LevelService(),
  dynamicVoice: new DynamicVoiceService(),
  modules: new BotModuleService(),
  admin: new AdminService(),
  statuspage: new StatuspageService(),
  birthdays: new BirthdayService(),
  reactionRoles: new ReactionRoleService(),
  events: new EventService(),
  ticketAutomation: new TicketAutomationService(),
  knowledgeBase: new KnowledgeBaseService(),
  register: new RegisterService(),
  stats: new StatsService()
};

context.modules.setHooks({
  musicEnabled: {
    onDisable: async () => context.music.stopAll()
  },
  dynamicVoiceEnabled: {
    onDisable: async (guildId: string) => context.dynamicVoice.cleanupGuild(guildId, context.client)
  },
  statuspageEnabled: {
    onEnable: async (guildId: string) => {
      const cfg = await context.statuspage.getConfig(guildId);
      await context.statuspage.saveConfig(guildId, { ...cfg, enabled: true });
    },
    onDisable: async (guildId: string) => {
      const cfg = await context.statuspage.getConfig(guildId);
      await context.statuspage.saveConfig(guildId, { ...cfg, enabled: false });
    }
  },
  birthdayEnabled: {
    onEnable: async (guildId: string) => context.birthdays.invalidate(guildId),
    onDisable: async (guildId: string) => context.birthdays.invalidate(guildId)
  },
  reactionRolesEnabled: {
    onEnable: async (guildId: string) => context.reactionRoles.resyncGuild(guildId),
    onDisable: async (guildId: string) => context.reactionRoles.resyncGuild(guildId)
  },
  eventsEnabled: {
    onEnable: async (guildId: string) => context.events.tick().catch(() => undefined)
  },
  serverStatsEnabled: {
    onEnable: async (guildId: string) => context.stats.refreshGuild(guildId).catch(() => undefined),
    onDisable: async (guildId: string) => context.stats.disableGuild(guildId).catch(() => undefined)
  }
});

setLoggingAdmin(context.admin);
