
/**
 * 说明：
 * * 不同服务器的请求模块。
 */

import type { DataModel, NewDataModel } from "@imengyu/js-request-transform";
import { BaseAppServerRequestModule } from "./BaseAppServerRequestModule";
import { appendGetUrlParams, defaultResponseDataHandlerCatch, RandomUtils, RequestApiError, RequestApiResult, RequestCoreInstance, RequestOptions, RequestResponse, type RequestApiInfoStruct } from "@imengyu/imengyu-utils";
import ApiCofig from "@/common/config/ApiCofig";

/**
 * 主应用服务请求模块
 */
export class AppServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
  constructor() {
    super('base-app-server');
  }
}

/**
 * 更新服务请求模块
 */
export class UpdateServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
  constructor() {
    super("https://update-server1.imengyu.top");
    this.config.requestInterceptor = (url, req) => {
      if (!req.headers)
        req.headers = {};
      req.headers['Authorization'] = JSON.stringify({
        "apiKey":"MQQDGbn8QfFJ7kStNtkxwifHP4sBTSDd",
        "apiSecret":"3BNAdR7NXGwfiRmQZkRcRM8PsyHPeBmaay2k2F4TXhGEziXSJ3ceEtH2ApfHsMhR"
      });
      return { newUrl: url, newReq: req };
    };
    this.config.responseDataHandler = async function responseDataHandler<T extends DataModel>(response: RequestResponse, req: RequestOptions, resultModelClass: NewDataModel | undefined, instance: RequestCoreInstance<T>, apiInfo: RequestApiInfoStruct): Promise<RequestApiResult<T>> {
      const method = req.method || 'GET';
      try {
        const json = await response.json();
        if (response.ok) {
          if (!json) {
            throw new RequestApiError(
              'businessError',
              '后端未返回数据',
              '',
              response.status,
              null,
              null,
              response.headers,
              apiInfo
            );
          }
          if (!json.success)
            throw new RequestApiError(
              'businessError',
              json.message,
              json.code.toString(),
              json.code,
              json,
              json,
              response.headers,
              apiInfo
            );
          
          return new RequestApiResult(
            resultModelClass ?? instance.config.modelClassCreator,
            json?.code || response.status,
            json.message,
            json.data,
            json,
            response.headers,
            apiInfo
          );
        }
        else {
          throw json;
        }

      } catch (err) {
        if (err instanceof RequestApiError) {
          throw response;
        }
        //错误统一处理
        return new Promise<RequestApiResult<T>>((resolve, reject) => {
          defaultResponseDataHandlerCatch(method, req, response, null, err as any, apiInfo, response.url, reject, instance);
        });
      }
    };
  }

  private static readonly DEVICE_UID_KEY = 'deviceUid';
  private uid = '';

  getDeviceUid() {
    if (!this.uid) {
      this.uid = uni.getStorageSync(UpdateServerRequestModule.DEVICE_UID_KEY);
      if (!this.uid) {
        this.uid = RandomUtils.genNonDuplicateID(20);
        uni.setStorageSync(UpdateServerRequestModule.DEVICE_UID_KEY, this.uid);
      }
    }
    return this.uid;
  }
}

/**
 * 腾讯地图服务请求模块
 */
export class MapServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
  constructor() {
    super("https://apis.map.qq.com");
    this.config.requestInterceptor = (url, req) => {
      url = appendGetUrlParams(url, 'key', ApiCofig.mapKey);
      return { newUrl: url, newReq: req };
    };
    this.config.responseDataHandler = async function responseDataHandler<T extends DataModel>(response: RequestResponse, req: RequestOptions, resultModelClass: NewDataModel | undefined, instance: RequestCoreInstance<T>, apiInfo: RequestApiInfoStruct): Promise<RequestApiResult<T>> {
      const method = req.method || 'GET';
      try {
        const json = await response.json();
        if (response.ok) {
          if (!json) {
            throw new RequestApiError(
              'businessError',
              '后端未返回数据',
              '',
              response.status,
              null,
              null,
              response.headers,
              apiInfo
            );
          }
          if (json.status !== 0)
            throw new RequestApiError(
              'businessError',
              json.message,
              json.status.toString(),
              json.status,
              json,
              json,
              response.headers,
              apiInfo
            );
          
          return new RequestApiResult(
            resultModelClass ?? instance.config.modelClassCreator,
            json?.status || response.status,
            json.message,
            json.result,
            json,
            response.headers,
            apiInfo
          );
        }
        else {
          throw json;
        }

      } catch (err) {
        if (err instanceof RequestApiError) {
          throw response;
        }
        //错误统一处理
        return new Promise<RequestApiResult<T>>((resolve, reject) => {
          defaultResponseDataHandlerCatch(method, req, response, null, err as any, apiInfo, response.url, reject, instance);
        });
      }
    };
  }
}