
local MakePlayerCharacter = require "prefabs/player_common"


local assets = {

        Asset( "ANIM", "anim/player_basic.zip" ),
        Asset( "ANIM", "anim/player_idles_shiver.zip" ),
        Asset( "ANIM", "anim/player_actions.zip" ),
        Asset( "ANIM", "anim/player_actions_axe.zip" ),
        Asset( "ANIM", "anim/player_actions_pickaxe.zip" ),
        Asset( "ANIM", "anim/player_actions_shovel.zip" ),
        Asset( "ANIM", "anim/player_actions_blowdart.zip" ),
        Asset( "ANIM", "anim/player_actions_eat.zip" ),
        Asset( "ANIM", "anim/player_actions_item.zip" ),
        Asset( "ANIM", "anim/player_actions_uniqueitem.zip" ),
        Asset( "ANIM", "anim/player_actions_bugnet.zip" ),
        Asset( "ANIM", "anim/player_actions_fishing.zip" ),
        Asset( "ANIM", "anim/player_actions_boomerang.zip" ),
        Asset( "ANIM", "anim/player_bush_hat.zip" ),
        Asset( "ANIM", "anim/player_attacks.zip" ),
        Asset( "ANIM", "anim/player_idles.zip" ),
        Asset( "ANIM", "anim/player_rebirth.zip" ),
        Asset( "ANIM", "anim/player_jump.zip" ),
        Asset( "ANIM", "anim/player_amulet_resurrect.zip" ),
        Asset( "ANIM", "anim/player_teleport.zip" ),
        Asset( "ANIM", "anim/wilson_fx.zip" ),
        Asset( "ANIM", "anim/player_one_man_band.zip" ),
        Asset( "ANIM", "anim/shadow_hands.zip" ),
        Asset( "SOUND", "sound/sfx.fsb" ),
        Asset( "SOUND", "sound/wilson.fsb" ),
        Asset( "ANIM", "anim/beard.zip" ),

        Asset( "ANIM", "anim/sollyz.zip" ),
        Asset( "ANIM", "anim/ghost_sollyz_build.zip" ),
}
local prefabs = {}
local start_inv = {
"fishingrod",
	-- Custom starting items
}

local function runner(inst)
	
	local max_upgrades = 30
	local upgrades = math.min(inst.level, max_upgrades)
			if TheWorld.state.phase == "day" then 
				inst.components.locomotor.walkspeed =  math.ceil (4 + upgrades / 6) --9
				inst.components.locomotor.runspeed = math.ceil (6 + upgrades / 5) --12
			elseif TheWorld.state.phase == "dusk" then
				inst.components.locomotor.walkspeed = math.ceil (4 + upgrades / 12) --6.5
				inst.components.locomotor.runspeed = math.ceil (6 + upgrades / 12) --8.5
			elseif TheWorld.state.phase == "night" then
				inst.components.locomotor.walkspeed =  4
				inst.components.locomotor.runspeed = 6
			end
		

end

local function applyupgrades(inst)

	local max_upgrades = 30
	local upgrades = math.min(inst.level, max_upgrades)

	local hunger_percent = inst.components.hunger:GetPercent()
	local health_percent = inst.components.health:GetPercent()
	local sanity_percent = inst.components.sanity:GetPercent()

	inst.components.hunger.max = math.ceil (150 + upgrades * 5) --300
	inst.components.health.maxhealth = math.ceil (100 + upgrades * 4) --220
	inst.components.sanity.max = math.ceil (90 + upgrades * 2) --150
	
	--inst.components.locomotor.walkspeed =  math.ceil (4 + upgrades / 6) --9
	--inst.components.locomotor.runspeed = math.ceil (6 + upgrades / 5) --12
	
	if TheWorld.state.phase == "day" then 
			inst.components.locomotor.walkspeed =  math.ceil (4 + upgrades / 6) --9
				inst.components.locomotor.runspeed = math.ceil (6 + upgrades / 5) --12
			elseif TheWorld.state.phase == "dusk" then
				inst.components.locomotor.walkspeed = math.ceil (4 + upgrades / 12) --6.5
				inst.components.locomotor.runspeed = math.ceil (6 + upgrades / 12) --8.5
			elseif TheWorld.state.phase == "night" then
				inst.components.locomotor.walkspeed =  4
				inst.components.locomotor.runspeed = 6
			end
	
	inst.components.talker:Say("Level : ".. (inst.level))
	
	if inst.level >29 then
		inst.components.talker:Say("Level : Max!")
	end

	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.health:SetPercent(health_percent)
	inst.components.sanity:SetPercent(sanity_percent)
	
	runner(inst)
end

local function oneat(inst, food)
	
	--if food and food.components.edible and food.components.edible.foodtype == "HELLO" then
	if food and food.components.edible and food.prefab == "fish"  then
		--give an upgrade!
		inst.level = inst.level + 1
		applyupgrades(inst)	
		inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")
		--inst.HUD.controls.status.heart:PulseGreen()
		--inst.HUD.controls.status.stomach:PulseGreen()
		--inst.HUD.controls.status.brain:PulseGreen()
		
		--inst.HUD.controls.status.brain:ScaleTo(1.3,1,.7)
		--inst.HUD.controls.status.heart:ScaleTo(1.3,1,.7)
		--inst.HUD.controls.status.stomach:ScaleTo(1.3,1,.7)
	end
end

local function onpreload(inst, data)
	if data then
		if data.level then
			inst.level = data.level
			applyupgrades(inst)
			--re-set these from the save data, because of load-order clipping issues
			if data.health and data.health.health then inst.components.health.currenthealth = data.health.health end
			if data.hunger and data.hunger.hunger then inst.components.hunger.current = data.hunger.hunger end
			if data.sanity and data.sanity.current then inst.components.sanity.current = data.sanity.current end
			inst.components.health:DoDelta(0)
			inst.components.hunger:DoDelta(0)
			inst.components.sanity:DoDelta(0)
		end
	end

end

local function onsave(inst, data)
	data.level = inst.level
	data.charge_time = inst.charge_time
end


-- This initializes for both clients and the host
local common_postinit = function(inst) 
	-- choose which sounds this character will play
	inst.soundsname = "willow"

	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "sollyz.tex" )
end

-- This initializes for the host only
local master_postinit = function(inst)
	inst.level = 0
	inst.components.eater:SetOnEatFn(oneat)
	applyupgrades(inst)
	-- Stats	
	inst.components.health:SetMaxHealth(100)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(90)
	inst.components.locomotor.walkspeed = 4
	inst.components.locomotor.runspeed = 6
	
	inst.OnSave = onsave
	inst.OnPreLoad = onpreload
	
	
	inst:ListenForEvent("clocktick", function() runner(inst) end, TheWorld)
end



return MakePlayerCharacter("sollyz", prefabs, assets, common_postinit, master_postinit, start_inv)
