local CharacterFSMManager = require "app/module/character_fsm/character_fsm_manager"
local CharacterStateBase = require "app/module/character_fsm/states/character_state_base"
local CharacterStateGlow = class("CharacterStateIce", CharacterStateBase)

local GLOW_COLOR_ID = CS.UnityEngine.Shader.PropertyToID("_glow_color")

function CharacterStateGlow:enter(params)
    local materials = self.characterObj:getCharacterMaterials()
    for _, material in ipairs(materials) do
        material:EnableKeyword(CharacterFSMManager.KEYWORD_GLOW)
        material:DisableKeyword(CharacterFSMManager.KEYWORD_ICE)
        material:DisableKeyword(CharacterFSMManager.KEYWORD_DISSOLVE)
    end

    local time = params.time or 0.7  -- 总时长
    local time1 = time * 0.2  -- 第一段时长
    local time2 = time * 0.5  -- 第二段时长
    local time3 = time - time1 - time2  -- 第三段时长

    self.sequence = self.characterObj:createBindTweenSequence()
    self.sequence:SetIntId(GConst.DOTWEEN_IDS.CHARACTER_EFFECT)
    local progress = 0.3
    local tween = CS.DG.Tweening.DOTween.To(
            function()
                return progress
            end,
            function(x)
                progress = x
                if not self.characterObj or self.characterObj:isDestroyed() then
                    return
                end
                local color = BF.Color(progress, 0, 0, 1)
                local renders = self.characterObj:getCharacterMeshRenderer()
                for _, render in ipairs(renders) do
                    local mpb = self.characterObj:getMaterialPropertyBlock()
                    render:GetPropertyBlock(mpb)
                    mpb:SetColor(GLOW_COLOR_ID, color)
                    render:SetPropertyBlock(mpb)
                end
            end, 1, time1)
    tween:SetEase(CS.DG.Tweening.Ease.InOutCirc)
    self.sequence:Append(tween)

    self.sequence:AppendInterval(time2)

    tween = CS.DG.Tweening.DOTween.To(
            function()
                return progress
            end,
            function(x)
                progress = x
                if not self.characterObj or self.characterObj:isDestroyed() then
                    return
                end
                local color = BF.Color(progress, 0, 0, 1)
                local renders = self.characterObj:getCharacterMeshRenderer()
                for _, render in ipairs(renders) do
                    local mpb = self.characterObj:getMaterialPropertyBlock()
                    render:GetPropertyBlock(mpb)
                    mpb:SetColor(GLOW_COLOR_ID, color)
                    render:SetPropertyBlock(mpb)
                end
            end, 0.3, time3)
    tween:SetEase(CS.DG.Tweening.Ease.InOutCirc)
    self.sequence:Append(tween)

    self.sequence:OnComplete(function()
        self.sequence = nil
        if params.callback then
            params.callback()
        end
    end)
    self.sequence.timeScale = self.characterObj.timeScale
end

function CharacterStateGlow:exit()
    if self.sequence then
        self.sequence:Kill()
        self.sequence = nil
    end
end

function CharacterStateGlow:clear()
    CharacterStateBase.clear(self)
    if self.sequence then
        self.sequence:Kill()
        self.sequence = nil
    end
end

return CharacterStateGlow