--[[
 -- @Description:
 -- Hello , DiaoSi ~ ( or not ? ) ,glad you saw my comment
 -- If you think my mod is okay , you can sponsor my breakfast by a tiny amount donation  ($_$)
 -- paypal:td1madao@163.com     alipay:td1madao@163.com
 -- If any question ,Please email me
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- @version 1.0.0
 -- @author td1madao
 -- @email td1madao@163.com
 -- @qq 360810498
 -- @date 16/8/6
 ]]
local TD = TD.td1madao.rivendst
local rivendst_player_mouse_listener = Class(function(self, inst)
    TD.see()
    self.inst = inst
    if not TD.smart() then
        local player = self.inst
        local taskName = "actionpicker"
        TD.getC(player, "rivendst_common_task_manager"):player_period(1, function()
            if player.components.playeractionpicker then
                local old = player.components.playeractionpicker.DoGetMouseActions
                local new = function(self, position, target)
                    local action
                    local second_action
                    local success = false
                    TD.try(function()
                        action, second_action = old(self, position, target) -- 客户端出错啦
                        success = true
                    end)
                    if not success then
                        return nil, nil
                    end
                    local useTool = self.inst.replica.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
                    -- local useTool = TD.getC(player, "rivendst_common_synchronizer"):player_get("hasWeapon")
                    if not useTool then
                        return action, second_action
                    end
                    local skill = TD.getC(player, "rivendst_common_skill_manager").skill
                    local buffonTag = false
                    for k, v in pairs(skill) do
                        if TD.getC(player, "rivendst_common_skill_manager").skillBuffer[k].tag == true then
                            buffonTag = k
                            break
                        end
                    end
                    if not buffonTag then
                        return action, second_action
                    end
                    local actionName = string.upper("rivendstcast" .. buffonTag)
                    if not ACTIONS[actionName] then
                        return action, second_action
                    end
                    second_action = BufferedAction(inst, nil, ACTIONS[string.upper("rivendstcancel")])
                    local x, y, z = (TheInput:GetWorldPosition() or Vector3(position.x, 0, position.z)):Get()
                    if not TD.canMoveTo(player, x, 0, z) then
                        return action, second_action
                    end
                    action = BufferedAction(inst, nil, ACTIONS[actionName])
                    return action, second_action
                end
                inst.components.playeractionpicker.DoGetMouseActions = new
                TD.getC(player, "rivendst_common_task_manager"):player_clean(taskName)
            end
        end, taskName, 60)
    end
end)
return rivendst_player_mouse_listener