import { Color } from 'three';
import Constant from './Constant.js';

export default class Render{
    constructor(){
        this.scene = null;
    }

    init(scene){
        this.scene = scene;
    }

    updateSelectModel(model,selected){
        if(model){
            if(selected){
                Constant.tilesMaterial.color.setHex(Constant.selectColor );
                if(model.type === "Group"){
                    this.setSelectMaterialForGroup(model);
                }
                else if(model.type === "Mesh"){
                    this.setSelectMaterialForMesh(model);
                }
            }
            else{
                Constant.tilesMaterial.color.setHex(0xffffff );
                if(model.type === "Group"){
                    this.setDefaultMaterialForGroup(model);
                }
                else if(model.type === "Mesh"){
                    this.setDefaultMaterialForMesh(model);
                }
            }
        }
    }

    setSelectMaterialForGroup(object3d){
        for(let i=0;i<object3d.children.length;++i){
            if(object3d.children[i].type === "Group"){
                this.setSelectMaterialForGroup(object3d.children[i]);
            }
            else{
                this.setSelectMaterialForMesh(object3d.children[i]);
            }
        }
    }

    setSelectMaterialForMesh(mesh){
        //mesh.material = Constant.selectTilesMaterial.clone();
        //mesh.currentHex = mesh.material.color.getHex();
        mesh.material.color.setHex(Constant.selectColor );
    }

    setDefaultMaterialForGroup(object3d){
        for(let i=0;i<object3d.children.length;++i){
            if(object3d.children[i].type === "Group"){
                this.setDefaultMaterialForGroup(object3d.children[i]);
            }
            else{
                this.setDefaultMaterialForMesh(object3d.children[i]);
            }
        }
    }

    setDefaultMaterialForMesh(mesh){
        //mesh.material = Constant.defaultTilesMaterial.clone();
        //mesh.material.color.setHex(mesh.currentHex);
        mesh.material.color.setHex(Constant.defaultColor );
    }
}

let render = new Render();
export {render}