import 'package:wukongimfluttersdk/common/mode.dart';
import 'package:wukongimfluttersdk/db/wk_db_helper.dart';
import 'package:wukongimfluttersdk/manager/channel_manager.dart';
import 'package:wukongimfluttersdk/manager/channel_member_manager.dart';
import 'package:wukongimfluttersdk/manager/cmd_manager.dart';
import 'package:wukongimfluttersdk/manager/connect_manager_web.dart';
import 'package:wukongimfluttersdk/manager/conversation_manager.dart';
import 'package:wukongimfluttersdk/manager/message_manager.dart';
import 'package:wukongimfluttersdk/manager/reminder_manager.dart';
import 'package:wukongimfluttersdk/model/wk_image_content.dart';
import 'package:wukongimfluttersdk/model/wk_text_content.dart';
import 'package:wukongimfluttersdk/model/wk_video_content.dart';
import 'package:wukongimfluttersdk/model/wk_voice_content.dart';
import 'package:wukongimfluttersdk/type/const.dart';

import 'common/options.dart';
import 'model/wk_card_content.dart';

class WKIM {
  WKIM._privateConstructor();
  static final WKIM _instance = WKIM._privateConstructor();
  static WKIM get shared => _instance;

  /// 设备标志应用程序
  int deviceFlagApp = 0;

  /// 使用选项设备标志
  @Deprecated('Use Options deviceFlag')
  void setDeviceFlag(int deviceFlag) {
    deviceFlagApp = deviceFlag;
  }

  /// 运行模式
  Model runMode = Model.app;

  /// 运行模式 - 是否是APP
  bool isApp() {
    return runMode == Model.app;
  }

  /// 配置
  Options options = Options();

  /// 设置配置
  Future<bool> setup(Options opts) async {
    options = opts;
    deviceFlagApp = opts.deviceFlag;
    _initNormalMsgContent();
    if (isApp()) {
      bool result = await WKDBHelper.shared.init();
      if (result) {
        messageManager.updateSendingMsgFail();
      }
      return result;
    }
    return true;
  }

  /// 初始化正常消息内容
  _initNormalMsgContent() {
    messageManager.registerMsgContent(WkMessageContentType.text,
        (dynamic data) {
      return WKTextContent('').decodeJson(data);
    });
    messageManager.registerMsgContent(WkMessageContentType.card,
        (dynamic data) {
      return WKCardContent('', '').decodeJson(data);
    });
    messageManager.registerMsgContent(WkMessageContentType.image,
        (dynamic data) {
      return WKImageContent(0, 0).decodeJson(data);
    });
    messageManager.registerMsgContent(WkMessageContentType.voice,
        (dynamic data) {
      return WKVoiceContent(0).decodeJson(data);
    });
    messageManager.registerMsgContent(WkMessageContentType.video,
        (dynamic data) {
      return WKVideoContent().decodeJson(data);
    });
  }

  /// 连接管理器
  WKWebConnectionManager connectionManager = WKWebConnectionManager.shared;

  /// 消息管理器
  WKMessageManager messageManager = WKMessageManager.shared;

  /// 会话管理器
  WKConversationManager conversationManager = WKConversationManager.shared;

  /// 渠道经理
  WKChannelManager channelManager = WKChannelManager.shared;

  /// 渠道成员经理
  WKChannelMemberManager channelMemberManager = WKChannelMemberManager.shared;

  /// 提醒管理器
  WKReminderManager reminderManager = WKReminderManager.shared;

  /// 命令管理器
  WKCMDManager cmdManager = WKCMDManager.shared;
}
