import 'package:domain/constants/api_constants.dart';
import 'package:plugin_basic/constants/app_constant.dart';
import 'package:plugin_platform/engine/sp/sp_util.dart';
import 'package:plugin_platform/http/http_provider.dart';

import '../basic_export.dart';
import '../dio_interceptors/interceptor_auth_dio.dart';
import '../dio_interceptors/interceptor_status_code_dio.dart';

class HttpProviderInjection {
  // 设置默认的
  static void putHttpProviderByCountry() {
    //根据选中的国家赋值Api
    String baseUrl;
    int country = SPUtil.getInt(AppConstant.storageSelectedCountry, defValue: 1) ?? 1;
    if (country == 1) {
      //新加坡
      baseUrl = ApiConstants.sgBaseUrl;
    } else if (country == 2) {
      //英国
      baseUrl = ApiConstants.ukBaseUrl;
    } else if (country == 3) {
      //马来西亚
      baseUrl = ApiConstants.msBaseUrl;
    } else if (country == 4) {
      //荷兰
      baseUrl = ApiConstants.nlBaseUrl;
    } else if (country == 5) {
      //泰国
      baseUrl = ApiConstants.thBaseUrl;
    } else {
      //默认越南
      baseUrl = ApiConstants.vnBaseUrl;
    }

    //注入 HttpProvider
    Get.put(
        HttpProvider(
          baseUrl,
          interceptors: [StatusCodeDioInterceptors(), AuthDioInterceptors()], //需要加上与App关联的一些拦截处理
        ),
        permanent: true);
  }

  // 切换应用的BaseUrl的逻辑
  static void switchBaseUrl(String baseUrl) {
    //找到单例对象 HttpProvider，切换域名
    HttpProvider httpProvider = Get.find();
    httpProvider.switchBaseUrl(baseUrl);
  }
}
