import *　as THREE from 'three'

import { Material as MaterialParameter, MviewAsset } from '../../assets/marmosets/MviewFiles'
import MarmosetTexture from '@/webgl/textures/MarmosetTexture';


// note 

// ---- Blend ----  ( blendTint )
// none | alpha | add
//
// none: disable
// alpha: blendFuncSeparate(a.SRC_ALPHA, a.ONE_MINUS_SRC_ALPHA, a.ONE_MINUS_DST_ALPHA, a.ONE)
// add: a.blendColor(g[0], g[1], g[2], 1); a.blendFunc(a.ONE, a.CONSTANT_COLOR)

//


export default class MeshMaterial extends THREE.MeshPhysicalMaterial {

   
    constructor() {

        super({})

        
    }


    build( parameter: MaterialParameter, textures: Map<string, MarmosetTexture>, envMap?: THREE.CubeTexture ) {

        this.name = parameter.name;
        console.log(parameter)

        //this.side = THREE.DoubleSide
        
        this.color.set(0xffffff)
        this.map = textures.get(parameter.albedoTex) || null
        
        //this.normalScale = new THREE.Vector2(0,0);
        this.normalMap = textures.get(parameter.normalTex) || null; 
        this.normalMapType = THREE.TangentSpaceNormalMap  
       
        //this.metalness = 1
        this.metalnessMap = textures.get(parameter.reflectivityTex) || null;

        //this.roughness = 0;
        this.roughnessMap = textures.get(parameter.glossTex) || null;

        // this.clearcoat = 0.6
        // this.clearcoatNormalMap = this.normalMap
        // this.clearcoatRoughness = 0.3;

        this.envMap = envMap || null
        this.envMapIntensity = 0;

        //this.aoMapIntensity = 0.5;
        //this.aoMap = textures.get(parameter.extrasTex) || null;
    
        this.reflectivity = 0;

        this.transparent = parameter.blend != "none";
   
       

        this.alphaMap = textures.get(parameter.alphaTex) || null;
        this.BlendingParameter( parameter.blend );
        
        this.alphaTest = parameter.alphaTest || 0;

        this.needsUpdate = true
    
    }   

    private BlendingParameter( blendingMode: string ) {

        switch (blendingMode) {

            case "none":
                this.blending = THREE.NoBlending;
                break;
            case "add":
                this.blending =  THREE.AdditiveBlending;
                break;
            case "alpha":
                this.blending = THREE.CustomBlending;        // gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE_MINUS_DST_ALPHA, gl.ONE)
                this.blendSrc = THREE.SrcAlphaFactor;
                this.blendDst = THREE.OneMinusSrcAlphaFactor;
                this.blendSrcAlpha = THREE.OneMinusDstAlphaFactor;
                this.blendDstAlpha = THREE.OneFactor;
                break;

            default:
                this.blending =  THREE.NoBlending
        }
    }


    // private generateVertexShader() {

    //     let vs = `

    //         attribute vec2 aTangent;
    //         attribute vec2 aBitangent;
    //         attribute vec2 aNormal;

         
    //         varying mediump vec3 vNormal;
    //         varying mediump vec2 vUv;

    //         vec3 iW(vec2 v)
    //         {
    //             bool iX=(v.y>(32767.1/65535.0));
    //             v.y=iX?(v.y-(32768.0/65535.0)):v.y;
    //             vec3 r;
    //             r.xy=(2.0*65535.0/32767.0)*v-vec2(1.0);
    //             r.z=sqrt(clamp(1.0-dot(r.xy,r.xy),0.0,1.0));
    //             r.z=iX?-r.z:r.z;
    //             return r;
    //         }


    //         void main() 
    //         {

    //             gl_Position = projectionMatrix * modelViewMatrix * vec4(position.xyz, 1.0);
                
    //             vNormal = normalMatrix * normal; //iW(aNormal);  //normalize(normalMatrix * normal);
    //             vUv = uv;
    //         }

    //     `
    //     this.vertexShader = vs;
    // }

    // private generateFragmentShader() {

    //     let fs = `

    //         varying mediump vec3 vNormal;
    //         varying mediump vec2 vUv;

    //         vec3 packNormalToRGB( const in vec3 normal ) {
    //             return normalize( normal ) * 0.5 + 0.5;
    //         }

    //         void main() 
    //         {
    //             vec3 color = packNormalToRGB(vNormal);  //vec3(vUv, 0.0); 
    //             gl_FragColor = vec4(color, 1.0);
    //         }
        
    //     `
    //     this.fragmentShader = fs;
    // }

}