local CharacterFSMManager = {}

CharacterFSMManager.KEYWORD_NONE = "_EFFECT_MODE_NONE"
CharacterFSMManager.KEYWORD_ICE = "_EFFECT_MODE_ICE"
CharacterFSMManager.KEYWORD_STONE = "_EFFECT_MODE_STONE"
CharacterFSMManager.KEYWORD_GLOW = "_GLOW_ON"
CharacterFSMManager.KEYWORD_DISSOLVE = "_EFFECT_MODE_DISSOLVE"

CharacterFSMManager.STATE_TYPE = {
    DEFAULT = 1,
    STONE = 2,
    ICE = 3,
    GLOW = 4,
    DISSOLVE = 5,
    STONE_2 = 6,
}

function CharacterFSMManager:getFsm(characterObj)
    if self.inactiveFsmList and #self.inactiveFsmList > 0 then
        local fsm = table.remove(self.inactiveFsmList, 1)
        fsm:setCharacterObj(characterObj)
        fsm:restart(CharacterFSMManager.STATE_TYPE.DEFAULT)
        return fsm
    else
        local fsm = self:createNewFsm(characterObj)
        fsm:start(CharacterFSMManager.STATE_TYPE.DEFAULT)
        return fsm
    end
end

function CharacterFSMManager:createNewFsm(characterObj)
    local fsm = require("app/module/character_fsm/character_fsm"):create()

    local state = require("app/module/character_fsm/states/character_state_default"):create()
    fsm:registerState(CharacterFSMManager.STATE_TYPE.DEFAULT, state)

    -- state = require("app/module/character_fsm/states/character_state_stone"):create()
    -- fsm:registerState(CharacterFSMManager.STATE_TYPE.STONE, state)

    -- state = require("app/module/character_fsm/states/character_state_stone2"):create()
    -- fsm:registerState(CharacterFSMManager.STATE_TYPE.STONE_2, state)

    state = require("app/module/character_fsm/states/character_state_ice"):create()
    fsm:registerState(CharacterFSMManager.STATE_TYPE.ICE, state)

    -- state = require("app/module/character_fsm/states/character_state_glow"):create()
    -- fsm:registerState(CharacterFSMManager.STATE_TYPE.GLOW, state)

    state = require("app/module/character_fsm/states/character_state_dissolve"):create()
    fsm:registerState(CharacterFSMManager.STATE_TYPE.DISSOLVE, state)

    fsm:setCharacterObj(characterObj)

    FSMManager:addStateMachine(fsm)
    return fsm
end

function CharacterFSMManager:stopFSM(fsm)
    fsm:stop()
    if not self.inactiveFsmList then
        self.inactiveFsmList = {}
    end
    table.insert(self.inactiveFsmList, fsm)
end

return CharacterFSMManager