--[[
BY
 _____   _____   __   _  
|  _  \ /  _  \ |  \ | | 
| | | | | | | | |   \| | 
| | | | | | | | | |\   | 
| |_| | | |_| | | | \  | 
|_____/ \_____/ |_|  \_| 

]]

local assets =
{
    Asset("ANIM", "anim/statue_maxwell.zip"),
	Asset("MINIMAP_IMAGE", "statue"),
}

local prefabs =
{
    "marble",
    "rock_break_fx",
    "chesspiece_formal_sketch",
}

SetSharedLootTable('statue_maxwell',
{
    { 'chesspiece_formal_sketch', 1.00},
    { 'marble', 1.00 },
    { 'marble', 1.00 },
    { 'marble', 0.33 },
})

local function OnWork(inst, worker, workleft)
    if workleft <= 0 then
        local pos = inst:GetPosition()
        SpawnPrefab("rock_break_fx").Transform:SetPosition(pos:Get())
        inst.components.lootdropper:DropLoot(pos)
        inst:Remove()
    elseif workleft < TUNING.MARBLEPILLAR_MINE / 3 then
        inst.AnimState:PlayAnimation("hit_low")
        inst.AnimState:PushAnimation("idle_low")
    elseif workleft < TUNING.MARBLEPILLAR_MINE * 2 / 3 then
        inst.AnimState:PlayAnimation("hit_med")
        inst.AnimState:PushAnimation("idle_med")
    else
        inst.AnimState:PlayAnimation("hit_full")
        inst.AnimState:PushAnimation("idle_full")
    end
end



local function ShouldAcceptItem(inst, item)
	if item.prefab == "donreviverdark" and inst.bosswellisspawned == false and inst.bosswellisdead == false then
		return true
	else
		return false
	end
end

local function SpawnBossMaxWell(inst)
inst.components.workable:SetWorkable(false)
inst:RemoveComponent("inspectable")
inst:RemoveComponent("trader")

inst.bosswellisspawned = true
inst.AnimState:PushAnimation("idle_low")
SpawnPrefab("donfx_shadow_despawn_purple").Transform:SetPosition(inst.Transform:GetWorldPosition())	
SpawnPrefab("shadow_despawn").Transform:SetPosition(inst.Transform:GetWorldPosition())	
SpawnPrefab("shadow_despawn").Transform:SetPosition(inst.Transform:GetWorldPosition())	
SpawnPrefab("donfx_shadow_despawn_pink").Transform:SetPosition(inst.Transform:GetWorldPosition())

local maxwellbossmob = SpawnPrefab("donmob_maxwell_boss")
maxwellbossmob.Transform:SetPosition(inst.Transform:GetWorldPosition())
maxwellbossmob.statuespawnerobjdon = inst
maxwellbossmob.reloadmaxwellbossspawned = true
end

local function OnGetItemFromPlayer(inst, giver, item)
	if item.prefab == "donreviverdark" and inst.bosswellisspawned == false then	
		giver:ShakeCamera(CAMERASHAKE.SIDE, .5, .05, .2)		
		SpawnBossMaxWell(inst)
		giver:ShakeCamera(CAMERASHAKE.SIDE, .5, .05, .2)
	end
end

local function OnRefuseItem(inst, item)

end
	
local function onsave(inst, data)
	data.bosswellisspawned = inst.bosswellisspawned
	data.bosswellisdead = inst.bosswellisdead
end

local function onload(inst, data)
	if data ~= nil and data.bosswellisspawned then
		inst.bosswellisspawned = data.bosswellisspawned
	end
	if data ~= nil and data.bosswellisdead then
		inst.bosswellisdead = data.bosswellisdead
	end
	
	if inst.bosswellisspawned == false then	
		inst.AnimState:PlayAnimation("idle_full")
	else
		inst.AnimState:PlayAnimation("idle_low")
	end
	
	if inst.bosswellisspawned == true and inst.bosswellisdead == false then
		SpawnBossMaxWell(inst)
	end
	
	if inst.bosswellisdead == true then
		inst:RemoveComponent("trader")
	end
		
end


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:AddTag("maxwell")
    inst.entity:AddTag("statue")

    MakeObstaclePhysics(inst, 0.66)

    inst.MiniMapEntity:SetIcon("statue.png")

    inst.AnimState:SetBank("statue_maxwell")
    inst.AnimState:SetBuild("statue_maxwell")
	inst.AnimState:PlayAnimation("idle_full")
	
	inst.bosswellisspawned = false
	inst.bosswellisdead = false

	inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader.onrefuse = OnRefuseItem
    inst.components.trader.deleteitemonaccept = true
	
	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.MINE)
	inst.components.workable:SetWorkLeft(TUNING.MARBLEPILLAR_MINE)
	inst.components.workable:SetOnWorkCallback(OnWork)
	MakeHauntableWork(inst)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
	
    inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetChanceLootTable('statue_maxwell')

    inst:AddComponent("inspectable")
		

	inst.OnSave = onsave
    inst.OnLoad = onload
	
    return inst
end

return Prefab("statuemaxwell", fn, assets, prefabs)
