# -*- coding: utf-8 -*-
import pymel.core as pm
import mtoa.utils as utils
import mtoa.ui.ae.utils as aeUtils
from mtoa.ui.ae.shaderTemplate import ShaderAETemplate


class AEaiStandardSurfaceTemplate(ShaderAETemplate):
    convertToMayaStyle = True
   
    def changeParams(self, nodeName):
        metal_0 = float(pm.getAttr(nodeName + '.metalness')) == 0.0
        metal_1 = float(pm.getAttr(nodeName + '.metalness')) == 1.0
        transmission_0 = float(pm.getAttr(nodeName + '.transmission')) == 0.0
        transmission_1 = float(pm.getAttr(nodeName + '.transmission')) == 1.0
        subsurface_0 = float(pm.getAttr(nodeName + '.subsurface')) == 0.0
        subsurface_1 = float(pm.getAttr(nodeName + '.subsurface')) == 1.0
        thin_walled = bool(pm.getAttr(nodeName + '.thin_walled'))

        pm.editorTemplate(dimControl=(nodeName, 'specularIOR', metal_1))
        pm.editorTemplate(dimControl=(nodeName, 'diffuseRoughness', metal_1 or transmission_1))

        dim_base = metal_0 and transmission_1
        pm.editorTemplate(dimControl=(nodeName, 'base', dim_base))
        pm.editorTemplate(dimControl=(nodeName, 'baseColor', dim_base))

        dim_transmission = metal_1 or transmission_0
        dim_transmission_interior = dim_transmission or thin_walled
        pm.editorTemplate(dimControl=(nodeName, 'transmission', metal_1))
        pm.editorTemplate(dimControl=(nodeName, 'transmissionColor', dim_transmission))
        pm.editorTemplate(dimControl=(nodeName, 'transmissionDepth', dim_transmission_interior))
        pm.editorTemplate(dimControl=(nodeName, 'transmissionScatter', dim_transmission_interior))
        pm.editorTemplate(dimControl=(nodeName, 'transmissionScatterAnisotropy', dim_transmission_interior))
        pm.editorTemplate(dimControl=(nodeName, 'transmissionDispersion', dim_transmission_interior))
        pm.editorTemplate(dimControl=(nodeName, 'transmissionExtraRoughness', dim_transmission_interior))

        dim_subsurface = metal_1 or transmission_1 or subsurface_0
        dim_subsurface_radius = dim_subsurface or thin_walled
        pm.editorTemplate(dimControl=(nodeName, 'subsurface', metal_1 or transmission_1))
        pm.editorTemplate(dimControl=(nodeName, 'subsurfaceColor', dim_subsurface))
        pm.editorTemplate(dimControl=(nodeName, 'subsurfaceRadius', dim_subsurface_radius))
        pm.editorTemplate(dimControl=(nodeName, 'subsurfaceScale', dim_subsurface_radius))

    def setup(self):
        self.addSwatch()

        self.beginScrollLayout()

        self.addCustom('message', 'AEshaderTypeNew', 'AEshaderTypeReplace')
        
        self.beginLayout("基础", collapse=False)
        self.addControl("base",  label="重量", annotation="Label Weight")
        self.addControl("baseColor", label="颜色", annotation="Label Color")
        self.addSeparator()
        self.addControl("diffuseRoughness", label="漫反射粗糙度", annotation="Diffuse Oren-Nayar Roughness")
        self.addSeparator()
        self.addControl("metalness", label="漫反射强度", annotation="Metalness Mix", changeCommand=self.changeParams)
        self.endLayout()

        self.beginLayout("高光", collapse=False)
        self.addControl("specular",  label="重量", annotation="Specular Weight")
        self.addControl("specularColor", label="颜色", annotation="Specular Color")
        self.addControl("specularRoughness", label="粗糙度", annotation="Specular Roughness")
        self.addSeparator()
        self.addControl("specularIOR", label="IOR", annotation="Specular IOR")
        self.addSeparator()
        self.addControl("specularAnisotropy", label="各向异性", annotation="Specular Anisotropy")
        self.addControl("specularRotation", label="回转", annotation="Specular Anisotropy Rotation")
        self.endLayout()

        self.beginLayout("透射", collapse=True)
        self.addControl("transmission",  label="重量", annotation="Transmission Mix", changeCommand=self.changeParams)
        self.addControl("transmissionColor", label="颜色", annotation="Transmission Color")
        self.addSeparator()
        self.addControl("transmissionDepth", label="深度", annotation="Transmission Depth")
        self.addControl("transmissionScatter", label="扩散", annotation="Transmission Scatter")
        self.addControl("transmissionScatterAnisotropy", label="散射各向异性", annotation="Transmission Scatter Anisotropy")
        self.addSeparator()
        self.addControl("transmissionDispersion", label="分散Abbe", annotation="Transmission Dispersion Abbe Number")
        self.addControl("transmissionExtraRoughness", label="额外粗糙度", annotation="Transmission Extra Roughness")
        self.endLayout()

        self.beginLayout("亚表面", collapse=True)
        self.addControl("subsurface",  label="重量", annotation="Subsurface Scattering Mix", changeCommand=self.changeParams)
        self.addControl("subsurfaceColor", label="颜色", annotation="Subsurface Scattering Color")
        self.addControl("subsurfaceRadius", label="半径", annotation="Subsurface Scattering Radius");
        self.addControl("subsurfaceScale", label="比例", annotation="Subsurface Scattering Scale");
        self.endLayout() 

        self.beginLayout("表层", collapse=True)
        self.addControl("coat",  label="重量", annotation="Coat Weight")
        self.addControl("coatColor", label="颜色", annotation="Coat Color")
        self.addControl("coatRoughness", label="粗糙度", annotation="Coat Roughness")
        self.addSeparator()
        self.addControl("coatIOR", label="IOR", annotation="Coat IOR")
        self.addSeparator()
        self.addControl("coatNormal", label="常规", annotation="Coat Normal")
        self.endLayout()

        self.beginLayout("发射", collapse=True)
        self.addControl("emission",  label="重量", annotation="Emission Weight")
        self.addControl("emissionColor", label="颜色", annotation="Emission Color")
        self.endLayout() 

        self.beginLayout("几何", collapse=True)
        self.addControl("thinWalled", label="薄壁", annotation="Thin Surface with no Interior", changeCommand=self.changeParams) 
        self.addControl("opacity",  label="不透明度", annotation="Cutout Opacity")
        self.addCustom("normalCamera", self.bumpNew, self.bumpReplace)
        self.addControl("tangent", label="各向异性切线", annotation="Tangent Shader")
        self.endLayout()

        self.beginLayout("哑光", collapse=True)
        self.addControl("aiEnableMatte", label="启用哑光")
        self.addControl("aiMatteColor", label="哑光颜色")
        self.addControl("aiMatteColorA", label="哑光不透明度")
        self.endLayout()

        self.beginLayout("高级", collapse=True)
        self.addControl("caustics", label="焦散", annotation="Enable Caustics")
        self.addControl("internalReflections", label="内部反射", annotation="Enable Internal Reflections")
        self.addControl("exitToBackground", label="退出背景", annotation="When bounce depth is exceeded, use background color instead")
        self.addSeparator()
        self.addControl("indirectDiffuse", label="间接漫反射", annotation="Indirect Diffuse")
        self.addControl("indirectSpecular", label="间接高光", annotation="Indirect Specular")
        self.endLayout()
        

        self.beginLayout("硬件纹理", collapse=True)
        pm.mel.eval('AEhardwareTextureTemplate "%s"' % self.nodeName + r'("base_color emission_color ")')
        self.endLayout()
        
        # include/call base class/node attributes
        pm.mel.AEdependNodeTemplate(self.nodeName)

        self.suppress('PhongExponent')
       
        self.addExtraControls()
        self.endScrollLayout()

        # update dimming
        self.changeParams(self.nodeName)

