
local MakePlayerCharacter = require "prefabs/player_common"

local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local assetsrog = {
        Asset( "ANIM", "anim/miaojids.zip" ),
}
local prefabs = {}

local start_inv = {
}
local function applyupgrades(inst)
	local max_upgrades = 50
	local upgrades = math.min(inst.level, max_upgrades)
	--print(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 * 3)
	inst.components.health.maxhealth = math.ceil (150 + upgrades * 5) 
	inst.components.sanity.max = math.ceil (100 + upgrades * 3) 
	if  TheSim:GetGameID() == "DST"  then	
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "miaoji_speed_mod", 1 + 0.1*upgrades/10)
	inst.components.combat.damagemultiplier = 1+0.015*upgrades
	else
	inst.components.locomotor.walkspeed = 4+ 0.1*upgrades/10
	inst.components.locomotor.runspeed = 6	+ 0.1*upgrades/10
		if IsDLCEnabled(CAPY_DLC) then
			inst.components.combat:AddDamageModifier("miaoji", 0.015*upgrades)
			--print("hainan")
		else
		inst.components.combat.damagemultiplier = 1+0.015*upgrades
			--print("feihainan")
		end
	end
	inst.components.temperature.inherentinsulation = 240 +	1.6*upgrades

	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.health:SetPercent(health_percent)
    local ignoresanity = inst.components.sanity.ignore
    inst.components.sanity.ignore = false
    inst.components.sanity:SetPercent(sanity_percent)
    inst.components.sanity.ignore = ignoresanity
	--inst.components.talker:Say("Level : ".. (inst.level))
	
	--if inst.level >49 then
	--	inst.components.talker:Say("Level : Max!")
	--end
end

local function onpreload(inst, data)
    if data ~= nil and data.level ~= nil then
        inst.level = data.level
        applyupgrades(inst)
		if TheSim:GetGameID() == "DST" then
        if data.health ~= nil and data.health.health ~= nil then
            inst.components.health:SetCurrentHealth(data.health.health)
        end
        if data.hunger ~= nil and data.hunger.hunger ~= nil then
            inst.components.hunger.current = data.hunger.hunger
        end
        if data.sanity ~= nil and data.sanity.current ~= nil then
            inst.components.sanity.current = data.sanity.current
        end
		else
			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
		end
        inst.components.health:DoDelta(0)
        inst.components.hunger:DoDelta(0)
        inst.components.sanity:DoDelta(0)
    end
end
local function onbecamehuman(inst)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "miaoji_speed_mod", 1.2)
end

local function onbecameghost(inst)
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "miaoji_speed_mod")
end
local function updatelight(inst, phase)
    if phase == "day" then
		if TUNING.MIAOJI_LIGHT and not inst:HasTag("playerghost")then
        inst.Light:Enable(false)
		end
    elseif phase == "night" then
		if TUNING.MIAOJI_LIGHT and not inst:HasTag("playerghost")then
        inst.Light:Enable(true) 
		end
		inst.components.sanity.night_drain_mult = 1	
    elseif phase == "dusk" then
		if TUNING.MIAOJI_LIGHT and not inst:HasTag("playerghost")then
        inst.Light:Enable(false)
		end
		inst.components.sanity.night_drain_mult = 1.3
	end
end
local function updatelightds(inst)
    if GetClock():IsDay() then
		if TUNING.MIAOJI_LIGHT then
        inst.Light:Enable(false)
		end 
    elseif GetClock():IsNight() then
		if TUNING.MIAOJI_LIGHT then
        inst.Light:Enable(true) 
		end
		inst.components.sanity.night_drain_mult = 1     
    elseif GetClock():IsDusk() then
		if TUNING.MIAOJI_LIGHT then
        inst.Light:Enable(false)
		end
		inst.components.sanity.night_drain_mult = 1.3 
	end
end
local function light(inst)
    inst.Light:SetFalloff(0.4)
    inst.Light:SetIntensity(.7)
    inst.Light:SetRadius(2.5)
    inst.Light:SetColour(180 / 255, 195 / 255, 150 / 255)
	updatelight(inst, TheWorld.state.phase)
end

local function onsave(inst, data)
    data.level = inst.level > 0 and inst.level or nil
end
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_respawnedfromghost", light)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end
local function oneat(inst, food)

	if food and food.components.edible and food.components.perishable then 
	local num = food.components.perishable:GetPercent()
	local healthm = food.components.edible:GetHealth(inst)
	local hungerm = food.components.edible:GetHealth(inst)
		if food.prefab == "fish" or food.prefab == "fish01" or food.prefab == "fish_med" or food.prefab == "fish_raw" or food.prefab == "fish_raw_small" 
			or food.prefab == "fish3" or food.prefab == "fish4" or food.prefab == "fish5"	or food.prefab == "tropical_fish" then
			inst.components.health:DoDelta(healthm*num)
			inst.components.hunger:DoDelta(hungerm*num)
			if inst.components.miaoji_exp.current <5000 then 
			inst.components.miaoji_exp:DoDelta(20)
			inst.level = math.floor(inst.components.miaoji_exp.current/100)
			applyupgrades(inst)
			end
		elseif food.prefab == "fish_cooked" or food.prefab == "fish01_cooked" or food.prefab == "fish3_cooked" or food.prefab == "fish4_cooked" or food.prefab == "fish5_cooked"then
			inst.components.health:DoDelta(healthm*num)
			inst.components.hunger:DoDelta(hungerm*num)	
		elseif food.prefab == "butterflywings" then		
			inst.components.health:DoDelta(healthm*num)
			inst.components.hunger:DoDelta(hungerm*num)
			if inst.components.miaoji_exp.current <5000 then
			inst.components.miaoji_exp:DoDelta(10) 
			inst.level = math.floor(inst.components.miaoji_exp.current/100)
			applyupgrades(inst)
			end	
		elseif food.prefab == "miaoji_food" or food.prefab == "miaoji_foodfu" then		
			if inst.components.miaoji_exp.current <5000 then
			inst.components.miaoji_exp:DoDelta(50)		
			inst.level = math.floor(inst.components.miaoji_exp.current/100)
			applyupgrades(inst)
			end			
		end
	end
end

local common_postinit = function(inst) 
	if TheSim:GetGameID() == "DST" then
	inst.currentmiaoji = net_shortint(inst.GUID,"currentmiaoji")
	end
	inst.MiniMapEntity:SetIcon( "miaoji.tex" )
	inst:AddTag("miaoji")
    inst.Light:SetFalloff(0.4)
    inst.Light:SetIntensity(.7)
    inst.Light:SetRadius(2.5)
    inst.Light:SetColour(180 / 255, 195 / 255, 150 / 255)
    inst.Light:Enable(false)
end

local master_postinit = function(inst)
	inst.level = 0
	inst.soundsname = "willow"
	inst:AddComponent("miaoji_exp")
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(100)
	inst.components.eater:SetOnEatFn(oneat)
    inst.components.combat.damagemultiplier = 1.5 
	inst.components.temperature.inherentinsulation = 240
	applyupgrades(inst)
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    inst:WatchWorldState("phase", updatelight)
    updatelight(inst, TheWorld.state.phase)	
    inst.OnSave = onsave
    inst.OnLoad = onload
    inst.OnPreLoad = onpreload

	
end
local fn = function(inst)
	common_postinit(inst)
	inst.AnimState:SetBuild("miaojids")
	inst.level = 0
	inst.soundsname = "willow"
	inst:AddComponent("miaoji_exp")
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(100)
	inst.components.eater:SetOnEatFn(oneat)
    inst.components.combat.damagemultiplier = 1.5 
	inst.components.temperature.inherentinsulation = 240
	applyupgrades(inst)
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    inst:ListenForEvent( "dusktime", function() updatelightds(inst) end , GetWorld())
    inst:ListenForEvent( "daytime", function() updatelightds(inst) end , GetWorld())
    inst:ListenForEvent( "nighttime", function() updatelightds(inst) end , GetWorld())
    updatelightds(inst)	
    inst.OnSave = onsave
    inst.OnPreLoad = onpreload

end
if TheSim:GetGameID() == "DST" then
return MakePlayerCharacter("miaoji", prefabs, assets, common_postinit, master_postinit, start_inv)
else
return MakePlayerCharacter("miaoji", prefabs, assetsrog, fn, start_inv)
end
