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

local ICE_TEXTURE_PATH = "assets/arts/materials/textures/model_ice_turbulence.png"
local ICE_TEX_ID = CS.UnityEngine.Shader.PropertyToID("_ice_tex")

function CharacterStateIce:enter()
    self.iceFunc = function()
        local materials = self.characterObj:getCharacterMaterials()
        for _, material in ipairs(materials) do
            material:SetTexture(ICE_TEX_ID, self.iceTexture)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_NONE)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_STONE)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_DISSOLVE)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_GLOW)
            material:EnableKeyword(CharacterFSMManager.KEYWORD_ICE)
        end
    end

    if self.iceTexture then
        self:iceFunc()
    else
        TextureManager:loadTextureAsync(self.characterObj, ICE_TEXTURE_PATH, function(assetPath, texture)
            if self.characterObj then
                if self.iceTexture then
                    TextureManager:unLoadTexture(assetPath)
                else
                    self.iceTexture = texture
                end

                if self.iceFunc then
                    self:iceFunc()
                end
            else
                TextureManager:unLoadTexture(assetPath)
            end
        end)
    end
end

function CharacterStateIce:exit()
    self.iceFunc = nil
end

function CharacterStateIce:clear()
    CharacterStateBase.clear(self)
    if self.iceTexture then
        TextureManager:unLoadTexture(ICE_TEXTURE_PATH)
        self.iceTexture = nil
    end
    self.iceFunc = nil
end

return CharacterStateIce