import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/rpc.dart';
import 'package:fis_i18n/i18n.dart';
import 'interfaces/server_config.dart';
import 'base_manager.dart';
import 'package:fis_lib_media_rt/config.dart';

class ServerConfigManager extends BaseManager implements IServerConfigManager {
  static bool isSMSOn = false;
  static bool isScreenshot = false;
  static bool isASROn = false;
  static int consultationTimeoutTimes = 10;
  static const String defaultWebServerUrl = "https://web.flyinsono.com/";
  static const String flyinsonoSchoolPath = "open/school/#/";
  static String flyinsonoSchoolUrl = defaultWebServerUrl + flyinsonoSchoolPath;

  @override
  Future<ServerSettingResult> getAllServerConfig() async {
    try {
      var result = await rpc.organization.getServerSettingAsync(TokenRequest());
      if (result.isSMSOn != isSMSOn) {
        isSMSOn = result.isSMSOn;
      }
      if (result.isASROn != isASROn) {
        isASROn = result.isASROn;
      }
      if (result.isScreenshot) {
        isScreenshot = result.isScreenshot;
      }
      consultationTimeoutTimes = result.consultationTimeoutTimes;
      flyinsonoSchoolUrl =
          (result.webServerUrl ?? defaultWebServerUrl) + flyinsonoSchoolPath;
      _initRealTimeLiveType(result.liveProtocolType);
      return result;
    } catch (e) {
      return ServerSettingResult();
    }
  }

  @override
  Future<bool> initAllServerConfig(int retryTimes) async {
    /// 如果网络连接失败，则200ms后再次尝试，直到成功
    try {
      var result = await rpc.organization.getServerSettingAsync(TokenRequest());
      if (result.isSMSOn != isSMSOn) {
        isSMSOn = result.isSMSOn;
      }
      if (result.isASROn != isASROn) {
        isASROn = result.isASROn;
      }
      flyinsonoSchoolUrl =
          (result.webServerUrl ?? defaultWebServerUrl) + flyinsonoSchoolPath;
      _initRealTimeLiveType(result.liveProtocolType);
      print("initAllServerConfig success");
      return true;
    } catch (e) {
      if (retryTimes > 200) {
        return false;
      }
      await Future.delayed(Duration(milliseconds: 500));
      print("initAllServerConfig retry : $retryTimes");
      initAllServerConfig(retryTimes + 1);
      return false;
    }
  }

  @override
  Future<String?> getCMSUrl() async {
    if (i18nBook.isCurrentRussian) {
      return "https://info-sonic.ru";
    }
    var result = await getAllServerConfig();
    return result.cMSUrl;
  }

  @override
  Future<String?> getUserManagerUrl() async {
    var result = await getAllServerConfig();
    return result.manageUrl;
  }

  @override
  Future<bool> checkNetWork() async {
    bool result = false;
    try {
      await getAllServerConfig();
      return true;
    } catch (e) {}
    return result;
  }

  void _initRealTimeLiveType(TransactionStatusEnum type) {
    if (type == TransactionStatusEnum.Common ||
        type == TransactionStatusEnum.Tencent) {
      FRTMediaConfig.serviceType = FRealTimeMediaServiceType.ntRtmp;
    } else if (type == TransactionStatusEnum.VRTC) {
      FRTMediaConfig.serviceType = FRealTimeMediaServiceType.vrtc;
    } else {
      FRTMediaConfig.serviceType = FRealTimeMediaServiceType.trtc;
    }
  }
}
