require("stategraphs/commonstates")

local events=
{


	EventHandler("locomote", function(inst)
        local is_moving = inst.sg:HasStateTag("moving")
        local is_running = inst.sg:HasStateTag("running")
        local is_idling = inst.sg:HasStateTag("idle")
        
        local should_move = inst.components.locomotor:WantsToMoveForward()
        local should_run = inst.components.locomotor:WantsToRun()
        if is_moving and not should_move then
            if is_running then
                inst.sg:GoToState("run_stop")
            else
                inst.sg:GoToState("walk_stop")
            end
        elseif (is_idling and should_move) or (is_moving and should_move and is_running ~= should_run) then
            if should_run then
            	if inst.sg:HasStateTag("empty") then
            		inst.sg:GoToState("spawn")
            	else
                	inst.sg:GoToState("run_start")
            	end
            else
                inst.sg:GoToState("walk_start")
            end
        end
    end)
    -- EventHandler("locomote", function(inst)
    --     local is_moving = inst.sg:HasStateTag("moving")
    --     local is_idling = inst.sg:HasStateTag("idle")
        
    --     local should_move = inst.components.locomotor:WantsToMoveForward()
    --     if is_moving and not should_move then
    --         inst.sg:GoToState("walk_stop")
    --     elseif (is_idling and should_move) then
    --     	if inst.sg:HasStateTag("empty") then
    --     		inst.sg:GoToState("spawn")
    --     	else
    --         	inst.sg:GoToState("walk_start")
    --         end
    --     end
    -- end)
}

local function destroystuff(inst)
    local pt = inst:GetPosition()
    local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, 4)
    for k,v in pairs(ents) do
        if v and v.components.workable and v.components.workable.workleft > 0 and v.prefab =="evergreen" or v.prefab =="evergreen_sparse" or 
		v.prefab =="marsh_tree" or v.prefab =="deciduoustree" or v.prefab =="deciduoustree_tall" or v.prefab =="deciduoustree_short" or v.prefab =="mushtree_tall" 
		 or v.prefab =="mushtree_medium" or v.prefab =="mushtree_small" or v.prefab =="deciduoustree_short" or v.prefab =="rock1" or v.prefab =="rock1" 
		 or v.prefab =="rock2" or v.prefab =="rock_flintless" or v.prefab =="stalagmite_full" or v.prefab =="stalagmite_med" or v.prefab =="stalagmite_low" 
		 or v.prefab =="stalagmite_tall_full" or v.prefab =="stalagmite_tall_med" or v.prefab =="stalagmite_tall_low" or v.prefab =="rock_ice" or
		 v.prefab =="marbletree" or v.prefab =="rock_petrified_tree_short" or v.prefab =="rock_petrified_tree_tall" or v.prefab =="rock_petrified_tree_old" 
		 or v.prefab =="marbleshrub_tall" or v.prefab =="marbleshrub_tall" or v.prefab =="marbleshrub_tall" 
		  or v.prefab =="marbleshrub_short" or v.prefab =="marbleshrub_short" or v.prefab =="marbleshrub_short" 
		   or v.prefab =="marbleshrub_old" or v.prefab =="marbleshrub_old" or v.prefab =="marbleshrub_old" 
		then
            SpawnPrefab("collapse_small").Transform:SetPosition(v:GetPosition():Get())        
            v.components.workable:Destroy(inst)
        end

        if v and v.components.health and not v.components.health:IsDead()
        and v ~= inst.WINDSTAFF_CASTER and v.components.combat and not v:HasTag("player") then
        	v.components.combat:GetAttacked(inst, 25)
        	v.components.combat:SuggestTarget(inst.WINDSTAFF_CASTER)
    		end
    	if v.components.burnable then 
			-- Extinguish smoldering/fire and reset the propagator to a heat of .2
			v.components.burnable:Extinguish(true, 0) 
		end
    end
end

local states=
{

	State
	{
		name = "empty",
		tags = {"idle", "empty"},

		onenter = function(inst)
			inst.Physics:Stop()
			inst.AnimState:PlayAnimation("empty")
		end,
	},

	State
	{
		name = "idle",
		tags = {"idle"},

		onenter = function(inst)
			inst.Physics:Stop()
			inst.AnimState:PushAnimation("tornado_loop", false)
			destroystuff(inst)
		end,

		onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,

		events = 
		{
			EventHandler("animqueueover", function(inst) 
				inst.sg:GoToState("idle") 
			end)
		},
	},
	
	State
	{
		name = "spawn",
		tags = {"moving", "canrotate"},

		onenter = function(inst)
            inst.components.locomotor:RunForward()
			inst.AnimState:PlayAnimation("tornado_pre")
		end,

		onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,

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

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

		onenter = function(inst)
			inst.Physics:Stop()
			inst.AnimState:PlayAnimation("tornado_pst")
		end,

		events = 
		{
			EventHandler("animover", function(inst) 
				inst:Remove()
			end)
		},
	},

	State
	{
		name = "walk_start",
		tags = {"moving", "canrotate"},

		onenter = function(inst)
			inst.sg:GoToState("walk")
		end,

		onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,
	},

	State
	{
		name = "walk",
		tags = {"moving", "canrotate"},

		onenter = function(inst)
            inst.components.locomotor:WalkForward()
			inst.AnimState:PushAnimation("tornado_loop", false)
			destroystuff(inst)
		end,

		onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,

		timeline =
		{
			TimeEvent(5*FRAMES, destroystuff),
		},

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

	State
	{
		name = "walk_stop",
		tags = {"canrotate"},

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

		onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,
	},

   State{
        name = "run_start",
        tags = {"moving", "running", "canrotate"},
        
        onenter = function(inst) 
            inst.components.locomotor:RunForward()
            inst.AnimState:PushAnimation("tornado_loop", false)
        end,

        onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,

		timeline =
		{
			TimeEvent(5*FRAMES, destroystuff),
		},

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

    State{            
        name = "run",
        tags = {"moving", "running", "canrotate"},
        
        onenter = function(inst) 
            inst.components.locomotor:RunForward()
            inst.AnimState:PushAnimation("tornado_loop", false)
        end,

        onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,

		timeline =
		{
			TimeEvent(5*FRAMES, destroystuff),
		},
        
        events=
        {   
            EventHandler("animqueueover", function(inst) inst.sg:GoToState("run") end ),        
        }, 
    },
        
	State{
    
        name = "run_stop",
        tags = {"idle"},
        
        onenter = function(inst) 
            inst.components.locomotor:StopMoving()
            inst.AnimState:PushAnimation("tornado_loop", false)
        end,

        onupdate = function(inst)
		if not inst.components.timer:TimerExists("lightning") then
               --inst.components.timer:StartTimer("lightning", math.random(2.5))
            end
        end,
        
		timeline =
		{
			TimeEvent(5*FRAMES, destroystuff),
		},

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

}
  
return StateGraph("tornado", states, events, "empty")