local ZUIMODPanel = {}

local function OnBlendSliderValueChange(skinnedMeshCom, nBlendShapeIndex, nValue)
    skinnedMeshCom:SetBlendShapeWeight(nBlendShapeIndex, nValue * 100)
end

local function GetAllBalndShapes()
    local boyObject         = CS.UnityEngine.GameObject.Find("Boy").gameObject
    local tSkinnedMeshCom   = boyObject.transform:GetComponentsInChildren(typeof(CS.UnityEngine.SkinnedMeshRenderer))
    local prefabSliderItem  = LoadResource("Prefabs/UI/MODSliderItem.prefab")
    local scrollViewCon     = ZUIMODPanel.rootPanel.transform:Find("ScrollView/Viewport/Content").gameObject

    for nSkinnedMeshIndex = 0, tSkinnedMeshCom.Length - 1, 1 do
        local sharedMesh = tSkinnedMeshCom[nSkinnedMeshIndex].sharedMesh

        for nBlendShapeIndex = 0, sharedMesh.blendShapeCount - 1, 1 do
            local szBlendShapeName = sharedMesh:GetBlendShapeName(nBlendShapeIndex)
            local sliderItem        = CS.UnityEngine.GameObject.Instantiate(prefabSliderItem, scrollViewCon.transform)
            local slider            = sliderItem.transform:Find("Slider").gameObject

            -- 读取blendShapeName作为每个blendShape的标题(后续可修改)
            local sliderItemTextCom = sliderItem.transform:Find("Text"):GetComponent("Text")
            sliderItemTextCom.text  = szBlendShapeName

            -- slider添加监听事件
            local sliderCom         = slider.transform:GetComponent("Slider")
            sliderCom.onValueChanged:AddListener(function (nValue)
                OnBlendSliderValueChange(tSkinnedMeshCom[nSkinnedMeshIndex], nBlendShapeIndex, nValue)
            end)
        end
    end
end

function ZUIMODPanel.Awake(luaRoot)
    ZUIMODPanel._luaRoot = luaRoot

    local canvas = CS.UnityEngine.GameObject.Find("Canvas")
    ZUIMODPanel._rootCanvas = canvas

    local prefabMOD = LoadResource("Prefabs/UI/ZUIMODPanel.prefab")
    local rootPanel = CS.UnityEngine.GameObject.Instantiate(prefabMOD, canvas.transform)
    ZUIMODPanel.rootPanel = rootPanel
end

function ZUIMODPanel.Start()
    GetAllBalndShapes()
end

function ZUIMODPanel.OnDestroy()
    CS.UnityEngine.GameObject.Destroy(ZUIMODPanel.rootPanel)
end

return ZUIMODPanel