--[[
作者：小开心
请不要擅自修改或删除
请尊重版权
@

说明：
想患 懒癌强迫症
]]

local Widget = require "widgets/widget" 
local Text = require "widgets/text" 
local TextButton = require "widgets/textbutton"
local Image = require "widgets/image"

local TEMPLATES = require "widgets/templates"
-- 640 360
local width , height = TheSim:GetScreenSize()
local pw = width / 1134
local ph = height / 638

--如果你想要定制 mod ，请联系我（当然是收费的）
--我遇到了一些麻烦，我没有足够的资金继续制作
local qrtext = [[
如果你遇到崩溃，请告知
如果你有自己的创意并且认可我的能力
联系我定制你的专属 mod 
]]

local function createbutton( self, text, onclick, x, y, labelText, textinfo, textbutton )
	x = x or 40
	y = y or 30
	self[text] = self:AddChild(TEMPLATES.Button(textbutton or text, onclick)) --, nil, nil, .5  Vector3(0 , pw * 1.5 , 0)
	self[text]:ForceImageSize(x, y)
	if labelText then
		if not textinfo then
	        textinfo = {}
	    end
		self[text]:SetHoverText(labelText, { font = textinfo.font or NEWFONT_OUTLINE, size = textinfo.size or 30, offset_x = textinfo.offset_x or -4, offset_y = textinfo.offset_y or 45, colour = textinfo.colour or {1,1,1,1}, bg = textinfo.bg })
	end
--[[    self[text]:SetOnGainFocus(function()
        self[text]:SetScale(.5 + .05)
    end)
    self[text]:SetOnLoseFocus(function()
        self[text]:SetScale(.5)
    end)  
    self[text]:SetScale(.5)
    ]]
end 

local News = Class(Widget, function(self, owner, parent)
	Widget._ctor(self, "News") 

    self.owner = owner or GetPlayer()
    self.width , self.height = TheSim:GetScreenSize() 

    self.bg = self:AddChild(Image("images/hud/bg.xml", "bg.tex"))
    self.bg:SetSize(400 * pw , 400 * ph)

    self.qrtext = self:AddChild(Text(BODYTEXTFONT, 20 , qrtext)) 
	self.qrtext:SetHAlign(ANCHOR_MIDDLE) 
	self.qrtext:SetPosition(0 * pw , 90 * ph , 0)
	self.qrtext:SetColour( 1 , 1 , .5 ,1)
--[[
    self.qrcode = self:AddChild(Image("images/hud/qrcode.xml","qrcode.tex"))
	self.qrcode:SetPosition(-50 * pw , -60 * ph , 0)
	self.qrcode:SetSize(200 * pw , 200 * ph)

    local image_name = "self_inspect_"..self.owner.prefab..".tex"
    local atlas_name = "images/avatars/self_inspect_"..self.owner.prefab..".xml"
    if softresolvefilepath(atlas_name) == nil then
        atlas_name = "images/hud.xml"
    end
    self.mg = self:AddChild(TEMPLATES.IconButton(atlas_name, image_name, "Connect to the artist", false, false, function() VisitURL("https://my.oschina.net/tdONEmadao/blog/1546501?lan=cn") end, {size = 30}, "self_inspect_mod.tex"))
    self.mg.icon:SetScale(.7)
    self.mg.icon:SetPosition(-4, 6)
    self.mg:SetPosition(-100 * pw , 120 * ph , 0)

    self.forum = self:AddChild(TEMPLATES.IconButton(atlas_name, image_name, "Connect to the forum", false, false, function() VisitURL("https://my.oschina.net/tdONEmadao/blog/1546501?lan=cn") end, {size = 30}, "self_inspect_mod.tex"))
    self.forum.icon:SetScale(.7)
    self.forum.icon:SetPosition(-4, 6)
    self.forum:SetPosition(100 * pw , 120 * ph , 0)
]]

	createbutton(self, "artist", function() VisitURL("https://my.oschina.net/tdONEmadao/blog/1546501?lan=cn") end, 200 , 50 , "查看画师作品", nil, "查看画师作品")
	self.artist:SetScale(.8)
    self.artist:SetPosition(0 * pw , -10 * ph , 0)

	createbutton(self, "mymod", function() VisitURL("http://steamcommunity.com/profiles/76561198354418168/myworkshopfiles/?appid=322330") end, 200 , 50 , "查看我的作品", nil, "查看作者作品")
	self.mymod:SetScale(.8)
    self.mymod:SetPosition(0 * pw , -50 * ph , 0)

	createbutton(self, "back", function( ) parent:Enable() end, 80 , 50 , "close")
	self.back:SetScale(.8)
    self.back:SetPosition(100 * pw , -90 * ph , 0)

    createbutton(self, "hide", function( ) self.owner.hide = true parent:_Hide() end, 80 , 50 , "隐藏图标，按键F10重新显示")
    self.hide:SetScale(.8)
    self.hide:SetPosition(100 * pw , -130 * ph , 0)

	self:StartUpdating()
end)

local function updatewindow( self , width , height)

	pw = (width - 640) / (1134 - 640)
	ph = height / 638
	self.width = width 
	self.height = height
end
	
function News:OnUpdate(dt)

	local width , height = TheSim:GetScreenSize()
	if self.width ~= width then
		updatewindow( self , width , height)
	end
end

return News
