import 'package:atmob_channel_reader/atmob_channel_reader.dart';

import '../../device/atmob_platform_info.dart';
import '../../utils/mmkv_util.dart';
import '../data/consts/constants.dart';

class ChannelUtil {
  ChannelUtil._();

  static Future<void> initChannel() async {
    await AtmobChannelReader.default4Test(Constants.appDefaultChannel,
        Constants.appDefaultAppId, Constants.appDefaultTgPlatformId);

    String? channel = KVUtil.getString(
        Constants.appChanelName, await AtmobChannelReader.getChannel());
    KVUtil.putString(Constants.appChanelName, channel);

    int? channelId = KVUtil.getInt(Constants.appChannelId, -1);
    if (channelId == -1) {
      channelId = await AtmobChannelReader.getAppId();
    }
    if (channelId != null) {
      KVUtil.putInt(Constants.appChannelId, channelId);
    }

    int? appTgPlatformId = KVUtil.getInt(Constants.appTgPlatformId, -1);
    if (appTgPlatformId == -1) {
      appTgPlatformId = await AtmobChannelReader.getTgPlatformId();
    }
    if (appTgPlatformId != null) {
      KVUtil.putInt(Constants.appTgPlatformId, appTgPlatformId);
    }
    atmobPlatformInfo.setChannelName(channel);
    atmobPlatformInfo.setAppId(channelId);
    atmobPlatformInfo.setTgPlatform(appTgPlatformId);
  }
}
