import { httpClient } from "../apis/axios";

export type ConfiguratorParams = {
    name: string;
    network?: string;
    modelName?: string;
    testModelName?: boolean;
    readLocalResource?: boolean;
    modelDir?: boolean;
    binDir?: boolean;
    gltfDir?: boolean;
    animDir?: boolean;
};

export type Configurator = {
    record: boolean;
    binAssetDir: string;
    gltfAssetDir: string;
    animAssetDir: string;
    modelAssetDir: string;
    isOnline: boolean;
    modelName: string;
    screenshot: boolean;
    targetModelName?: string;
    init: (params: ConfiguratorParams) => Promise<boolean>;
};

const configurator: Configurator = {
    record: false,
    binAssetDir: 'statics/glb/',
    gltfAssetDir: 'statics/gltf',
    animAssetDir: 'statics/anim',
    modelAssetDir: 'statics/models',
    isOnline: false,
    modelName: '',
    screenshot: false,
    targetModelName: undefined,

    /** 初始化应用配置 */
    init: async function (params: ConfiguratorParams): Promise<boolean> {
        if (typeof params !== 'object' || params === null) {
            console.error("params must be an object");
            return false;
        }
        if (typeof params.name !== 'string' || params.name.length < 1) {
            console.error("params.name must be a string");
            return false;
        }

        if (params.modelName !== undefined) {
            configurator.modelName = params.modelName;
        }

        if (params.testModelName === true) {
            configurator.targetModelName = params.modelName;
        }

        if (params.network !== undefined) {
            httpClient.baseURL = params.network;
        }

        if (params.readLocalResource === false) {
            configurator.isOnline = true;
            configurator.modelAssetDir = `${httpClient.baseURL}/api/v2/assets/sign/latest/glb`;
            configurator.binAssetDir = `${httpClient.baseURL}/api/v3/assets/sign/latest/glb/bin/`;
            configurator.gltfAssetDir = `${httpClient.baseURL}/api/v3/assets/sign/latest/glb`;
            configurator.animAssetDir = `${httpClient.baseURL}/api/v3/assets/sign/latest/glb/anim`;
        } else {
            if (params.modelDir === false) configurator.modelAssetDir = `${httpClient.baseURL}/api/v2/assets/sign/latest/glb`;
            if (params.binDir === false) configurator.binAssetDir = `${httpClient.baseURL}/api/v3/assets/sign/latest/glb/bin/`;
            if (params.gltfDir === false) configurator.gltfAssetDir = `${httpClient.baseURL}/api/v3/assets/sign/latest/glb`;
            if (params.animDir === false) configurator.animAssetDir = `${httpClient.baseURL}/api/v3/assets/sign/latest/glb/anim`;
        }

        return true;
    }
};

export default configurator;
