Assets = {
	Asset("ATLAS", "images/status_bg.xml"),
}

local SHOWTEMPERATURE = GetModConfigData("ShowTemperature")
local CELSIUS = GetModConfigData("Celsius")

local function BadgePostConstruct(self)
	self:SetScale(.9,.9,.9)
	
	local Image = GLOBAL.require('widgets/image')
	self.bg = self:AddChild(Image("images/status_bg.xml", "status_bg.tex"))
	self.bg:SetScale(.4,.43,0)
    self.bg:SetPosition(-.5,-25,0)
	
	self.num:SetFont(GLOBAL.NUMBERFONT)
	self.num:SetSize(28)
    self.num:SetPosition(3.5, -40.5, 0)
	self.num:SetScale(1,.78,1)
	self.num:MoveToFront()
	self.num:Show()

	local Text = GLOBAL.require('widgets/text')
	self.maxnum = self:AddChild(Text(GLOBAL.NUMBERFONT, 33))
    self.maxnum:SetPosition(6, 0, 0)
	self.num:SetScale(1,.78,1)
	self.maxnum:MoveToFront()
    self.maxnum:Hide()
	
	local OldOnGainFocus = self.OnGainFocus
	function self:OnGainFocus()
		OldOnGainFocus(self)
		self.maxnum:Show()
	end

	local OldOnLoseFocus = self.OnLoseFocus
	function self:OnLoseFocus()
		OldOnLoseFocus(self)
		self.maxnum:Hide()
		self.num:Show()
	end
	
	local OldSetPercent = self.SetPercent
	function self:SetPercent(val, max, ...)
		self.maxnum:SetString(tostring(math.ceil(max)))
		OldSetPercent(self, val, max, ...)
	end
end
AddClassPostConstruct("widgets/badge", BadgePostConstruct)

local controls = nil
local function ControlsPostConstruct(self)
	controls = self
	self.clock._text:SetPosition(5,0,0)
	self.clock._text:SetScale(.8,.8,0)
	
	self.sidepanel:SetPosition(-100,-70,0)
	
	-- local kramped = GLOBAL.require "components/kramped"

	-- local NaughtyBadge = Class(Badge, function(status, owner)
		-- Badge._ctor(status, "naughty", owner)
	-- end)

	-- local TemperatureBadge = Class(Badge, function(status, owner)
		-- Badge._ctor(status, "temperature", owner)
	-- end)
	
end
AddClassPostConstruct("widgets/controls", ControlsPostConstruct)

local function StatusPostConstruct(self)
	self.brain:SetPosition(0,10,0)
	self.stomach:SetPosition(-62,35,0)
	self.heart:SetPosition(62,35,0)

	local Badge = GLOBAL.require("widgets/badge")
	
	if SHOWTEMPERATURE then
		self.temperature = self:AddChild(Badge("temperature", self.owner))
		-- self.temperature.num:SetTemperature(30)--ThePlayer:GetTemperature())
		self.inst:ListenForEvent("temperaturedelta",
			function(inst)
				local val = self.owner:GetTemperature()
				if CELSIUS then
					self.temperature.num:SetString(tostring(math.ceil(val)).."\176C")
				else
					self.temperature.num:SetString(tostring(math.ceil(1.8*(val) + 32)).."\176F")
				end
				-- status.temperature:SetTemperature(self.owner:GetTemperature())
			end,
			self.owner)
		self.temperature:SetPosition(57.5,5,0)
	end
	
	if self.moisturemeter then
		self.moisturemeter:SetPosition(0,-80,0)
	end
end
AddClassPostConstruct("widgets/statusdisplays", StatusPostConstruct)

local function UIClockPostInit(self)
	GLOBAL.STRINGS.UI.HUD.WORLD_CLOCKDAY = "World\nDay"
	self:UpdateWorldString()
	local world = GLOBAL.TheWorld
    self.inst:ListenForEvent("cycleschanged",
		function(inst, data)
			if not self._showingcycles then self:UpdateWorldString() end
		end, world)
    self.inst:ListenForEvent("clocktick",
		function(inst, data)
			if not self._showingcycles then self:UpdateWorldString() end
		end, world)
	
	function self:OnGainFocus()
		self._base.OnGainFocus(self)
		self:UpdateDayString()
		return true
	end

	function self:OnLoseFocus()
		self._base.OnLoseFocus(self)
		self:UpdateWorldString()
		return true
	end
end

AddClassPostConstruct("widgets/uiclock", UIClockPostInit)

--[[
--This may need to be rewritten if they actually add back the badge scaling
-- (it's been removed because in DST it can't be in the oneat directly)

-- WX78 Upgrade Animation Fix
local function applyupgrades(inst)

	local max_upgrades = 15
	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(TUNING.WX78_MIN_HUNGER + upgrades* (TUNING.WX78_MAX_HUNGER - TUNING.WX78_MIN_HUNGER)/max_upgrades)
	inst.components.health.maxhealth = math.ceil(TUNING.WX78_MIN_HEALTH + upgrades* (TUNING.WX78_MAX_HEALTH - TUNING.WX78_MIN_HEALTH)/max_upgrades)
	inst.components.sanity.max = math.ceil(TUNING.WX78_MIN_SANITY + upgrades* (TUNING.WX78_MAX_SANITY - TUNING.WX78_MIN_SANITY)/max_upgrades)

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

local function newoneat(inst, food)
	
	if food and food.components.edible and food.components.edible.foodtype == "GEARS" then
		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.2,.9,.7)
		inst.HUD.controls.status.heart:ScaleTo(1.2,.9,.7)
		inst.HUD.controls.status.stomach:ScaleTo(1.2,.9,.7)
	end
end

local function wx78eat(inst)
	inst.components.eater:SetOnEatFn(newoneat) --problem when joining
end

AddPrefabPostInit("wx78", wx78eat)
]]


--[[
--This may need to be looked at again if RPG HUD is added

-- Status Display Fix
local function StatusDisplayFix(self)
	function self:CloseControllerInventory()
		TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/craft_close")
		self.controls:ShowStatusNumbers()
		self:ShowControllerCrafting()
		self.controls.inv:CloseControllerInventory()
	end
end

local RPGActive = 0
for _, moddir in ipairs(GLOBAL.KnownModIndex:GetModsToLoad()) do
    if GLOBAL.KnownModIndex:GetModInfo(moddir).name == "RPG HUD" then
		RPGActive = 1
    end
end

if RPGActive == 0 then
	AddClassPostConstruct("screens/playerhud", StatusDisplayFix)
end
]]