
------------- -------大结算UI  ----------------------
local CloseUI                    --关闭UI
local CleanUp                    --清除
--local CreJixuTipsUIFun           --pushu准备房主解散提示
local RegisterEventListen		 -- 注册事件监听
local UnRegisterEventListen		 -- 取消事件监听
local m_BaseNode    
local m_headDateInfoTb           --头像信息
local m_sendRead                 --发送准备
local m_PushCount                --点击次数 
local m_bigfailreasnValue        --房主  
local m_bigShareurl              --大结算url
local m_fuzhiBtn                 --复制按钮   
CleanUp=function (  )
	m_BaseNode     = nil
	m_headDateInfoTb ={}
	m_sendRead    =false
	m_PushCount   =0
	m_bigfailreasnValue  =0
    m_bigShareurl        =nil
    m_fuzhiBtn           =nil
end

CloseUI=function ( ... )
	if m_BaseNode  then 
		m_BaseNode:removeFromParent(true)
		m_BaseNode=nil
	end
	CleanUp()
    UnRegisterEventListen()
end
---创建ui
function LzDaer_M_CreateBigBlanceUI(_prent,s )
	CCLOG("LzDaer_M_CreateBigBlanceUI") 
	SHOW(s)
	if not _prent  then return  end
	CleanUp()
    Pt_M_UnShowTalKFun()
	m_BaseNode = cc.NodeRGBA:create()
	_prent:addChild(m_BaseNode,105)
    local mask = xui.creMaskLy( m_BaseNode, nil, nil, nil, function (  )
    	--CloseUI()
	end )
    local bg = xui.addSingleResSprite(m_BaseNode, "lzde_jsb_bg.jpg", sys.center.x, sys.center.y)
    local screenSize = CCDirector:getInstance():getWinSize()
    if(screenSize.width / screenSize.height > 1334 / 750)then
        bg:setScaleX((screenSize.width / screenSize.height) / (1334 / 750))
    end
    local namres = "lzde_sjd_name"
    if DEF_DAER_GAME_ID ==99 then 
       namres = "jade_sjd_name"
    elseif DEF_DAER_GAME_ID==100 then
    	namres = "clde_sjd_name"
    end
    CCLOG("DEF_DAER_GAME_ID",DEF_DAER_GAME_ID,"namres",namres)
    xui.addSprite(m_BaseNode,namres,sys.center.x,sys.height-60)
    local bg2 =  xui.addSprite(m_BaseNode,"lzde_dbjb",sys.center.x,sys.center.y+60)
    local tishsiSp = xui.addSprite(m_BaseNode,"lzde_jjdb_qz",sys.width-110,sys.height-55)
    local jian_x = 30
    local koulingStr=Pt_M_GetRoomInfoTb().deskcode or 0
    local hostid = Pt_M_GetFangZhuPlayerId()
    local jushu = s.curturn+s.lastturn
    local koulingLabel = xui.addLabel( bg2,"口令："..koulingStr, sys.creColor3( 253,231,110 ), 26, bg2:getContentSize().width/2+145, bg2:getContentSize().height-60, sys.ap.leftCenter)
    local koulingLabel = xui.addLabel( bg2, "局数：".. tostring(jushu), sys.creColor3( 253,231,110 ), 26, bg2:getContentSize().width/2+320, bg2:getContentSize().height-60, sys.ap.leftCenter)
    local bigWinTb =s.costlist  or {}
    local bigSptb = s.bigwin    or {}
    local startx =sys.center.x-390
    local jian_x = 390
    local bgsize=bg2:getContentSize()
    local straty = bgsize.height/2+100
    local  huMaxValueTb=s.ex.maxbalval or  {}
    -- CCLOG("huMaxValueTb")
    -- SHOW(huMaxValueTb)
    local  function GetIsBigWin( playerid )
		local isbig = false
		for k,v in pairs(bigSptb) do
			if v.playerid and v.playerid ==playerid then
				isbig=true
			end
		end
		return isbig
    end
    for k1,v1 in pairs(s.player or {}) do
    	local bgLayer = xui.addSprite(m_BaseNode,"lzde_djs_bg",startx+jian_x*(k1-1),straty)
    	local headbg  =xui.addSprite(bgLayer,"gframe/yxgy_tx_mr",bgLayer:getContentSize().width/2,bgLayer:getContentSize().height-80)

        if v1.headurl then
           local m_headSp = urlImageClass:create({url =v1.headurl,tag = v1.playerid,size = {width=78,height=78}})
           headbg:addChild(m_headSp)
           m_headSp:setPosition(headbg:getContentSize().width/2,headbg:getContentSize().height/2)
        end
		local headkuang = xui.addSprite(bgLayer,"gframe/yxgy_tx_txk",bgLayer:getContentSize().width/2,bgLayer:getContentSize().height-80)
		local wxname = v1.nickname
		if wxname ~= nil and string.utf8len(wxname) > 4 then
		      wxname = string.utf8sub(wxname,1,4).."…"
	    end
		local namstr = wxname
		local name = xui.addLabel(bgLayer,namstr,sys.colors.white,26,bgLayer:getContentSize().width/2,bgLayer:getContentSize().height/2+85) 
		local IDLable = xui.addLabel(bgLayer,"ID:"..v1.playerid,sys.colors.white,26,bgLayer:getContentSize().width/2,bgLayer:getContentSize().height/2+40)

        if  Pt_GetPlayerInfo().playerid==v1.playerid then 
            local  headselfSp = xui.addSprite(bgLayer,"lzde_head_kuang",bgLayer:getContentSize().width/2,bgLayer:getContentSize().height-120)
        end
        if GetIsBigWin(v1.playerid) then
	        local dayingjiaSp = xui.addSprite(bgLayer,"lzde_dyj_red",bgLayer:getContentSize().width-70,bgLayer:getContentSize().height-70)
        end
        local zuigaoNum = huMaxValueTb[v1.playerid] or 0
        if zuigaoNum >0 then zuigaoNum="+"..tostring(zuigaoNum) elseif zuigaoNum<0 then  zuigaoNum="-"..tostring(math.abs(zuigaoNum)) end
        local name = xui.addLabel(bgLayer,"单局最高分数："..zuigaoNum,cc.c3b(65, 32,32),26,bgLayer:getContentSize().width/2,bgLayer:getContentSize().height/2-20) 
		local money = v1.curcoin
  	    local jiares = "lzde_jdnum1_+"
 	    local fullres = "lzde_jdnum1_"
 	    if money<0 then
            jiares = "lzde_jdnum2_-"
            fullres = "lzde_jdnum2_"
 	    end
        local moneySp= xui.addNumPicByName(bgLayer,math.abs(money),fullres,bgLayer:getContentSize().width/2+15,120)
        if money~=0 then
	 	   local jiaSp=xui.addSprite(moneySp,jiares,20,moneySp:getContentSize().height/2,sys.ap.rightCenter)
	 	end
 	    for k2,v2 in pairs(bigWinTb) do
        	if  v1.playerid==v2.playerid  then
               local fangKbg=   xui.addSprite(bgLayer,"lzde_js_fkbj",bgLayer:getContentSize().width/2,35)
               local fangkaBg = xui.addSprite(bgLayer,"lzde_js_fk",bgLayer:getContentSize().width/2-50,35)
        	   local moneylab=string.format("%0.1f",tonumber(v2.changecard)/10)
        	   local num = xui.addLabel( fangkaBg, moneylab, sys.creColor3( 255,255,255 ), 30,fangkaBg:getContentSize().width-5,fangkaBg:getContentSize().height/2, sys.ap.leftCenter)                       
        	end
        end
    end
    local gamestate = M_public_SetGameState()
	local startx=sys.width-330
	local straty = sys.center.y+50
	local offset_y =150
    local haveSend=Pt_M_GetRoomInfoTb().share or 0
    CCLOG("haveSend",haveSend)
    local offx1 = 0
    local offx2 = 0
    if haveSend==1 then
        offx1=-70
        offx2 =70
        local playerinfo  = {}
        for k1,v1 in pairs(s.player or {}) do
            local infotb={}
            infotb.userid=v1.playerid
            infotb.score=v1.curcoin
            infotb.playername=v1.nickname
            playerinfo[k1]=infotb
        end
         m_fuzhiBtn = MyBtnClass:new("gframe/yxgy_btn_fzcj_zc","gframe/yxgy_btn_fzcj_dj","gframe/yxgy_btn_fzcj_zc",function ( ... )
            CCLOG("push m_fuzhiBtn")
            if m_bigShareurl then
                local gamename = Pt_M_GetRoomInfoTb().deskparam.cname or ""
                local deskcode =Pt_M_GetRoomInfoTb().deskcode
                local datetime = tostring(GetFormatTime(os.time()))
                datetime=string.gsub(datetime,"%.","-")
                CCLOG("ostimeostime",datetime)
                copyBusinessCardToClipboard(gamename, deskcode, datetime, playerinfo,m_bigShareurl)
            else
                CCLOG("m_bigShareurl",m_bigShareurl)
                SendGameServer(emFunction_Game, 157, {})
            end
        end)
        m_BaseNode:addChild(m_fuzhiBtn.btnLy)
        m_fuzhiBtn.btnLy:setPosition(sys.center.x-m_fuzhiBtn.btnLy:getContentSize().width/2,20)
        m_fuzhiBtn.btnLy:setName("m_fuzhiBtn")
        -- xui.setSpriteGray(m_fuzhiBtn.disableSp, true)
        -- m_fuzhiBtn.SetMyBtnEnable(false)
    end

	local res1,res2,res3 = "lzde_btn_fxcj_zc","lzde_btn_fxcj_dj","lzde_btn_fxcj_zc"
    if(sys.checkSupportKeLiao and sys.checkSupportKeLiao())then
        res1,res2,res3 = "gframe/yxgy_new_btn_fxcj_zc","gframe/yxgy_new_btn_fxcj_dj","gframe/yxgy_new_btn_fxcj_zc"
    end
	local fenxiangChengjiBtn = MyBtnClass:new(res1,res2,res3,function ( )
		Pt_Public_ChoniceWXUi()
	end)
	m_BaseNode:addChild(fenxiangChengjiBtn.btnLy)
	fenxiangChengjiBtn.btnLy:setPosition(sys.center.x-470+offx1,20)
    --审核 iOS
    if  CHECK_VER_FLAG_CONTROL ~=nil  then 
        fenxiangChengjiBtn.SetMyBtnVisible(false)
    end
	local backDaingBtn = MyBtnClass:new("lzde_btn_thdt_zc","lzde_btn_thdt_dj","lzde_btn_thdt_zc",function ( ... )
		 -- 点击退出是提示弹窗
		m_PushCount=m_PushCount+1
	    CCLOG("push 大结算  返回大厅",m_PushCount)
		local icanHost = Pt_M_GetIsBigHost()
		CCLOG("icanHost",icanHost)
		if icanHost ==false then	   
		    Pt_M_SetGoOutCondition( true )
		    Pt_M_BigJieSuanPushBackCB()
		else
			CloseUI()
            Pt_M_BigJieSuanPushBackCB()
            Pt_M_SetIsBigHost(false)
		end	
	end,nil,nil,nil,nil,nil,nil,nil,true)
	m_BaseNode:addChild(backDaingBtn.btnLy)
	backDaingBtn.btnLy:setPosition(sys.center.x+150+offx2,20)
    RegisterEventListen()
end

RegisterEventListen=function (  )
    --设置玩家状态
    local  function share_link_Fun( key,s )
        CCLOG("share_link_Fun")
        local m_code = s.code or 1
        if m_code ==0 then 
            m_bigShareurl=s.shorturl  or nil
        else
            CCLOG("请求失败")
            tips.creUpTimeTips(s.msg)
        end
    end
    Pt_EventMgr:register( Pt_Events.SC_SHARE_LINK, "Ydr_Big", share_link_Fun )
end

UnRegisterEventListen=function (  )
    Pt_EventMgr:unregister( Pt_Events.SC_SHARE_LINK, "Ydr_Big" )
end

function LzDaer_M_CleanUpBigBalanceUi(  )
	CleanUp()
end
--关闭大结算
function LzDaer_M_ColseBigUI(  )
	Pt_M_GameJieSanTips()

	CloseUI()
end