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

local STONE_TEXTURE_PATH = "assets/arts/effects/common/textures/nature/stone_010.png"
local STONE_RANGE_ID = CS.UnityEngine.Shader.PropertyToID("_range")

function CharacterStateStone2:enter(params)
    self.stoneFunc = function()
        local materials = self.characterObj:getCharacterMaterials()
        for _, material in ipairs(materials) do
            material:SetTexture("_stone", self.stoneTexture)
            material:SetFloat(STONE_RANGE_ID, 0.435)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_NONE)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_ICE)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_DISSOLVE)
            material:DisableKeyword(CharacterFSMManager.KEYWORD_GLOW)
            material:EnableKeyword(CharacterFSMManager.KEYWORD_STONE)
        end

        if params and params.callback then
            params.callback(true)
        end
    end

    if self.stoneTexture then
        self:stoneFunc()
    else
        TextureManager:loadTextureAsync(self.characterObj, STONE_TEXTURE_PATH, function(assetPath, texture)
            if self.characterObj then
                if self.stoneTexture then
                    TextureManager:unLoadTexture(assetPath)
                else
                    self.stoneTexture = texture
                end

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

                if params and params.callback then
                    params.callback(false)
                end
            end
        end)
    end
end

function CharacterStateStone2:exit()
    self.stoneFunc = nil
end

function CharacterStateStone2:clear()
    CharacterStateBase.clear(self)
    if self.stoneTexture then
        TextureManager:unLoadTexture(STONE_TEXTURE_PATH)
        self.stoneTexture = nil
    end
    self.stoneFunc = nil
end

return CharacterStateStone2