import { ChannelHandler } from "../interfaces/ChannelHandler";
import { FourThreeNineNineChannelHandler } from "../handlers/FourThreeNineNineChannelHandler";
import { SanLiIOSChannelHandler } from "../handlers/SanLiIOSChannelHandler";
import { SanLiAndroidChannelHandler } from "../handlers/SanLiAndroidChannelHandler";
import { QQChannelHandler } from "../handlers/QQChannelHandler";
import { SanLiH5ChannelHandler } from "../handlers/SanLiH5ChannelHandler";
import { ThreeSixZeroChannelHandler } from "../handlers/ThreeSixZeroChannelHandler";
import { MuZiChannelHandler } from "../handlers/MuZiChannelHandler";
import { QiAiH5ChannelHandler } from "../handlers/QiAiH5ChannelHandler";
import { MuZiIosChannelHandler } from "../handlers/MuZiIosChannelHandler";
import { MiniappChannelHandler } from "../handlers/MiniappChannelHandler";
import { SYIOSChannelHandler } from "../handlers/SYIOSChannelHandler";
import { MianyouChannelHandler } from "../handlers/MianyouChannelHandler";
import { HupuChannelHandler } from "../handlers/HupuChannelHandler";
import { SevenTwoZeroChannelHandler } from "../handlers/SevenTwoZeroChannelHandler";
import { MeituanChannelHandler } from "../handlers/MeituanChannelHandler";
import { HuaweiChannelHandler } from "../handlers/HuaweiChannelHandler";
import { HongKongTaiwanChannelHandler } from "../handlers/HongKongTaiwanChannelHandler";
import { ZeroOneChannelHandler } from "../handlers/ZeroOneChannelHandler";
import { QingtianChannelHandler } from "../handlers/QingtianChannelHandler";
import { WebGameChannelHandler } from "../handlers/WebGameChannelHandler";
import { OneZheShenjiangJiangmoChannelHandler } from "../handlers/OneZheShenjiangJiangmoChannelHandler";


const logger = require("../../utils/log");

class ChannelFactory {
  private handlers: Map<number, ChannelHandler>;

  constructor() {
    this.handlers = new Map();
    this.initializeHandlers();
  }

  private initializeHandlers() {
    // 1：木子
    // 2：三狸iOS（原泰国）
    // 3：三狸安卓
    // 4：QQ大厅
    // 5：三狸H5
    // 6：4399h5
    // 注册所有渠道处理器
    // 注意：这里的渠道ID需要与配置中的一致
    this.registerHandler(1, new MuZiChannelHandler()); // 木子
    this.registerHandler(2, new SanLiAndroidChannelHandler()); // 三狸Android
    this.registerHandler(3, new SanLiIOSChannelHandler()); // 三狸iOS
    this.registerHandler(4, new QQChannelHandler()); // QQ
    this.registerHandler(5, new SanLiH5ChannelHandler()); // 三狸H5
    this.registerHandler(6, new FourThreeNineNineChannelHandler()); // 4399
    this.registerHandler(7, new ThreeSixZeroChannelHandler()); // 350
    this.registerHandler(8, new QiAiH5ChannelHandler()); // qi ai
    this.registerHandler(9, new MuZiIosChannelHandler()); // muzi ios
    this.registerHandler(10, new SanLiAndroidChannelHandler()); // muzi ios
    this.registerHandler(11, new MiniappChannelHandler()); // miniapp
    this.registerHandler(12, new SYIOSChannelHandler()); // sy ios
    this.registerHandler(13, new MianyouChannelHandler()); // 面游渠道
    this.registerHandler(14, new HupuChannelHandler()); // 虎扑渠道
    this.registerHandler(15, new SevenTwoZeroChannelHandler()); // 720渠道
    this.registerHandler(16, new MeituanChannelHandler()); // 美团渠道
    this.registerHandler(17, new HuaweiChannelHandler()); // 华为渠道
    this.registerHandler(18, new HongKongTaiwanChannelHandler()); // 港台渠道
    this.registerHandler(20, new ZeroOneChannelHandler()); // 0.1渠道
    this.registerHandler(21, new QingtianChannelHandler()); // 晴天SDK
    this.registerHandler(22, new ZeroOneChannelHandler()); // 1折渠道
    this.registerHandler(23, new ZeroOneChannelHandler()); // 逍遥浪人
    this.registerHandler(24, new ZeroOneChannelHandler()); // 逍遥浪人50倍返利
    this.registerHandler(25, new WebGameChannelHandler()); // WebGame
    this.registerHandler(26, new OneZheShenjiangJiangmoChannelHandler()); // 1折-神将降魔
  }

  /**
   * 注册渠道处理器
   * @param channelId 渠道ID
   * @param handler 渠道处理器实例
   */
  public registerHandler(channelId: number, handler: ChannelHandler) {
    this.handlers.set(channelId, handler);
    logger.info(`注册渠道处理器: channelId=${channelId}`);
  }

  /**
   * 获取渠道处理器
   * @param channelId 渠道ID
   * @returns 对应的渠道处理器实例，如果未找到则返回undefined
   */
  public getHandler(channelId: number): ChannelHandler | undefined {
    const handler = this.handlers.get(channelId);
    if (!handler) {
      logger.warn(`未找到渠道处理器: channelId=${channelId}`);
    }
    return handler;
  }

  /**
   * 检查渠道处理器是否存在
   * @param channelId 渠道ID
   * @returns 是否存在对应的渠道处理器
   */
  public hasHandler(channelId: number): boolean {
    return this.handlers.has(channelId);
  }

  /**
   * 获取所有已注册的渠道ID
   * @returns 渠道ID数组
   */
  public getRegisteredChannelIds(): number[] {
    return Array.from(this.handlers.keys());
  }
}

// 创建单例实例
export const channelFactory = new ChannelFactory();
