local Screen = require "widgets/screen"
local Button = require "widgets/button"
local AnimButton = require "widgets/animbutton"
local ImageButton = require "widgets/imagebutton"
local Menu = require "widgets/menu"
local Text = require "widgets/text"
local Image = require "widgets/image"
local UIAnim = require "widgets/uianim"
local Widget = require "widgets/widget"
local PopupDialogScreen = require "screens/popupdialog"
local Grid = require "widgets/grid"
local Spinner = require "widgets/spinner"
require "util"
require "strings"
local TextEdit = require "widgets/textedit"
--local KnownModIndex = GLOBAL.KnownModIndex
local textfont = UIFONT
local fontsize1 = 34
local fontsize2 = 30

for _, moddir in ipairs(KnownModIndex:GetModsToLoad()) do
if KnownModIndex:GetModInfo(moddir).name == "Chinese" then fontsize1 = 34 fontsize2 = 30 end
end

local YiyuScreen = Class(Screen, function(self)

    Screen._ctor(self, "YiyuScreen")
    self.active = true
    SetPause(true,"pause")

    --darken everything behind the dialog
    self.proot = self:AddChild(Widget("ROOT"))
    self.proot:SetVAnchor(ANCHOR_MIDDLE)
    self.proot:SetHAnchor(ANCHOR_MIDDLE)
    self.proot:SetPosition(0,0,0)
    self.proot:SetScaleMode(SCALEMODE_PROPORTIONAL)

    --throw up the background
    local shield = self.proot:AddChild( Image( "images/shezhi.xml", "shezhi.tex" ) )
    shield:SetPosition( 0,45,0 )
    shield:SetSize( 900, 750 )

    local player = GetPlayer()
	local expxuyao = player.yiyulevel*5					--当前级别需经验
	local expshang = expxuyao-5							--上个级别需经验
	local expbzong = player.yiyulevel*(5+expxuyao)/2		--本级总经验
	local expszong = (player.yiyulevel-1)*(5+expshang)/2	--上级总经验
	local expyiyou = player.level-expszong				--当前级别有经验
    local label = self.proot:AddChild( Text( textfont, fontsize1, "".."hahh".."" ))
	local labels = self.proot:AddChild( Text( textfont, fontsize1, string.format("Version[%s]","hahh") ))
	local yiyu_absorball = 0
	local absorb1 = 0--护甲头
	local absorb2 = 0--护甲胸
	local absorb3 = 0--护吸收
	local corselet = player.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
	local helmet = player.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
	if corselet and corselet.components.armor and corselet.components.armor.absorb_percent then
		absorb1 = corselet.components.armor.absorb_percent
	end
	if helmet and helmet.components.armor and helmet.components.armor.absorb_percent then
		absorb2 = helmet.components.armor.absorb_percent
	end
	if player and player.components.health then
		absorb3 = player.components.health.absorb
	end
	local yiyu_absorball1 = 100-(100*absorb3)
	local yiyu_absorball2 = yiyu_absorball1-(yiyu_absorball1*absorb2)
	local yiyu_absorball3 = yiyu_absorball2-(yiyu_absorball2*absorb1)
--	yiyu_absorball = math.floor(100-yiyu_absorball3)
	yiyu_absorball = 100-yiyu_absorball3

    label:SetPosition( -170, 250, 0 )
    label:SetRegionSize( 200, 40 )
    label:SetHAlign( ANCHOR_MIDDLE )
    label:SetColour(20,240,150,1)
    labels:SetPosition( 120, 250, 0 )
    labels:SetRegionSize( 200, 40 )
    labels:SetHAlign( ANCHOR_MIDDLE )
    labels:SetColour(0,128,255,1)

    self.grid1 = self.proot:AddChild(Grid())
    self.grid1:InitSize(1, 15, 700, -70)
    self.grid1:SetPosition(-170, 200, 0)
    self.grid1:SetScale(0.9, 0.6, 0.9)

    local spinners1 = {}
	local yiyulevels = GetPlayer().yiyulevel
    table.insert(spinners1, {"".."hahh".."", yiyulevels, "hh" ,false ,nil})
    table.insert(spinners1, {"".."hahh".."", string.format("%.0f/%.0f", expyiyou, expxuyao), "hh" ,false ,nil})
    table.insert(spinners1, {"".."hahh".."",string.format("%.0f/%.0f", yiyu_absorball, 100), "hh" ,false ,nil}) 
    table.insert(spinners1, {"".."hahh".."",nil, "hh" ,false ,nil})
    table.insert(spinners1, {"R-G-J-K-L-U-I-O",nil, "hh" ,false ,nil})
    table.insert(spinners1, {"".."hahh".."Z",nil, "hh" ,false ,nil})
    table.insert(spinners1, {"hahh", nil , "hh" ,false ,nil})
	table.insert(spinners1, {"hahh", nil , "hh" ,false ,nil})
	table.insert(spinners1, {"hahh", nil , "hh" ,false ,nil})
    for k,v in ipairs(spinners1) do self.grid1:AddItem(self:CreateSpinnerGroup(v[1], v[2], v[3], v[4]), 1, k) end

    self.grid2 = self.proot:AddChild(Grid())
    self.grid2:InitSize(1, 15, 700, -70)
    self.grid2:SetPosition(130, 200, 0)
    self.grid2:SetScale(0.9, 0.6, 0.9)
    local spinners2 = {}
    table.insert(spinners2, {"".."hahh".."",nil, "hh" ,false ,nil})
	table.insert(spinners2, {"".."hahh".."",nil, "hh" ,false ,nil})
    table.insert(spinners2, {"".."hahh".."",nil, "hh" ,false ,nil})
	table.insert(spinners2, {"".."hahh".."",nil, "hh" ,false ,nil})
	table.insert(spinners2, {"".."hahh".."",nil, "hh" ,false ,nil})
    table.insert(spinners2, {"".."hahh".."",nil, "hh" ,false ,nil})
    table.insert(spinners2, {"".."hahh".."542905985",nil, "hh" ,false ,nil})
    table.insert(spinners2, {"".."hahh".."",nil, "".."hahh".."" ,false ,nil})
    for k,v in ipairs(spinners2) do self.grid2:AddItem(self:CreateSpinnerGroup(v[1], v[2], v[3], v[4]), 1, k) end

	self.skillbutton1 = self.proot:AddChild(ImageButton())
	self.skillbutton1:SetText("+")
    self.skillbutton1:SetPosition(-70, -55, 0)
    self.skillbutton1:SetScale(0.4)
    self.skillbutton1:SetOnClick(
	function()
	self:Close()
	if player.yiyulevel < 25 then
		player.components.talker:Say("hahh")
	elseif player.yiyulevel >= 25 then
		if player.yiyumaxskill == 3 then
		player.yiyumaxskill = 1
		player.components.talker:Say("hahh")
		elseif player.yiyumaxskill == 1 then
		player.components.talker:Say("hahh")
		elseif player.yiyumaxskill == 2 then
		player.components.talker:Say("hahh")
		else
		player.components.talker:Say("hahh")
		end
	end	
	end
	)

	self.skillbutton2 = self.proot:AddChild(ImageButton())
	self.skillbutton2:SetText("+")
    self.skillbutton2:SetPosition(-70, -95, 0)
    self.skillbutton2:SetScale(0.4)
    self.skillbutton2:SetOnClick(

	function()
	self:Close()
	if player.yiyulevel < 25 then
		player.components.talker:Say("hahh")
	elseif player.yiyulevel >= 25 then
		if player.yiyumaxskill == 3 then
		player.yiyumaxskill = 2
		player.components.talker:Say("hahh")
		elseif player.yiyumaxskill == 1 then
		player.components.talker:Say("hahh")
		elseif player.yiyumaxskill == 2 then
		player.components.talker:Say("hahh")
		else
		player.components.talker:Say("hahh")
		end
	end
	end
	
	)

	self.textbutton = self.proot:AddChild(ImageButton())
	self.textbutton:SetText(TUNING.YIYUDDGG)
    self.textbutton:SetPosition(-70, -135, 0)
    self.textbutton:SetScale(0.4)
    self.textbutton:SetOnClick(
	function()
	if GetPlayer().HUD and GetPlayer().HUD.nandu1 then
	if GetPlayer():HasTag("toso") then
		GetPlayer():RemoveTag("toso")
		GetPlayer().HUD.nandu1:Hide()
		GetPlayer().HUD.nandu2:Hide()
		GetPlayer().HUD.nandu3:Hide()
		GetPlayer().HUD.nandu5:Hide()
	else
	GetPlayer():AddTag("toso")
		GetPlayer().HUD.nandu1:Show()
		GetPlayer().HUD.nandu2:Show()
		GetPlayer().HUD.nandu3:Show()
	end
	end
	end)

	self.vistbutton = self.proot:AddChild(ImageButton())
	self.vistbutton:SetText(TUNING.YIYUDDDS)
    self.vistbutton:SetPosition(65, -95, 0)
    self.vistbutton:SetScale(0.4)
    self.vistbutton:SetOnClick( function() VisitURL("http://tieba.baidu.com/p/4405091040") end)

	self.vistbutton = self.proot:AddChild(ImageButton())
	self.vistbutton:SetText(TUNING.YIYUDDDG)
    self.vistbutton:SetPosition(65, -135, 0)
    self.vistbutton:SetScale(0.4)
    self.vistbutton:SetOnClick( function() VisitURL("http://steamcommunity.com/sharedfiles/filedetails/?id=642704851") end)

	self.vistbutton = self.proot:AddChild(ImageButton())
	self.vistbutton:SetText(TUNING.YIYUDDDK)
    self.vistbutton:SetPosition(155, -95, 0)
    self.vistbutton:SetScale(0.4)
    self.vistbutton:SetOnClick( function() self:Close() player.components.talker:Say("hhah") end)

    self.menu = self.proot:AddChild(Menu(nil, 200, true))
    self.menu:SetPosition(155, -135 ,0)
    self.menu:SetScale(0.4)
    self.menu:AddItem(""..TUNING.YIYUDDGG.."", function() self:Close() end)
end)

function YiyuScreen:CreateSpinnerGroup( text, text2 , text3 ,button,fn)
    local label_width = 250
    local group = Widget("SpinnerGroup")
    local label = group:AddChild( Text( textfont, fontsize2, text ) )
    label:SetPosition( 0, 0, 0 )
    label:SetRegionSize( label_width, fontsize2 )
    label:SetHAlign( ANCHOR_LEFT )
    label:SetColour(0,0.6,1,1)
	
    local label2 = group:AddChild( Text( textfont, fontsize2, text2 ) )
    label2:SetPosition( 0, 0, 0 )
    label2:SetRegionSize( label_width, fontsize2 )
    label2:SetHAlign( ANCHOR_MIDDLE )
    label2:SetColour(0,0.6,1,1)	

    if button then
        local label3 = group:AddChild(Menu(nil, 200, true))
        label3:SetPosition( 100, 0, 0 )
        label3:SetScale(0.6)
        label3:SetHRegPoint(ANCHOR_RIGHT)
        label3:AddItem(text3, function()
            if fn then
                fn()

            end
         end)
    end

    return group
end

function YiyuScreen:Close()
    self.active = false
    SetPause(false)
    TheFrontEnd:PopScreen()
end 

function YiyuScreen:OnControl(control, down)
    if YiyuScreen._base.OnControl(self, control, down) then return true end
    if not down and  control == CONTROL_CANCEL then
        self:Close()
        return true
    end
    return false
end


return YiyuScreen