local UIAnim = require "widgets/uianim"
local Text = require "widgets/text"
local Widget = require "widgets/widget"
local Image = require "widgets/image"
local ImageButton = require "widgets/imagebutton"
local AnimButton = require "widgets/animbutton"
local HoverText = require "widgets/hoverer"


local function refresh(self,Lose)
	if self.CDduration.duration <=0 and Lose == true then
		self.CDduration:Hide()
		if self.lv then
			self.lv:Show()
		end
	elseif self.CDduration.duration >0 and Lose == false then 
		self.CDduration:Hide()
		if self.lv then
			self.lv:Show()
		end
	else
		self.CDduration:Show()
		if self.lv then
			self.lv:Hide()
		end		
	end
	if self.lv and self.lv.lv <= 0 then
		self.CDduration:Hide()
		self.lv:Hide()
	end
end



local RPG_State = Class(Widget, function(self, owner)
	Widget._ctor(self, "RPG_State")
	self.owner = owner
	self.root = self:AddChild(Widget("root"))
	self.root:SetPosition(-130,0,0)	
		--————————————大招指示器————————————————————————
	if self.owner and self.owner.components.playerskill and self.owner.components.playerskill.skill.ultimate then	
		self.ultimate = self.root:AddChild(Widget("ultimate"))
		self.ultimate:SetPosition(0, 0,0)
		self.ultimate:SetScale(0.3,0.3,0.3)	
		self.ultimate.anim = self.ultimate:AddChild(UIAnim())
		self.ultimate.anim:GetAnimState():SetBank("ultimate_cd")
		self.ultimate.anim:GetAnimState():SetBuild("ultimate_cd")
		self.ultimate.anim:GetAnimState():PlayAnimation("anim")

		
		--ultimate相关组件的声明
		self.ultimate.CDduration = self.ultimate:AddChild(Text(BODYTEXTFONT, 120)) --显示CD时间
		self.ultimate.lv = self.ultimate:AddChild(Text(BODYTEXTFONT, 120)) --显示技能等级
		
		
			local duration = self.owner.components.playerskill.skill.ultimate.CDduration.A 
			local CD = self.owner.components.playerskill.skill.ultimate.CD.A 
			local lv = self.owner.components.playerskill.skill.ultimate.lv
			self.ultimate.percent = duration / CD
			self.ultimate.anim:GetAnimState():SetPercent("anim", self.ultimate.percent)
			self.ultimate.CDduration.duration = duration
			self.ultimate.lv.lv = lv
		
		
		self.ultimate.CDduration:SetHAlign(ANCHOR_MIDDLE)
		self.ultimate.CDduration:SetPosition(5, 0, 0)
		self.ultimate.CDduration:SetString("CD:"..tostring(self.ultimate.CDduration.duration or 0))

		self.ultimate.lv:SetHAlign(ANCHOR_MIDDLE)
		self.ultimate.lv:SetPosition(5, 0, 0)
		self.ultimate.lv:SetString("Lv:"..tostring(self.ultimate.lv.lv or nil))	

		refresh(self.ultimate,true)
		
		self.ultimate.OnGainFocus = function()
			self:OnGainFocus(self)
			refresh(self.ultimate,false)
		end
		self.ultimate.OnLoseFocus = function()
			self:OnLoseFocus(self)
			refresh(self.ultimate,true)
		end	

		self.ultimate.point = self.ultimate:AddChild(ImageButton("images/skillpoint_button.xml", "skillpoint_button.tex", "skillpoint_button.tex", "skillpoint_button.tex"))
		self.ultimate.point:SetTooltip("U")
		self.ultimate.point:SetPosition(-140, 0, 0)
		self.ultimate.point:SetScale(4)	
		self.ultimate.point:MoveToFront()
		self.ultimate.point:SetOnClick( function() self.owner:PushEvent("PointChanged",{AR ="R",order = "ultimate"})  end)	
		self.ultimate.point:Hide()
	end
	--————————小招指示器——————————————————————
	--normal_1
	if self.owner and self.owner.components.playerskill and self.owner.components.playerskill.skill.normal_1 then
		self.normal_1 = self.root:AddChild(Widget("normal_1"))
		self.normal_1:SetPosition(-80, 40,0)
		self.normal_1:SetScale(0.7,0.7,0.7)	
		--动画部分
		self.normal_1.anim = self.normal_1:AddChild(UIAnim())
		self.normal_1.anim:GetAnimState():SetBank("normal_cd")
		self.normal_1.anim:GetAnimState():SetBuild("normal_cd")
		self.normal_1.anim:GetAnimState():PlayAnimation("anim")

		--文字部分
		self.normal_1.lv = self.normal_1:AddChild(Text(BODYTEXTFONT, 40))
		self.normal_1.lv:SetHAlign(ANCHOR_MIDDLE)
		self.normal_1.lv:SetPosition(0, 0, 0)
		self.normal_1.lv.num = self.owner.components.playerskill.skill.normal_1.lv
		self.normal_1.lv:SetString(tostring(self.normal_1.lv.num or nil))	
		self.normal_1.lv:SetColour(0, 0.5, 0, 0.5)
		self.normal_1.lv:Hide()	

		--初始化一些相关信息:CD指示器的动画位置和等级
			
			local duration = self.owner.components.playerskill.skill.normal_1.CDduration.A 
			local CD = self.owner.components.playerskill.skill.normal_1.CD.A 
			local lv = self.owner.components.playerskill.skill.normal_1.lv
			
			self.normal_1.percent = duration / CD
			self.normal_1.anim:GetAnimState():SetPercent("anim", self.normal_1.percent)
			self.normal_1.lv.num = lv
				
		--设置面板更新
		self.normal_1.lv.refresh =function(newlevel)
			self.normal_1.lv:SetString(tostring(newlevel or nil))	
		end
		--设置鼠标移动到上面时的状态
		self.normal_1.OnGainFocus = function()
			self:OnGainFocus(self)
			self.normal_1.lv:Show()
		end
		self.normal_1.OnLoseFocus = function()
			self:OnLoseFocus(self)
			self.normal_1.lv:Hide()	
		end
		
		--与加点有关的按钮	
		self.normal_1.point = self.normal_1:AddChild(ImageButton("images/skillpoint_button.xml", "skillpoint_button.tex", "skillpoint_button.tex", "skillpoint_button.tex"))
		self.normal_1.point:SetTooltip("A1")
		self.normal_1.point:SetPosition(-50, 0, 0)

		self.normal_1.point:SetOnClick( function()self.owner:PushEvent("PointChanged",{AR ="R",order = "normal_1"})  end)	
	end
	--normal_2
	if self.owner and self.owner.components.playerskill and self.owner.components.playerskill.skill.normal_2 then
		self.normal_2 = self.root:AddChild(Widget("normal_2"))
		self.normal_2:SetPosition(-80, 0,0)
		self.normal_2:SetScale(0.7,0.7,0.7)	
		--动画部分
		self.normal_2.anim = self.normal_2:AddChild(UIAnim())
		self.normal_2.anim:GetAnimState():SetBank("normal_cd")
		self.normal_2.anim:GetAnimState():SetBuild("normal_cd")
		self.normal_2.anim:GetAnimState():PlayAnimation("anim")
		
		--文字部分
		self.normal_2.lv = self.normal_2:AddChild(Text(BODYTEXTFONT, 40))
		self.normal_2.lv:SetHAlign(ANCHOR_MIDDLE)
		self.normal_2.lv:SetPosition(0, 0, 0)
		self.normal_2.lv.num = self.owner.components.playerskill.skill.normal_2.lv
		self.normal_2.lv:SetString(tostring(self.normal_2.lv.num or nil))	
		self.normal_2.lv:SetColour(0, 0.5, 0, 0.5)
		self.normal_2.lv:Hide()	

		--初始化一些相关信息:CD指示器的动画位置和等级
			
			local duration = self.owner.components.playerskill.skill.normal_2.CDduration.A 
			local CD = self.owner.components.playerskill.skill.normal_2.CD.A 
			local lv = self.owner.components.playerskill.skill.normal_2.lv
			
			self.normal_2.percent = duration / CD
			self.normal_2.anim:GetAnimState():SetPercent("anim", self.normal_2.percent)
			self.normal_2.lv.num = lv
				
		--设置面板更新
		self.normal_2.lv.refresh =function(newlevel)
			self.normal_2.lv:SetString(tostring(newlevel or nil))	
		end
		--设置鼠标移动到上面时的状态
		self.normal_2.OnGainFocus = function()
			self:OnGainFocus(self)
			self.normal_2.lv:Show()
		end
		self.normal_2.OnLoseFocus = function()
			self:OnLoseFocus(self)
			self.normal_2.lv:Hide()	
		end
		
		--与加点有关的按钮	
		self.normal_2.point = self.normal_2:AddChild(ImageButton("images/skillpoint_button.xml", "skillpoint_button.tex", "skillpoint_button.tex", "skillpoint_button.tex"))
		self.normal_2.point:SetTooltip("A2")
		self.normal_2.point:SetPosition(-50, 0, 0)

		self.normal_2.point:SetOnClick( function() self.owner:PushEvent("PointChanged",{AR ="R",order = "normal_2"})  end)	
	end	
	--normal_3
	if self.owner and self.owner.components.playerskill and self.owner.components.playerskill.skill.normal_3 then
		self.normal_3 = self.root:AddChild(Widget("normal_3"))
		self.normal_3:SetPosition(-80, -40,0)
		self.normal_3:SetScale(0.7,0.7,0.7)	
		--动画部分
		self.normal_3.anim = self.normal_3:AddChild(UIAnim())
		self.normal_3.anim:GetAnimState():SetBank("normal_cd")
		self.normal_3.anim:GetAnimState():SetBuild("normal_cd")
		self.normal_3.anim:GetAnimState():PlayAnimation("anim")

		--文字部分
		self.normal_3.lv = self.normal_3:AddChild(Text(BODYTEXTFONT, 40))
		self.normal_3.lv:SetHAlign(ANCHOR_MIDDLE)
		self.normal_3.lv:SetPosition(0, 0, 0)
		self.normal_3.lv.num = self.owner.components.playerskill.skill.normal_3.lv
		self.normal_3.lv:SetString(tostring(self.normal_3.lv.num or nil))	
		self.normal_3.lv:SetColour(0, 0.5, 0, 0.5)
		self.normal_3.lv:Hide()	

		--初始化一些相关信息:CD指示器的动画位置和等级
			
			local duration = self.owner.components.playerskill.skill.normal_3.CDduration.A 
			local CD = self.owner.components.playerskill.skill.normal_3.CD.A 
			local lv = self.owner.components.playerskill.skill.normal_3.lv
			
			self.normal_3.percent = duration / CD
			self.normal_3.anim:GetAnimState():SetPercent("anim", self.normal_3.percent)
			self.normal_3.lv.num = lv
				
		--设置面板更新
		self.normal_3.lv.refresh =function(newlevel)
			self.normal_3.lv:SetString(tostring(newlevel or nil))	
		end
		--设置鼠标移动到上面时的状态
		self.normal_3.OnGainFocus = function()
			self:OnGainFocus(self)
			self.normal_3.lv:Show()
		end
		self.normal_3.OnLoseFocus = function()
			self:OnLoseFocus(self)
			self.normal_3.lv:Hide()	
		end
		
		--与加点有关的按钮	
		self.normal_3.point = self.normal_3:AddChild(ImageButton("images/skillpoint_button.xml", "skillpoint_button.tex", "skillpoint_button.tex", "skillpoint_button.tex"))
		self.normal_3.point:SetTooltip("A3")
		self.normal_3.point:SetPosition(-50, 0, 0)
		
		self.normal_3.point:SetOnClick( function() self.owner:PushEvent("PointChanged",{AR ="R",order = "normal_3"}) end)	
		
	end	

	--——————————————————被动指示器——————————————————
	if self.owner and self.owner.components.playerskill then	
		self.passive = self.root:AddChild(Widget("passive"))	
		self.passive:SetPosition(0,80,0)
		self.passive:SetScale(0.5,0.5,0.5)	
		
		self.passive.anim = self.passive:AddChild(UIAnim())
		self.passive.anim:GetAnimState():SetBank("passive_cd")
		self.passive.anim:GetAnimState():SetBuild("passive_cd")
		self.passive.anim:GetAnimState():PlayAnimation("anim")
		
		self.passive.CDduration = self.passive:AddChild(Text(BODYTEXTFONT, 45)) --显示CD时间
		self.passive.lv = self.passive:AddChild(Text(BODYTEXTFONT, 40)) --显示技能等级
		
		local duration = self.owner.components.playerskill.skill.passive.CDduration.P 
		local CD = self.owner.components.playerskill.skill.passive.CD.P 
		local lv = self.owner.components.playerskill.skill.passive.lv
		self.passive.percent = duration / CD

		self.passive.anim:GetAnimState():SetPercent("anim", self.passive.percent)
		
		self.passive.CDduration.duration = duration
		self.passive.CDduration:SetPosition(5, -30, 0)
		self.passive.CDduration:SetString("CD:"..tostring(self.passive.CDduration.duration or 0))	

		self.passive.lv.lv = lv
		self.passive.lv:SetPosition(5, 0, 0)
		self.passive.lv:SetString("Lv:"..tostring(self.passive.lv.lv or 1))	
			
		refresh(self.passive,true)
		
		self.passive.lv:Hide()
		
		self.passive.OnGainFocus = function()
			self:OnGainFocus(self)
			self.passive.lv:Show()
		end
		self.passive.OnLoseFocus = function()
			self:OnLoseFocus(self)
			self.passive.lv:Hide()
		end		
	
	end
	
	--用HUDChanged事件，控制所有跟这个面板有关的变化。
	self.OnHUDChangedFn = function(owner,data) self:OnHUDChanged(owner,data) end
	self.owner:ListenForEvent("HUDChanged", self.OnHUDChangedFn)
	
	--——————————————显示等级和经验	
	if self.owner.components and self.owner.components.level and self.owner.components.level.exp then
		self.level = self.root:AddChild(Widget("level"))
		
		self.level.lv = self.level:AddChild(Text(BODYTEXTFONT, 40))
		self.level.lv:SetHAlign(ANCHOR_MIDDLE)
		self.level.lv:SetPosition(0, 145, 0)
		self.level.lv.num = self.owner.components.level.lv
		self.level.lv:SetString("Lv:"..tostring(self.level.lv.num))
		
		self.level.exp = self.level:AddChild(Text(BODYTEXTFONT, 25))
		self.level.exp:SetHAlign(ANCHOR_MIDDLE)
		self.level.exp:SetPosition(0,120, 0)
		self.level.exp.num = self.owner.components.level.exp
		self.level.exp:SetString("EXP:"..tostring(self.level.exp.num).."/"..tostring(self.level.lv.num*25))	
		--以下用于面板刷新
		self.level.refresh =  function(exp,lv)  
			if lv then
				self.level.lv.num = lv
				self.level.lv:SetString("Lv:"..tostring(self.level.lv.num))
			end	
			if exp then 
				if self.level.lv.num == 18 then
					self.level.exp:SetString("EXP:--")
				else
					self.level.exp.num = exp
					self.level.exp:SetString("EXP:"..tostring(self.level.exp.num).."/"..tostring(self.level.lv.num*25))		
				end
			end
		end
	
	end
	
	
end)
function RPG_State:OnGainFocus()
    RPG_State._base.OnGainFocus(self)
end

function RPG_State:OnLoseFocus()
    RPG_State._base.OnLoseFocus(self)
end


--总处理函数
function RPG_State:OnHUDChanged(owner,data)
	if data then
		if data.tag then
			--如果是技能方面的
			if data.tag == "Skill" and data.tag2 then
				--如果是CD
				if data.tag2 == "CD" then
					self:CDDelta(data)
				end
				if data.tag2 == "level" then

					self:OnSkillLevel(data)
				end
			end
			if data.tag =="Level" then
				if data.tag2 == "exp" then
					self.level.refresh(data.nexp)
				end
				if data.tag2 == "level" then
					self.level.refresh(data.surplus,data.level)
				end
			end
		end
	end
end
--CD有关的，统一放到这里处理。
local function animSetPercent (self, val, num)
    local val = val or 0
    self.anim:GetAnimState():SetPercent("anim", val)
	if self.CDduration  then
	    self.CDduration.duration = num or 0
		self.CDduration:SetString("CD:"..tostring(self.CDduration.duration))
    end
end
function RPG_State:CDDelta(data)
	if data and data.newpercent and data.order and data.AP then
		local np = data.newpercent	
		local order = data.order
		local AP = data.AP
		if order == "ultimate" and AP == "A" and data.duration then
			local duration = data.duration
			animSetPercent(self.ultimate,np,duration)
			refresh(self.ultimate,true)
		end
		if order == "passive" and AP == "P" and data.duration then
			local duration = data.duration
			animSetPercent(self.passive,np,duration)
			refresh(self.passive,true)
		end
		if AP == "A" and data.duration and (order == "normal_1" or order == "normal_2" or order == "normal_3") then 
			animSetPercent(self[order],np)
		end
	end
end

--与技能有关的，统一放到这里
local function AddPoint(self,Valid)
	for k,v in pairs(Valid) do
		if Valid[k] ==true then
			self[k].point:Show()
		else
			self[k].point:Hide()
		end
	end
end
local function RefreshSkill(self,order,Lv)--更新等级显示
	if self[order].lv.lv then
		self[order].lv.lv = Lv
		self[order].lv:SetString("Lv:"..tostring(self[order].lv.lv or nil))
	elseif self[order].lv.num then
		self[order].lv.num = Lv	
		self[order].lv:SetString(tostring(self[order].lv.num or nil))
	end
end
function RPG_State:OnSkillLevel(data)
	local order = data.order
	local Lv = data.NewSkillLv
	if data.HS then	
		local Valid = data.Valid
		AddPoint(self,Valid)
		if order and Lv then
			RefreshSkill(self,order,Lv)
		end
		return
	end
	if order =="passive" then
		RefreshSkill(self,order,Lv)
		return
	end

end


function RPG_State:OnUpdate()

end
return RPG_State

