# -*- coding: utf-8 -*-
import pymel.core as pm
import mtoa.ui.ae.lightTemplate as lightTemplate
import mtoa.ui.ae.aiSwatchDisplay as aiSwatchDisplay
import mtoa.ui.ae.templates as templates

class AEaiAreaLightTemplate(lightTemplate.LightTemplate):
    def addSwatch(self):
        self.addCustom("message", aiSwatchDisplay.aiSwatchDisplayNew, aiSwatchDisplay.aiSwatchDisplayReplace)

    def makeLightExclusive(self, attr):
        lightName = attr.split(".")[0]
        pm.rowLayout(nc=2, cal=[2, 'left'])
        pm.text(label="")
        pm.exclusiveLightCheckBox('exclusiveButton', light=lightName, label="默认照明")
        pm.setParent('..')
        
    def replaceLightExclusive(self, attr):
        lightName = attr.split(".")[0]
        pm.exclusiveLightCheckBox('exclusiveButton', edit=True, light=lightName)
    
    def setup(self):        
        self.addSwatch()

        self.beginScrollLayout()

        self.beginLayout("Arnold区域光属性", collapse=False)

        self.addControl("color", label = "颜色")
        self.addControl("intensity", label = "强度")
        self.addControl("aiExposure", label = "曝光")
        self.addSeparator()        
        self.setupColorTemperature("ArnoldArea")
        self.addCustom("instObjGroups", self.makeLightExclusive, self.replaceLightExclusive)
        
        self.addChildTemplate('aiTranslator', templates.getNodeTemplate('aiAreaLight'))
        
        self.addSeparator()

        self.commonLightAttributes()
        
        self.endLayout()

        # include/call base class/node attributes
        pm.mel.AEdependNodeTemplate(self.nodeName)

        self.addExtraControls()
        suppressList = ['aiShadowDensity', 'aiCastShadows', 'update',
            'aiSamples', 'aiNormalize', 'aiColorTemperature',
            'aiShadowColor', 'aiResolution', 'ghostFrames',
            'motionBlur', 'visibleInReflections', 'visibleInRefractions',
            'castsShadows', 'receiveShadows', 'maxVisibilitySamplesOverride',
            'maxVisibilitySamples', 'geometryAntialiasingOverride', 'antialiasingLevel',
            'shadingSamplesOverride', 'shadingSamples', 'maxShadingSamples',
            'volumeSamplesOverride', 'volumeSamples', 'layerRenderable',
            'ghostingControl', 'ghostCustomSteps', 'ghostColorPreA',
            'ghostColorPre', 'ghostColorPostA', 'ghostColorPost',
            'ghostRangeStart', 'ghostRangeEnd', 'ghostDriver',
            'depthJitter', 'ignoreSelfShadowing', 'primaryVisibility',
            'localPosition', 'localScale', 'pointCamera', 'normalCamera',
            'visibility', 'intermediateObject', 'template', 'ghosting',
            'objectColorRGB', 'useObjectColor', 'objectColor',
            'containerType', 'creationDate', 'creator',
            'customTreatment', 'uiTreatment', 'templateVersion',
            'viewMode', 'iconName', 'viewName', 'templatePath',
            'rmbCommand', 'blackBox', 'drawOverride',
            'renderInfo', 'renderLayerInfo', 'compInstObjGroups',
            'lodVisibility', 'templateName', 'selectionChildHighlighting']
        for sup in suppressList:
            self.suppress(sup)
        self.endScrollLayout()

class BaseAreaLightTemplate(lightTemplate.LightTemplate):
    def addCommonParameters(self):
        self.addControl("aiSamples", label = "采样")
        self.addControl("aiNormalize", label = "统一化")

        self.addSeparator()

        self.addControl("aiCastShadows", label = "投射阴影")
        self.addControl("aiShadowDensity", label = "阴影密度")
        self.addControl("aiShadowColor", label = "阴影颜色")

    def setup(self):
        self.addCommonParameters()       
        
class QuadAreaLightTemplate(BaseAreaLightTemplate):
    def setup(self):
        self.addControl("aiSpread", label = "扩散")
        self.addControl("aiResolution", label = "分辨率")
        self.addControl("aiRoundness", label = "圆度")
        self.addControl("aiSoftEdge", label = "软边")
        #self.addControl("aiPortal")  hiding it as we now have a dedicated light portal node
        self.addSeparator()
        self.addCommonParameters()

class DiskAreaLightTemplate(BaseAreaLightTemplate):
    def setup(self):
        self.addControl("aiSpread")
        self.addSeparator()
        self.addCommonParameters()

class MeshLightTemplate(BaseAreaLightTemplate):
    def setup(self):
        self.addControl("color")
        self.addControl("intensity")
        self.addControl("aiExposure", label = "曝光")
        self.addSeparator()        
        self.setupColorTemperature("ArnoldArea")
        
        self.addSeparator()
        self.addControl("lightVisible")
        
        self.addSeparator()
        
        self.addCommonParameters()

        self.addSeparator()

        self.commonLightAttributes(False)

        self.beginLayout('Subdivision', collapse=True)
        self.addControl("aiSubdivType", label="类型")
        self.addControl("aiSubdivIterations", label="迭代")
        self.addControl("aiSubdivAdaptiveMetric", label="自适应度量")
        self.addControl("aiSubdivPixelError", label="自适应错误")
        self.addControl("aiSubdivAdaptiveSpace", label="自适应空间")
        # TODO: add dicing camera UI
        #self.addControl("aiSubdivDicingCamera", label="Dicing Camera")
        self.addControl("aiSubdivUvSmoothing", label="UV平滑")
        self.addControl("aiSubdivSmoothDerivs", label="平滑切线")
        self.endLayout()

        self.addControl("aiUserOptions", "User Options")


templates.registerAETemplate(templates.TranslatorControl, "aiAreaLight", label="灯光形状")
templates.registerTranslatorUI(QuadAreaLightTemplate, "aiAreaLight", "quad")
templates.registerTranslatorUI(BaseAreaLightTemplate, "aiAreaLight", "cylinder")
templates.registerTranslatorUI(DiskAreaLightTemplate, "aiAreaLight", "disk")
templates.registerDefaultTranslator('aiAreaLight', "quad")
templates.registerTranslatorUI(MeshLightTemplate, "mesh", "mesh_light")
