import * as THREE from 'three'

import { MviewAsset, Scene } from './MviewFiles'
import { Parser } from './parser' 

import { MeshMaterial } from '../../materials/marmoset'
import MarmosetModel from '../../objects/MarmosetModel'
import MarmosetTexture from '@/webgl/textures/MarmosetTexture'
import MarmosetSky from '@/webgl/sky/MarmosetSky'

export default class MarmosetAssetsManager {

    parser: Parser

    files: Map <string, MviewAsset>

    private materials: Map<string, MeshMaterial>

    private textures: Map<string, MarmosetTexture>

    sky: THREE.CubeTexture;

    private sceneInfo: Scene | null;

    constructor() {

        this.parser = new Parser();

        this.files = new Map();

        this.materials = new Map();

        this.textures = new Map();

        this.sky = new THREE.CubeTexture()

        this.sceneInfo = null;
    }

    getTextures() { return this.textures }
    getMaterials() { return this.materials }

    private loadBuffer( url: string ) {

       return fetch(new Request(url)).then( response => response.arrayBuffer())

    }

    async loadAssets( url: string ) {

        return this.loadBuffer( url ).then( buffer => {

            let files = this.parser.parse(buffer);
            let sceneInfoData = files.get("scene.json");

            if(!sceneInfoData) {

                throw("文件没有场景数据")
            }

            let sceneInfo = this.parser.parseSceneInfo(sceneInfoData)

            return [ files, sceneInfo ]
        })
    }

    async load( url: string ) {
 
        return this.loadBuffer(url).then( buffer => {

            this.files = this.parser.parse(buffer)

            let sceneInfoData = this.files.get("scene.json");
                
            if(!sceneInfoData) {

                throw("文件没有场景数据")
            }

            this.sceneInfo = this.parser.parseSceneInfo( sceneInfoData );
            console.log(this.sceneInfo)
            console.log(this.files)

            if(this.sceneInfo ) {

                return this.parseScene( this.sceneInfo, this.files );
            }
            
        }) 
        
    }

    private parseScene( sceneInfo: Scene, assets: Map<string, MviewAsset> ) {

        //贴图
        assets.forEach( item => {

            if(item.type == "image/jpeg") {

                let texture = new MarmosetTexture()
                texture.build(item);
                this.textures.set(item.name, texture);

            }

        })

        

        //建立材质
        sceneInfo.materials.forEach( item => {

            //console.log(item)
            let material = new MeshMaterial();
            material.build( item, this.textures, this.sky );

            this.materials.set(item.name, material)
        })


        //建立模型
        let model = new MarmosetModel();
        model.buildMeshes(sceneInfo.meshes, this.files, this.materials);

    
        //建立背景
        let background = new MarmosetSky();
            background.build(sceneInfo.sky, this.files); 


        let group = new THREE.Group()
            group.add(model);
            group.add(background);

        return group;
    }
 


}