require("stategraphs/commonstates")

local actionhandlers =
{
	ActionHandler(ACTIONS.EAT,

        function(inst, action)
		if inst.components.eater and action.target and action.target.components.edible then
			inst.components.eater:Eat(action.target)
		end
			return"doshortaction"
        end),
    ActionHandler(ACTIONS.PICKUP,
        function(inst, action)

		inst:PushEvent("onpickup", {item = action.target})
		if action.target and action.target.components.pickupable then
			action.target.components.pickupable:OnPickup(inst)
		end
            return "doshortaction"
        end),
    ActionHandler(ACTIONS.DROP,
        function(inst, action)
		if inst.components.inventory then
		local wholestack = action.options.wholestack
		if action.invobject and action.invobject.components.stackable and action.invobject.components.stackable.forcedropsingle then wholestack = false end
		inst.components.inventory:DropItem(action.invobject, wholestack, false, action.pos)
		end
--            return "doshortaction"
        end),


--[[
    ActionHandler(ACTIONS.PICK, 
        function(inst, action)
            if action.target.components.pickable then
                if action.target.components.pickable.quickpick then
                    return "hit"
                else
                    return "hit"--³¤Ê±¼ä
                end
            end
        end),
]]
}

local events=
{
    EventHandler("attacked", function(inst) if not inst.components.health:IsDead() and not inst.sg:HasStateTag("attack") then inst.sg:GoToState("hit") end end),
    EventHandler("death", function(inst) inst.sg:GoToState("death") end),
    EventHandler("doattack", function(inst, data) if not inst.components.health:IsDead() and (inst.sg:HasStateTag("hit") or not inst.sg:HasStateTag("busy")) then inst.sg:GoToState("attack", data.target) end end),
    CommonHandlers.OnLocomote(true,false),
}

local states=
{

    State{
        name = "idle",
        tags = {"idle", "canrotate"},
        
        onenter = function(inst, playanim)
            inst.Physics:Stop()
            if playanim then
                inst.AnimState:PlayAnimation(playanim)
                inst.AnimState:PushAnimation("idle", true)
            else
                inst.AnimState:PlayAnimation("idle", true)
            end
--			inst.sg:SetTimeout(1*math.random()+.5)
        end,

--			ontimeout = function(inst) inst.sg:GoToState("idle") end,

        events=
        {
            EventHandler("animover", function(inst) inst.sg:GoToState("idle") end),
        },
   },

    State{
        name = "attack",
        tags = {"attack", "busy"},

        onenter = function(inst, target)
            inst.sg.statemem.target = target
            inst.Physics:Stop()
            inst.components.combat:StartAttack()
            inst.AnimState:PlayAnimation("atk", true)
            inst.sg:SetTimeout(0.5)
        end,

        timeline=
        {
			TimeEvent(5*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve_DLC001/creatures/catcoon/attack") end),
            TimeEvent(10*FRAMES, function(inst) inst.components.combat:DoAttack(inst.sg.statemem.target) end),
        },
        ontimeout = function(inst) inst.sg:GoToState("idle") end,
        events=
        {
        },
    },

	State{
		name = "hit",
        tags = {"busy", "hit"},

        onenter = function(inst, cb)
            inst.Physics:Stop()
            inst.AnimState:PlayAnimation("hit")
            inst.sg:SetTimeout(0.33)
        end,
        ontimeout = function(inst) inst.sg:GoToState("idle") end,
        events=
        {
--			EventHandler("animover", function(inst) inst.sg:GoToState("idle") end),
        },
    },

    State{
            name = "run_start",
            tags = {"moving", "running", "atk_pre", "canrotate"},

            onenter = function(inst) 
                    inst.components.locomotor:RunForward()
                    inst.AnimState:PlayAnimation("run_pre")
					inst.sg:SetTimeout(0.4)
            end,
        ontimeout = function(inst) inst.sg:GoToState("run") end,
            events = {   },
        },
        
    State{
            
            name = "run",
            tags = {"moving", "running", "canrotate"},
            
            onenter = function(inst) 
                inst.components.locomotor:RunForward()
                inst.AnimState:PlayAnimation("run_loop")
            end,

            timeline=
            {
            TimeEvent(40*FRAMES, function(inst) PlayFootstep(inst) end),
            TimeEvent(80*FRAMES, function(inst) PlayFootstep(inst) end),
            },

            events=
            {
				EventHandler("animover", function(inst) inst.sg:GoToState("run") end ),        
            },
        },        
    
    State{
            
            name = "run_stop",
            tags = {"canrotate"},
            
            onenter = function(inst) 
                inst.components.locomotor:StopMoving()
                local should_softstop = false
                inst.AnimState:PlayAnimation("run_pst")          
					inst.sg:SetTimeout(0.1)
            end,
        ontimeout = function(inst) inst.sg:GoToState("idle") end,

            events=
            {
            },
        },

    State{
        name = "death",
        tags = {"busy"},

        onenter = function(inst)
			inst.SoundEmitter:PlaySound("dontstarve/creatures/hound/death")
            inst.AnimState:PlayAnimation("death")
            inst.Physics:Stop()
            RemovePhysicsColliders(inst)
--			inst.components.lootdropper:DropLoot(Vector3(inst.Transform:GetWorldPosition()))            
        end,

    },

    State{
        name = "eat",
        
        onenter = function(inst)
            inst.Physics:Stop()
			inst.SoundEmitter:PlaySound("dontstarve/wilson/eat", "eating")    
            inst.AnimState:PushAnimation("hit", true)
            inst.sg:SetTimeout(0.5)
        end,
        
        ontimeout = function(inst)
            inst:PerformBufferedAction()
            inst.sg:GoToState("idle")
        end,

        onexit = function(inst)
            inst:ClearBufferedAction()
            inst.SoundEmitter:KillSound("eating")    
        end,
    },   

    State{
        name = "doshortaction",
        tags = {"doing", "busy"},
        
        onenter = function(inst)
            inst.components.locomotor:Stop()
            inst.AnimState:PlayAnimation("atk")
            inst.sg:SetTimeout(6*FRAMES)
        end,
        
        timeline=
        {
            TimeEvent(4*FRAMES, function( inst )
                inst.sg:RemoveStateTag("busy")
            end),
            TimeEvent(10*FRAMES, function( inst )
            inst.sg:RemoveStateTag("doing")
            inst.sg:AddStateTag("idle")
            end),
        },
        
        ontimeout = function(inst)
            inst:PerformBufferedAction()
        end,
        
        events=
        {
            EventHandler("animover", function(inst) if inst.AnimState:AnimDone() then inst.sg:GoToState("idle") end end ),
        },
    },
	
}

--[[
CommonStates.AddWalkStates( states,
{
	starttimeline =
	{
        TimeEvent(7*FRAMES, DeerclopsFootstep),
	},
    walktimeline = 
    { 
        TimeEvent(23*FRAMES, DeerclopsFootstep),
        TimeEvent(42*FRAMES, DeerclopsFootstep),
    },
    endtimeline=
    {
        TimeEvent(5*FRAMES, DeerclopsFootstep),
    },
})
]]

return StateGraph("cat_yuyi", states, events, "idle", actionhandlers)
