local assets =
{
	Asset("ANIM", "anim/ancient_chest.zip"),	
	Asset("ANIM", "anim/ui_chest_5x5.zip"),
}

local function OnGetItemFromPlayer(inst, giver, item)
	local ancientchest = SpawnPrefab("ancient_chest_unlocked")
	ancientchest.Transform:SetPosition(inst:GetPosition():Get())
	ancientchest:AddComponent("scenariorunner")
	ancientchest.components.scenariorunner:SetScript("chest_fortress")
	ancientchest.components.scenariorunner:Run()
	ancientchest.openedseason = GetSeasonManager():GetSeason()
    inst.SoundEmitter:PlaySound("dontstarve/common/ghost_spawn")
	local fx = SpawnPrefab("statue_transition_2")
	if fx then
		fx.Transform:SetPosition(inst:GetPosition():Get())
		fx.AnimState:SetScale(1,2,1)
	end
	fx = SpawnPrefab("statue_transition")
	if fx then
		fx.Transform:SetPosition(inst:GetPosition():Get())
		fx.AnimState:SetScale(1,1.5,1)
	end
	if giver.components.sanity then
		giver.components.sanity:DoDelta(50)
	end
	inst:DoTaskInTime(1.5, function(inst) 
		GetPlayer().components.talker:Say("It's now unlocked!")  
		inst:Remove()
	end)
end

local function ShouldAcceptItem(inst, item)
	if item.prefab == "fortress_key" then   
		return true
    end
end

local function OnRefuseItem(inst, item)
	GetPlayer().components.talker:Say("I don't think this gonna work.")
end

local function chestlocked(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	
	inst.AnimState:SetBank("dragonfly_chest")
	inst.AnimState:SetBuild("ancient_chest")
	inst.AnimState:PlayAnimation("closed")
	inst.Transform:SetScale(2.0, 2.0, 2.0)
	
	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon("ancient_chest.tex")
	
	inst:AddTag("chest")	
		
	inst:AddComponent("inspectable")
	inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader.onrefuse = OnRefuseItem
    return inst
end

local slotpos = {}

for y = 4, 0, -1 do
	for x = 0, 4 do
		table.insert(slotpos, Vector3(80*x-80*2, 80*y-80*2,0))
	end
end

local function onopen(inst) 
	inst.AnimState:PlayAnimation("open") 
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
end 

local function onclose(inst)
	inst.AnimState:PlayAnimation("close") 
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
end

local function ReLock(inst)
	local ancientchest = SpawnPrefab("ancient_chest_locked")
	ancientchest.Transform:SetPosition(inst:GetPosition():Get())
	inst.SoundEmitter:PlaySound("dontstarve/common/ghost_spawn")
	local fx = SpawnPrefab("statue_transition_2")
	if fx then
		fx.Transform:SetPosition(inst:GetPosition():Get())
		fx.AnimState:SetScale(1,2,1)
	end
	fx = SpawnPrefab("statue_transition")
	if fx then
		fx.Transform:SetPosition(inst:GetPosition():Get())
		fx.AnimState:SetScale(1,1.5,1)
	end
	for k = 1,math.random(2,4) do	
		local pt = Vector3(inst.Transform:GetWorldPosition())
		local theta = math.random() * 2 * PI
		local radius = math.random(2,8)
		local offset = FindWalkableOffset(pt, theta, radius, 12, true)
		if offset then
			local pos = pt + offset
			local ground = GetWorld()
			local tile = GROUND.GRASS
			if ground and ground.Map then
				tile = inst:GetCurrentTileType(pos:Get())
			end
			local guard = SpawnPrefab("knight_nightmare")
			if guard then
				guard.Physics:Teleport(pos:Get())			
				fx = SpawnPrefab("statue_transition")
				if fx then
					fx.Transform:SetPosition(guard:GetPosition():Get())
					fx.AnimState:SetScale(1,1.5,1)
				end	
			end
		end	
	end
	for k = 1,math.random(1,3) do	
		local pt = Vector3(inst.Transform:GetWorldPosition())
		local theta = math.random() * 2 * PI
		local radius = math.random(2,8)
		local offset = FindWalkableOffset(pt, theta, radius, 12, true)
		if offset then
			local pos = pt + offset
			local ground = GetWorld()
			local tile = GROUND.GRASS
			if ground and ground.Map then
				tile = inst:GetCurrentTileType(pos:Get())
			end
			local guard = SpawnPrefab("bishop_nightmare")
			if guard then
				guard.Physics:Teleport(pos:Get())
				fx = SpawnPrefab("statue_transition")
				if fx then
					fx.Transform:SetPosition(guard:GetPosition():Get())
					fx.AnimState:SetScale(1,1.5,1)
				end
			end
		end	
	end
	for k = 1,math.random(1,2) do	
		local pt = Vector3(inst.Transform:GetWorldPosition())
		local theta = math.random() * 2 * PI
		local radius = math.random(2,8)
		local offset = FindWalkableOffset(pt, theta, radius, 12, true)
		if offset then
			local pos = pt + offset
			local ground = GetWorld()
			local tile = GROUND.GRASS
			if ground and ground.Map then
				tile = inst:GetCurrentTileType(pos:Get())
			end
			local guard = SpawnPrefab("rook_nightmare")
			if guard then
				guard.Physics:Teleport(pos:Get())
				fx = SpawnPrefab("statue_transition")
				if fx then
					fx.Transform:SetPosition(guard:GetPosition():Get())
					fx.AnimState:SetScale(1,1.5,1)
				end
			end
		end	
	end
	inst:Remove()
end

local function chestunlocked(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	
	inst.AnimState:SetBank("dragonfly_chest")
	inst.AnimState:SetBuild("ancient_chest")
	inst.AnimState:PlayAnimation("closed")
	inst.Transform:SetScale(2.0, 2.0, 2.0)
	
	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon("ancient_chest.tex")
	
	inst:AddTag("chest")	
		
	inst:AddComponent("inspectable")
	
	inst:AddComponent("container")
    inst.components.container.slotbgimagetint = {.42,.80,1,1}
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose
    inst.components.container.widgetslotpos = slotpos
    inst.components.container.widgetanimbank = "ui_chest_5x5"
    inst.components.container.widgetanimbuild = "ui_chest_5x5"
    inst.components.container.widgetpos = Vector3(200,200,0)
    inst.components.container.widgetpos_controller = Vector3(0,200,0)
    inst.components.container.side_align_tip = 160
	
	MakeSnowCovered(inst, .01)
	
	--ancientchest.openedseason = GetSeasonManager():GetSeason()
	
	inst:ListenForEvent("seasonChange", function() if GetSeasonManager():GetSeason() ~= inst.openedseason then ReLock(inst) end end, GetWorld())
	
	
	inst.OnSave = function(inst, data)
		if inst.openedseason then 
			data.openedseason = inst.openedseason
		end
    end     
    inst.OnLoad = function(inst, data) 
		if data and data.openedseason then
			inst.openedseason = data.openedseason
		end
    end
    return inst
end

STRINGS.NAMES.ANCIENT_CHEST_LOCKED = "Ancient Chest"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ANCIENT_CHEST_LOCKED = "It's locked."
STRINGS.NAMES.ANCIENT_CHEST_UNLOCKED = "Ancient Chest"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ANCIENT_CHEST_UNLOCKED = "Never seen such a big treasure."

return Prefab("common/ancient_chest_locked", chestlocked, assets),
	   Prefab("common/ancient_chest_unlocked", chestunlocked, assets)
