require "behaviours/chaseandattack"
require "behaviours/runaway"
require "behaviours/wander"
require "behaviours/doaction"
require "behaviours/attackwall"
require "behaviours/panic"
require "behaviours/minperiod"


local SEE_DIST = 40

local CHASE_DIST = 32
local CHASE_TIME = 20

local function BaseDestroy(inst)
    if inst.components.knownlocations:GetLocation("targetbase") then
    	local target = FindEntity(inst, SEE_DIST, function(item) 
    		if item.components.workable
    		   and item.components.workable.action == ACTIONS.HAMMER
    		   and not item:HasTag("wall") then
    				return true
    			end
    		end)
    	if target then
    		return BufferedAction(inst, target, ACTIONS.HAMMER)
    	end
    end
end

local function GoNearPlayer(inst)
	if inst.components.knownlocations:GetLocation("playerpos") then
        return BufferedAction(inst, nil, ACTIONS.GOHOME, nil, inst.components.knownlocations:GetLocation("playerpos"))
	elseif inst.components.knownlocations:GetLocation("leavepos") then
        return BufferedAction(inst, nil, ACTIONS.GOHOME, nil, inst.components.knownlocations:GetLocation("leavepos"))
    end
end

local function LeavePlanet(inst)
	if inst.components.knownlocations:GetLocation("leavepos") then
        return BufferedAction(inst, nil, ACTIONS.GOHOME, nil, inst.components.knownlocations:GetLocation("leavepos"))
    end
end


local function GetWanderPos(inst)
    if inst.components.knownlocations:GetLocation("targetbase") then
        return inst.components.knownlocations:GetLocation("targetbase")
	elseif inst.components.knownlocations:GetLocation("home") then
		return inst.components.knownlocations:GetLocation("home")
	elseif inst.components.knownlocations:GetLocation("spawnpoint") then
		return inst.components.knownlocations:GetLocation("spawnpoint")
	end
end

local AlienShipBrain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)

function AlienShipBrain:OnStart()

    local clock = GetClock()

    local root =
        PriorityNode(
        {
			--AttackWall(self.inst),
            --ChaseAndAttack(self.inst, CHASE_TIME, CHASE_DIST),
            --DoAction(self.inst, function() return BaseDestroy(self.inst) end, "DestroyBase", true),
            -- WhileNode(function() return self.inst.components.knownlocations:GetLocation("playerpos") end, "KnowPlayerPos",
			--	DoAction(self.inst, function() return GoNearPlayer(self.inst) end, "GoHome", true)),
			--WhileNode(function() return self.inst.components.knownlocations:GetLocation("leavepos") end, "KnowPlayerPos",
				DoAction(self.inst, function() return GoNearPlayer(self.inst) end, "GoHome", true),--),
            --Wander(self.inst, GetWanderPos, 30, {minwwwalktime = 10}),
			--Wander(self.inst, function() return self.inst.components.knownlocations:GetLocation("home") end, 25)  
        },1)
    
    self.bt = BT(self.inst, root)
         
end

function AlienShipBrain:OnInitializationComplete()
    self.inst.components.knownlocations:RememberLocation("spawnpoint", Point(self.inst.Transform:GetWorldPosition()))
end

return AlienShipBrain