import { AssetLoader } from "../AssetLoader.js";
import { loadDDSTexture } from "./loadDDSTexture.js";
import { loadStandardImageTexture } from "./loadStandardImageTexture.js";
import { computeFileExtension } from "../../../../core/path/computeFileExtension.js";

export class TextureAssetLoader extends AssetLoader {
    load(scope, path, success, failure, progress) {
        //figure out what kind of a texture it is
        let fileExtension = computeFileExtension(path);

        if (fileExtension === null) {
            //check if it's a data path

            const match = path.match(/^data\:image\/([a-zA-Z0-9]+)\;/);

            if (match === null) {
                throw new Error(`no file extension on path '${path}'`);

            } else {
                //seems ok
                fileExtension = match[1];
            }

        }

        const lowerCaseExtension = fileExtension.toLowerCase();
        switch (lowerCaseExtension) {
            case  'dds':
                loadDDSTexture(path, success, failure, progress);
                break;
            case 'png':
            case 'jpg':
                loadStandardImageTexture(path, success, failure, progress);
                break;
            default:
                throw new Error(`Unsupported texture file format: '${lowerCaseExtension}'`);

        }
    }
}
