
local CleanUp
local Close
local CreateUI
local RegisterEventListen
local unRegisterEventListen

--变量
local m_BaseNode = nil
local m_GameType
local m_GameTypeIndex = -1
local m_GameInfo = {}
local m_RoundBtn = {}
local m_MultiBtn = {}
local m_GameBtn = {}
local m_setInfo = nil
local m_singleRuleBtn = {}
local m_singleRuleBtn2 = {}
local m_playWayBtn = {}
local m_ShowNode = nil
local m_SelectType = 1
local m_DeskType = 0
local m_fangzhuBtn = nil
local m_winnerBtn = nil
local m_costTypeFocus = nil
local m_NeedCardLabel = nil
local m_connectIndex = 0

function Pt_M_getGameRuleSetGameType()
	return m_GameType
end

--全局函数
function Pt_M_CreGameRuleSetUI(gameType)
	if(m_BaseNode ~= nil)then
		return
	end

	CCLOG("Pt_M_CreGameRuleSetUI gameType = " .. gameType)
	CleanUp()
	-- 获取当前场景
	local sDirector = CCDirector:getInstance()
	local currScene = sDirector:getRunningScene()
	m_GameType = gameType

	m_BaseNode = cc.NodeRGBA:create()
	currScene:addChild(m_BaseNode)
	Pt_M_UIAni_RightMoveIn(m_BaseNode)
	CreateUI()

	RegisterEventListen()


	local function onEnter( )
		
	end

	local function onExit( )
		CleanUp()
	end

	local function onNodeEvent(event)
        if "enter" == event then
            onEnter()
        elseif "exit" == event then
            onExit()
        end
    end
    m_BaseNode:registerScriptHandler(onNodeEvent)
end


function Pt_M_GetCurGameRuleSetting()
	local info = m_GameInfo[m_GameTypeIndex]
	if(m_setInfo == nil or info == nil)then
		return nil
	end

	local rtn = {}
	rtn.turn = info.tlist[m_setInfo.turn].turn

	if(m_setInfo.multi <= #info.mlist)then
		rtn.multi = info.mlist[m_setInfo.multi].multi
	else
		if(info.mllist ~= nil)then
			rtn.multi = info.mllist[1].mlist[m_setInfo.multi - #info.mlist].multi
		end
	end
	
	rtn.deducttype = m_DeskType
	if(info.plist ~= nil and m_setInfo.ptype ~= nil)then
		rtn.type = info.plist[m_setInfo.ptype].type
	end
	
	if(info.plist2 ~= nil and m_setInfo.ptype2 ~= nil)then
		rtn.type2 = info.plist2[m_setInfo.ptype2].type
	end
	rtn.rule = {}
	for i = 1, #m_playWayBtn do
		if(m_playWayBtn[i].GetIsSelect())then
			table.insert(rtn.rule, info.rlist[i].type)
		end
	end
	
	return rtn
end

--保存创建房间设置
local function saveCreateSetting()
	local info = m_GameInfo[m_GameTypeIndex]

	local settingInfo = {}
	settingInfo.m_GameTypeIndex = m_GameTypeIndex
	settingInfo.turn = info.tlist[m_setInfo.turn].turn 				--轮数选择
	if(m_setInfo.multi <= #info.mlist)then
		settingInfo.multi = info.mlist[m_setInfo.multi].multi 			--番数选择
	else
		if(info.mllist ~= nil)then
			settingInfo.multi = info.mllist[1].mlist[m_setInfo.multi - #info.mlist].multi 			--番数选择
		end
	end
	
	settingInfo.deducttype = m_DeskType 			--支付模式
	if(info.plist ~= nil and m_setInfo.ptype ~= nil)then 		--单选规则1
		settingInfo.type = info.plist[m_setInfo.ptype].type
	end
	if(info.plist2 ~= nil and m_setInfo.ptype2 ~= nil)then      --单选规则2
		settingInfo.type2 = info.plist2[m_setInfo.ptype2].type
	end
	settingInfo.rule = {}
	for i = 1, #m_playWayBtn do 						--多选规则
		if(m_playWayBtn[i].GetIsSelect())then
			table.insert(settingInfo.rule, info.rlist[i].type)
		end
	end

	local ruleStr = json.encode(settingInfo)

	local m_UserDefault = cc.UserDefault:getInstance()
	m_UserDefault:setStringForKey("ruleset_" .. info.gametype, ruleStr)
	CCLOG("saveCreateSetting ruleStr = " .. ruleStr)
end

CleanUp = function (  )
	m_BaseNode = nil
	m_GameType = nil

	m_setInfo = nil
	m_NeedCardLabel = nil
	unRegisterEventListen()
end

Close = function ( )
	if m_BaseNode then
		m_BaseNode:removeFromParent(true)
		m_BaseNode = nil
	end 
	CleanUp()
end

--点击创建房间
local function clickCreateRoom(isCheckRecconect)
	local info = m_GameInfo[m_GameTypeIndex]
	if(info == nil)then
		return
	end

	if(info.tlist[m_setInfo.turn] == nil)then
		return
	end

	local needCard = info.tlist[m_setInfo.turn].roomcard
	if(needCard > Pt_GetPlayerInfo()[Def_Player_Money])then
		local function _showChongzhiTip()
			local sDirector = CCDirector:getInstance()
			local currScene = sDirector:getRunningScene()
			if nil == currScene then return end
			local m_ChongzhiTipLayer = CCLayer:create()
			currScene:addChild(m_ChongzhiTipLayer,PT_PUBLIC_TIPS_ZORDER)

			local mask = xui.creMaskLy( m_ChongzhiTipLayer )

			xui.addSpr9(m_ChongzhiTipLayer, "xxqp_bg_0", 510, 276, sys.center.x, sys.center.y)
			Pt_M_UIAni_ScaleOut(m_ChongzhiTipLayer)
			local frameSp = xui.addSprite(m_ChongzhiTipLayer, "xxqp_grzx_bt", sys.center.x, sys.center.y + 276 / 2 - 15)
			xui.addSprite(frameSp, "xxqp_gmfk_title", frameSp:getContentSize().width / 2, frameSp:getContentSize().height / 2)

			local tipstr = "充值信息获取中..."
			if(Pt_GetMyCurAreainfo() ~= nil and Pt_GetMyCurAreainfo().paydesc ~= nil)then
				tipstr = Pt_GetMyCurAreainfo().paydesc
			end
			local descLabel = xui.addLabel(m_ChongzhiTipLayer, tipstr, cc.c3b(255, 255, 255), 24, sys.center.x, sys.center.y + 50)
			descLabel:setDimensions({width = 460, height = 0})
			descLabel:setHorizontalAlignment(kCCTextAlignmentCenter)
			descLabel:setTag(10)

			local sureBtn = MyBtnClass:new("xxqp_xzdq_qd_zc","xxqp_xzdq_qd_dj","",function (  )
																	local function _closeCall()
																		m_ChongzhiTipLayer:removeFromParent()
																		m_ChongzhiTipLayer = nil
																	end
																	Pt_M_UIAni_ScaleIn(m_ChongzhiTipLayer, _closeCall)
																	
																end,nil,nil,nil)
			m_ChongzhiTipLayer:addChild(sureBtn.btnLy)
			sureBtn.btnLy:setPosition(sys.width / 2 - sureBtn.btnLy:getContentSize().width / 2, sys.center.y - 100)

			local closeBtn = MyBtnClass:new("xxqp_cancel_zc","xxqp_cancel_dj","",function (  )
																	local function _closeCall()
																		m_ChongzhiTipLayer:removeFromParent()
																		m_ChongzhiTipLayer = nil
																	end
																	Pt_M_UIAni_ScaleIn(m_ChongzhiTipLayer, _closeCall)
																	
																end,nil,nil,nil)
			m_ChongzhiTipLayer:addChild(closeBtn.btnLy)
			closeBtn.btnLy:setPosition(sys.width / 2 + 225 - closeBtn.btnLy:getContentSize().width / 2, sys.center.y + 88)
		end
		local tipBg = tips.creMOKMaskTips( "", cc.c3b(255, 255, 255), 26, true, nil, _showChongzhiTip,nil, nil, nil, true , true)
		xui.addSprite(tipBg, "xxqp_hp_title_fk", 0, 60)
		return
	end

	tips.creMaskTips("请稍候")
	g_SetIsCreateRoom(true)

	CCLOG("点击创建游戏time = " .. os.time())

	local m_GameServerList = Pt_Get_GameServerList()
	local ip = info["ip"]
	local port = info["port"]

	--随机选择负载较小的服务器
	local m_serverlist = {}
	for k,v in pairs(m_GameServerList) do
		if(v.gameid == info.gameid and v.gametype == info.gametype)then
			table.insert(m_serverlist, v)
		end
	end
	local function _sortByFreedesk(a, b)
		return a.freedesknum > b.freedesknum
	end
	table.sort(m_serverlist, _sortByFreedesk)

	local limitDeskCnt = 500  --(500张桌子*4人 = 2000)
	local passage = math.random(500)
	local deskNum = 0
	local isIn = false
	for i = 1, #m_serverlist do
		deskNum = deskNum + m_serverlist[i].freedesknum
		if(deskNum > passage)then
			isIn = true
			ip = m_serverlist[i].ip
			port = m_serverlist[i].port
			m_connectIndex = i
			break
		end
	end
	if(isIn == false)then
		local rdcnt = 10
		if(#m_serverlist < 10)then
			rdcnt = #m_serverlist
		end
		local idx = math.random(rdcnt)
		if(isCheckRecconect == true)then
			while(idx == m_connectIndex)do
				idx = math.random(rdcnt)
			end
		end
		
		ip = m_serverlist[idx].ip
		port = m_serverlist[idx].port
		m_connectIndex = idx
	end


	--选择负载最小的服务器
	--[[
	for k,v in pairs(m_GameServerList) do
		if(v.serverid ~= info.serverid and v.gameid == info.gameid and v.gametype == info.gametype)then
			if(v.freedesknum > info.freedesknum)then
				ip = v.ip
				port = v.port
				info = v
			elseif(v.freedesknum == info.freedesknum and v.playernum < info.playernum)then
				ip = v.ip
				port = v.port
				info = v
			end
		end
	end
	]]

	saveCreateSetting()
	CCLOG("ConnectGameServer ip = " .. ip .. "  port = " .. port)
	ConnectGameServer(ip,port)
end

function Pt_M_gameRuleReconnectGameServer()
	if(m_BaseNode ~= nil)then
		clickCreateRoom(true)
	end	
end

local function updateNeedCardLabel()
	local info = m_GameInfo[m_GameTypeIndex]
	if(info ~= nil and info.tlist[m_setInfo.turn] ~= nil)then
		local needCard = string.format("%.1f", info.tlist[m_setInfo.turn].roomcard / Pt_getCoinScale())
		if(m_NeedCardLabel ~= nil)then
			m_NeedCardLabel:setString("本轮游戏消耗" .. needCard .. "张房卡")
		end
	end
end

--创建规则界面
local textureIdx = 1
local function createRuleUI(Typeindex, setting)
	local info = m_GameInfo[Typeindex]
	if(info == nil)then
		return
	end

	m_setInfo = {}
	m_GameTypeIndex = Typeindex
	m_ShowNode:removeAllChildren(true)

	local starty = sys.center.y + 120 + (sys.height - 750) / 2
	local startx = sys.center.x - 250
	local yDis = 55


	--游戏类型
	m_GameBtn = {}
	local gameLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_yxwf", startx, starty, sys.ap.leftCenter)
	for i = 1, #m_GameInfo do
		--点击选择游戏类型
		local function clickSelectGame()
			createRuleUI(i)
		end

		local gameBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_xz",clickSelectGame)
		m_ShowNode:addChild(gameBtn.btnLy)
		gameBtn.btnLy:setPosition(startx + 180 + ((i - 1) % 3) * 170, starty - math.floor((i - 1) / 3) * yDis - gameBtn.btnLy:getContentSize().height / 2)
		local nameLabel = xui.addLabel(m_ShowNode, m_GameInfo[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + (i - 1) * 170 + 50, starty, sys.ap.leftCenter)
		if(Typeindex == i)then
			gameBtn.SetMyBtnSelect(true)
		end
	end
	starty = starty - (math.ceil(#m_GameInfo / 3)) * yDis


	local wanfaLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_gzsm", startx, starty, sys.ap.leftCenter)
	local isHaveRule = false
	--单选规则选择
	m_singleRuleBtn = {}
	if(info.plist ~= nil and #info.plist > 0)then
		isHaveRule = true
		local wanfaLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_gzsm", startx, starty, sys.ap.leftCenter)

		local xadd = 0
		for i = 1, #info.plist do
			--点击选择玩法
			local function clickSelectSingleRule()
				for j = 1, #m_singleRuleBtn do
					if(j == i)then
						m_singleRuleBtn[j].SetMyBtnSelect(true)
					else
						m_singleRuleBtn[j].SetMyBtnSelect(false)
					end
					m_setInfo.ptype = i
				end
			end
			local singleBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_xz",clickSelectSingleRule)
			m_ShowNode:addChild(singleBtn.btnLy)
			singleBtn.btnLy:setPosition(startx + 180 + xadd + ((i - 1) % 4) * 170, starty - singleBtn.btnLy:getContentSize().height / 2 - math.floor((i - 1) / 4) * yDis)
			local introLabel = xui.addLabel(m_ShowNode, info.plist[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + xadd + ((i - 1) % 4) * 170 + 50, starty - math.floor((i - 1) / 4) * yDis, sys.ap.leftCenter)
			table.insert(m_singleRuleBtn, singleBtn)
			if(introLabel:getContentSize().width > 115)then
				xadd = xadd + (introLabel:getContentSize().width - 115)
			end

			if(i == 1)then
				singleBtn.SetMyBtnSelect(true)
			end
		end
		m_setInfo.ptype = 1

		--记忆选择
		if(setting ~= nil and setting.type ~= nil)then
			for i = 1, #info.plist do
				if(info.plist[i].type == setting.type)then
					m_setInfo.ptype = i
					for j = 1, #m_singleRuleBtn do
						if(j == i)then
							m_singleRuleBtn[j].SetMyBtnSelect(true)
						else
							m_singleRuleBtn[j].SetMyBtnSelect(false)
						end
					end
					break
				end
			end
		end

		starty = starty - (math.ceil(#info.plist / 3)) * yDis
	end

	--单选规则选择2
	m_singleRuleBtn2 = {}
	if(info.plist2 ~= nil and #info.plist2 > 0)then
		isHaveRule = true
		--local wanfaLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_gzsm", startx, starty, sys.ap.leftCenter)

		for i = 1, #info.plist2 do
			--点击选择玩法
			local function clickSelectSingleRule()
				for j = 1, #m_singleRuleBtn2 do
					if(j == i)then
						m_singleRuleBtn2[j].SetMyBtnSelect(true)
					else
						m_singleRuleBtn2[j].SetMyBtnSelect(false)
					end
					m_setInfo.ptype2 = i
				end
			end
			local singleBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_xz",clickSelectSingleRule)
			m_ShowNode:addChild(singleBtn.btnLy)
			singleBtn.btnLy:setPosition(startx + 180 + ((i - 1) % 4) * 170, starty - singleBtn.btnLy:getContentSize().height / 2 - math.floor((i - 1) / 4) * yDis)
			local introLabel = xui.addLabel(m_ShowNode, info.plist2[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + ((i - 1) % 4) * 170 + 50, starty - math.floor((i - 1) / 4) * yDis, sys.ap.leftCenter)
			table.insert(m_singleRuleBtn2, singleBtn)

			if(i == 1)then
				singleBtn.SetMyBtnSelect(true)
			end
		end
		m_setInfo.ptype2 = 1

		--记忆选择
		if(setting ~= nil and setting.type2 ~= nil)then
			for i = 1, #info.plist2 do
				if(info.plist2[i].type == setting.type2)then
					m_setInfo.ptype2 = i
					for j = 1, #m_singleRuleBtn2 do
						if(j == i)then
							m_singleRuleBtn2[j].SetMyBtnSelect(true)
						else
							m_singleRuleBtn2[j].SetMyBtnSelect(false)
						end
					end
					break
				end
			end
		end

		starty = starty - (math.ceil(#info.plist2 / 4)) * yDis
	end

	--多选规则选择
	m_playWayBtn = {}
	if(info.rlist ~= nil and #info.rlist > 0)then
		local col = 4
		if(#info.rlist > 6)then
			startx = startx - 170
			col = 4
		end
		isHaveRule = true
		for i = 1, #info.rlist do
			--点击选择玩法
			local function clickSelectPlayway()
				if(info.mllist ~= nil)then
					local isChoose = false
					for k = 1, #m_playWayBtn do
						if(m_playWayBtn[k].GetIsSelect() and info.rlist[k].name == info.mllist[1].name)then
							isChoose = true
							break
						end
					end
					if(isChoose)then
						for i = 1, #info.mlist do
							m_MultiBtn[i].btnLy:setVisible(false)
							m_MultiBtn[i].SetMyBtnTouchEnable(false)
							m_MultiBtn[i].SetMyBtnSelect(false)
						end
						for i = #info.mlist + 1, #m_MultiBtn do
							m_MultiBtn[i].btnLy:setVisible(true)
							m_MultiBtn[i].SetMyBtnTouchEnable(true)
						end
						if(m_setInfo.multi <= #info.mlist)then
							m_setInfo.multi = #info.mlist + 1
							m_MultiBtn[#info.mlist + 1].SetMyBtnSelect(true)
						end
					else
						for i = 1, #info.mlist do
							m_MultiBtn[i].btnLy:setVisible(true)
							m_MultiBtn[i].SetMyBtnTouchEnable(true)
						end
						for i = #info.mlist + 1, #m_MultiBtn do
							m_MultiBtn[i].btnLy:setVisible(false)
							m_MultiBtn[i].SetMyBtnTouchEnable(false)
							m_MultiBtn[i].SetMyBtnSelect(false)
						end
						if(m_setInfo.multi > #info.mlist)then
							m_setInfo.multi = 1
							m_MultiBtn[1].SetMyBtnSelect(true)
						end
					end
				end
			end
			local playwayBtn = MySelectBtnClass:new("xxqp_cjfj_wgx","xxqp_cjfj_gx",clickSelectPlayway)
			m_ShowNode:addChild(playwayBtn.btnLy)

			playwayBtn.btnLy:setPosition(startx + 180 + ((i - 1) % col) * 170, starty - playwayBtn.btnLy:getContentSize().height / 2 - math.floor((i - 1) / col)*yDis)
			local introLabel = xui.addLabel(m_ShowNode, info.rlist[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + ((i - 1) % col) * 170 + 50, starty - math.floor((i - 1) / col)*yDis, sys.ap.leftCenter)

			-- if(m_GameType == 9)then
			-- 	--泸州大二
			-- 	if(i <= 2)then
			-- 		playwayBtn.btnLy:setPosition(startx + 180 + ((i - 1) % col) * 170, starty - playwayBtn.btnLy:getContentSize().height / 2 - math.floor((i - 1) / col)*yDis)
			-- 		local introLabel = xui.addLabel(m_ShowNode, info.rlist[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + ((i - 1) % col) * 170 + 50, starty - math.floor((i - 1) / col)*yDis, sys.ap.leftCenter)
			-- 	else
			-- 		local posIndex = i + 1
			-- 		playwayBtn.btnLy:setPosition(startx + 180 + ((posIndex - 1) % col) * 170, starty - playwayBtn.btnLy:getContentSize().height / 2 - math.floor((posIndex - 1) / col)*yDis)
			-- 		local introLabel = xui.addLabel(m_ShowNode, info.rlist[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + ((posIndex - 1) % col) * 170 + 50, starty - math.floor((posIndex - 1) / col)*yDis, sys.ap.leftCenter)
			-- 	end
			-- else
			-- 	playwayBtn.btnLy:setPosition(startx + 180 + ((i - 1) % col) * 170, starty - playwayBtn.btnLy:getContentSize().height / 2 - math.floor((i - 1) / col)*yDis)
			-- 	local introLabel = xui.addLabel(m_ShowNode, info.rlist[i].name, cc.c3b(255, 255, 255), 24, startx + 180 + ((i - 1) % col) * 170 + 50, starty - math.floor((i - 1) / col)*yDis, sys.ap.leftCenter)
			-- end
			
			table.insert(m_playWayBtn, playwayBtn)

			if(setting ~= nil and setting.rule ~= nil)then
				for j = 1, #setting.rule do
					if(setting.rule[j] == info.rlist[i].type)then
						playwayBtn.SetMyBtnSelect(true)
						break
					end
				end
			else
				if(info.rlist[i].default ~= nil and info.rlist[i].default == 1)then
					playwayBtn.SetMyBtnSelect(true)
				end
			end

		end

		if(#info.rlist > 6)then
			startx = startx + 170
		end
		starty = starty - (math.ceil(#info.rlist / col)) * yDis
	end

	if(isHaveRule == false)then
		wanfaLabel:removeFromParent(true)
	end


	--番数
	m_MultiBtn = {}
	if(info.mlist ~= nil and #info.mlist > 0)then
		local fanshuLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_zdfs", startx, starty, sys.ap.leftCenter)
		local off_x = 0

		for i = 1, #info.mlist do
			--点击选择番数
			local function clickSelectMulti()
				for j = 1, #m_MultiBtn do
					if(j == i)then
						m_MultiBtn[j].SetMyBtnSelect(true)
					else
						m_MultiBtn[j].SetMyBtnSelect(false)
					end
				end
				m_setInfo.multi = i
			end

			local multiBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_xz",clickSelectMulti)
			m_ShowNode:addChild(multiBtn.btnLy)
			multiBtn.btnLy:setPosition(startx + 180 + (i - 1) * 130 + off_x, starty - multiBtn.btnLy:getContentSize().height / 2)

			local fantext = ""
			if(info.mlist[i].multi == 0)then
				if(info.mlist[i].name == nil)then
					fantext = "不限番"
				else
					fantext = "不限" .. info.mlist[i].name
				end
			else
				if(info.mlist[i].name == nil)then
					fantext = info.mlist[i].multi .. "番"
				else
					fantext = info.mlist[i].multi .. info.mlist[i].name
				end
			end

			local introLabel = xui.addLabel(multiBtn.btnLy, fantext, cc.c3b(255, 255, 255), 24, 50, 26, sys.ap.leftCenter)
			if(i == 1)then
				multiBtn.SetMyBtnSelect(true)
			end
			table.insert(m_MultiBtn, multiBtn)

			if(introLabel:getContentSize().width > 80)then
				off_x = off_x + (introLabel:getContentSize().width - 80)
			end
		end


		--mllist
		if(info.mllist ~= nil)then
			for i = 1, #info.mllist[1].mlist do

				--点击选择番数
				local function clickSelectMulti()
					for j = 1, #m_MultiBtn do
						if(j == i + #info.mlist)then
							m_MultiBtn[j].SetMyBtnSelect(true)
						else
							m_MultiBtn[j].SetMyBtnSelect(false)
						end
					end
					m_setInfo.multi = i + #info.mlist
				end

				local multiBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_xz",clickSelectMulti)
				m_ShowNode:addChild(multiBtn.btnLy)
				multiBtn.btnLy:setPosition(startx + 180 + (i - 1) * 130, starty - multiBtn.btnLy:getContentSize().height / 2)

				local fantext = ""
				if(info.mllist[1].mlist[i].multi == 0)then
					if(info.mllist[1].mlist[i].name == nil)then
						fantext = "不限番"
					else
						fantext = "不限" .. info.mllist[1].mlist[i].name
					end
				else
					if(info.mllist[1].mlist[i].name == nil)then
						fantext = info.mllist[1].mlist[i].multi .. "番"
					else
						fantext = info.mllist[1].mlist[i].multi .. info.mllist[1].mlist[i].name
					end
				end

				local introLabel = xui.addLabel(multiBtn.btnLy, fantext, cc.c3b(255, 255, 255), 24, 50, 26, sys.ap.leftCenter)
				if(i == 1)then
					multiBtn.SetMyBtnSelect(true)
				end
				table.insert(m_MultiBtn, multiBtn)
			end

			local isChoose = false
			for k = 1, #m_playWayBtn do
				if(m_playWayBtn[k].GetIsSelect() and info.rlist[k].name == info.mllist[1].name)then
					isChoose = true
					break
				end
			end
			if(isChoose)then
				for i = 1, #info.mlist do
					m_MultiBtn[i].btnLy:setVisible(false)
					m_MultiBtn[i].SetMyBtnTouchEnable(false)
				end
			else
				for i = #info.mlist + 1, #m_MultiBtn do
					m_MultiBtn[i].btnLy:setVisible(false)
					m_MultiBtn[i].SetMyBtnTouchEnable(false)
				end
			end
		end

		m_setInfo.multi = 1

		--记忆选择
		if(setting ~= nil and setting.multi ~= nil)then
			local isChoose = false
			for k = 1, #m_playWayBtn do
				if(m_playWayBtn[k].GetIsSelect() and info.mllist ~= nil and info.rlist[k].name == info.mllist[1].name)then
					isChoose = true
					break
				end
			end
			if(isChoose)then
				for j = 1, #info.mllist[1].mlist do
					if(info.mllist[1].mlist[j].multi == setting.multi)then
						m_setInfo.multi = j + #info.mlist
						m_MultiBtn[1 + #info.mlist].SetMyBtnSelect(false)
						m_MultiBtn[j + #info.mlist].SetMyBtnSelect(true)
					end
				end
			else
				for j = 1, #info.mlist do
					if(info.mlist[j].multi == setting.multi)then
						m_setInfo.multi = j
						m_MultiBtn[1].SetMyBtnSelect(false)
						m_MultiBtn[j].SetMyBtnSelect(true)
					end
				end
			end


			
		end

		starty = starty - yDis
	end



	--局数设置
	m_RoundBtn = {}
	if(info.tlist ~= nil and #info.tlist > 0)then
		local jushuLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_zdjs", startx, starty, sys.ap.leftCenter)
		for i = 1, #info.tlist do
			--点击选择局数
			local function clickSelectRound()
				for j = 1, #m_RoundBtn do
					if(j == i)then
						m_RoundBtn[j].SetMyBtnSelect(true)
					else
						m_RoundBtn[j].SetMyBtnSelect(false)
					end
				end
				m_setInfo.turn = i
				updateNeedCardLabel()
			end

			local roundBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_xz",clickSelectRound)
			m_ShowNode:addChild(roundBtn.btnLy)
			roundBtn.btnLy:setPosition(startx + 180 + (i - 1) * 130, starty - roundBtn.btnLy:getContentSize().height / 2)
			local introLabel = xui.addLabel(m_ShowNode, info.tlist[i].turn .. "局", cc.c3b(255, 255, 255), 24, startx + 180 + (i - 1) * 130 + 50, starty, sys.ap.leftCenter)
			if(i == 1)then
				roundBtn.SetMyBtnSelect(true)
			end
			table.insert(m_RoundBtn, roundBtn)
		end

		m_setInfo.turn = 1

		--记忆选择
		if(setting ~= nil and setting.turn ~= nil)then
			for j = 1, #info.tlist do
				if(info.tlist[j].turn == setting.turn)then
					m_setInfo.turn = j
					m_RoundBtn[1].SetMyBtnSelect(false)
					m_RoundBtn[j].SetMyBtnSelect(true)
				end
			end
		end


		starty = starty - yDis
	end

	--房费设定
	if(CHECK_VER_FLAG_CONTROL == nil)then
		local fangfeiLabel = xui.addSprite(m_ShowNode, "xxqp_cjfj_ffsd", startx, starty, sys.ap.leftCenter)

		m_fangzhuBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_wxz",function()
				m_DeskType = 0
				m_costTypeFocus:setPosition(m_fangzhuBtn.btnLy:getPositionX() + m_fangzhuBtn.btnLy:getContentSize().width / 2, m_fangzhuBtn.btnLy:getPositionY() + m_fangzhuBtn.btnLy:getContentSize().height / 2)
			end)
		m_ShowNode:addChild(m_fangzhuBtn.btnLy)
		m_fangzhuBtn.btnLy:setPosition(startx + 250 + (2 - 1) * 100, starty - m_fangzhuBtn.btnLy:getContentSize().height / 2)
		local introLabel = xui.addLabel(m_ShowNode, "房主支付", cc.c3b(255, 255, 255), 24, startx + 250 + (2 - 1) * 100 + 50, starty, sys.ap.leftCenter)
		m_fangzhuBtn:SetMyBtnSelect(true)

		m_winnerBtn = MySelectBtnClass:new("xxqp_cjfj_wxz","xxqp_cjfj_wxz",function()
				m_DeskType = 1	
				m_costTypeFocus:setPosition(m_winnerBtn.btnLy:getPositionX() + m_winnerBtn.btnLy:getContentSize().width / 2, m_winnerBtn.btnLy:getPositionY() + m_winnerBtn.btnLy:getContentSize().height / 2)
			end)
		m_ShowNode:addChild(m_winnerBtn.btnLy)
		m_winnerBtn.btnLy:setPosition(startx + 180 + (1 - 1) * 100, starty - m_winnerBtn.btnLy:getContentSize().height / 2)
		local introLabel = xui.addLabel(m_ShowNode, "大赢家支付", cc.c3b(255, 255, 255), 24, startx + 180 + (1 - 1) * 100 + 50, starty, sys.ap.leftCenter)

		m_costTypeFocus = xui.addSprite(m_ShowNode, "xxqp_cjfj_xz", m_fangzhuBtn.btnLy:getPositionX() + m_fangzhuBtn.btnLy:getContentSize().width / 2, m_fangzhuBtn.btnLy:getPositionY() + m_fangzhuBtn.btnLy:getContentSize().height / 2)

		m_costTypeFocus:setPosition(m_winnerBtn.btnLy:getPositionX() + m_winnerBtn.btnLy:getContentSize().width / 2, m_winnerBtn.btnLy:getPositionY() + m_winnerBtn.btnLy:getContentSize().height / 2)
	end
	
	--默认大赢家支付
	m_DeskType = 1

	--记忆选择
	if(setting ~= nil and setting.deducttype ~= nil)then
		if(setting.deducttype ~= 1)then
			m_DeskType = 0
			m_costTypeFocus:setPosition(m_fangzhuBtn.btnLy:getPositionX() + m_fangzhuBtn.btnLy:getContentSize().width / 2, m_fangzhuBtn.btnLy:getPositionY() + m_fangzhuBtn.btnLy:getContentSize().height / 2)
		end
	end
	

	--游戏介绍
	local gameIntroLabel = xui.addLabel(m_ShowNode, Pt_M_getGameIntroduce(m_GameInfo[m_GameTypeIndex].gameid), cc.c3b(255, 255, 255), 24, 40, 519 + (sys.height - 750) / 2, sys.ap.leftTop)
	gameIntroLabel:setDimensions({width = 255, height = 0})
	starty = starty - gameIntroLabel:getContentSize().height - yDis

	--创建房间按钮
	local createBtn = MyBtnClass:new("xxqp_cjfj_an_zc", "xxqp_cjfj_an_dj", "xxqp_cjfj_an_dj", clickCreateRoom)
	m_ShowNode:addChild(createBtn.btnLy)
	createBtn.btnLy:setPosition(sys.center.x - createBtn.btnLy:getContentSize().width / 2 + 100, 58)
	xui.addSprite(createBtn.btnLy, "xxqp_cjfj_cjfj", createBtn.btnLy:getContentSize().width / 2, createBtn.btnLy:getContentSize().height / 2)


	--消耗房卡数量
	m_NeedCardLabel = xui.addLabel(m_ShowNode, "", cc.c3b(255, 255, 255), 24, sys.center.x + 250, 100, sys.ap.leftCenter)
	updateNeedCardLabel()
	if(CHECK_VER_FLAG_CONTROL ~= nil)then
		m_NeedCardLabel:setVisible(false)
	end


	--test code
	-- textureIdx = 1
	-- local Majo_M_TextureRes = { 
 --                        "Majo/Majo_Res/Majo_MobileRes/PicRes/PlMj_M_GameRes.png",
 --                        "Majo/Majo_Res/Majo_MobileRes/PicRes/PlMj_M_PokerRes.png",
	-- 					"Majo/Majo_Res/Majo_MobileRes/PicRes/XxMj_M_PlayAin_01.png",
 --                        "Majo/Majo_Res/Majo_MobileRes/PicRes/XxMj_M_PlayAin_02.png",
	-- 					"Majo/Majo_Res/Majo_MobileRes/PicRes/XxMj_M_PlayAin_03.png",
						
 --                     }
 --    local Majo_M_TextureResPlist = { 
 --                        "Majo/Majo_Res/Majo_MobileRes/PicRes/PlMj_M_GameRes.plist",
 --                        "Majo/Majo_Res/Majo_MobileRes/PicRes/PlMj_M_PokerRes.plist",
	-- 					"Majo/Majo_Res/Majo_MobileRes/PicRes/XxMj_M_PlayAin_01.plist",
 --                        "Majo/Majo_Res/Majo_MobileRes/PicRes/XxMj_M_PlayAin_02.plist",
	-- 					"Majo/Majo_Res/Majo_MobileRes/PicRes/XxMj_M_PlayAin_03.plist",
 --                     }
 --    local function addTextureAsyncCall(texture)
 --    	CCLOG("addTextureAsyncCall time = " .. os.time())
 --    	CCSpriteFrameCache:getInstance():addSpriteFramesWithFile(Majo_M_TextureResPlist[textureIdx], texture)
 --    	textureIdx = textureIdx + 1
 --    	CCLOG("addTextureAsyncCall ")
 --    end
 --    for i = 1, #Majo_M_TextureRes do
 --    	local cache = CCDirector:getInstance():getTextureCache()
 --    	if(cache ~= nil)then
 --    		cache:addImageAsync(Majo_M_TextureRes[i], addTextureAsyncCall)
 --    	end
 --    end
    --sys.ResLoader( Majo_M_TextureRes )
end

--获取游戏标题图片
function getGameRuleUITitleRes(gameType)
	local rtn = "xxqp_cjfj_cdmj"

	if(gameType == 1)then
		rtn = "xxqp_cjfj_cdmj"		--成都麻将
	elseif(gameType == 2)then
		rtn = "xxqp_ybmj"		--宜宾麻将
	elseif(gameType == 4)then 
		rtn = "xxqp_cjfj_njmj"	--内江麻将
	elseif(gameType == 6)then
		rtn = "xxqp_cjfj_lzmj"	--泸州麻将
	elseif(gameType == 7)then
		rtn = "xxqp_cjfj_zgmj"	--自贡麻将
	elseif(gameType == 8)then
		rtn = "xxqp_cjfj_ybcp"	--宜宾长牌
	elseif(gameType == 9)then
		rtn = "xxqp_cjfj_drhj"	--大二
	elseif(gameType == 3)then
		rtn = "xxqp_cjfj_ybddz"	--宜宾斗地主
	end

	return rtn
end

CreateUI = function (  )
	CCLOG("创建设置UI")
	xui.creMaskLy(m_BaseNode, nil,nil,nil,nil,false)
	m_SelectType = 1
	
	local bg = xui.addSprite(m_BaseNode, "xxqp_zj_bj", sys.center.x, sys.center.y)
	bg:setScaleY(sys.height / bg:getContentSize().height)
	
	--叉叉
	local closeBtn = MyBtnClass:new("xxqp_zj_fh_zc","xxqp_zj_fh_dj","xxqp_zj_fh_dj",
						function ( )
							Pt_M_UIAni_LeftMoveOut(m_BaseNode,function ( )
			 						 		Close()
			 						 	end)
						end)
	m_BaseNode:addChild(closeBtn.btnLy,4)
	closeBtn.btnLy:setPosition(23, sys.height - 114)
	
	m_GameInfo = Pt_GetGameServerInfoByType(m_GameType)
	if(#m_GameInfo < 1)then
		xui.addLabel(m_BaseNode, "没有游戏设置", cc.c3b(255, 255, 255), 24, sys.center.x, sys.center.y)
		return
	end

	--titleSp
	if(GUANGDIAN_SHENHE_FLAG ~= true)then
		local titleSp = xui.addSprite(m_BaseNode, getGameRuleUITitleRes(m_GameType), sys.center.x, sys.height - 55)
	end
	

	--leftbg
	local leftBg = xui.addSpr9( m_BaseNode, "xxqp_cjfj_dbj", 284, sys.height - 190, 166, 320 + (sys.height - 750) / 2, sys.ap.center)
	local leftFrame = xui.addSpr9(m_BaseNode, "xxqp_cjfj_hbj", leftBg:getContentSize().width - 4, 51, leftBg:getPositionX(), leftBg:getPositionY() + leftBg:getContentSize().height / 2 - 27)
	xui.addSprite(m_BaseNode, "xxqp_cjfj_yxsm", leftFrame:getPositionX(), leftFrame:getPositionY())

	--rightBg
	local _w = sys.width - 284 - 52
	local rightBg = xui.addSpr9(m_BaseNode, "xxqp_cjfj_dbj", _w, sys.height - 190, 316 + _w / 2, 320 + (sys.height - 750) / 2, sys.ap.center)
	local rightFrame = xui.addSpr9(m_BaseNode, "xxqp_cjfj_rbj", rightBg:getContentSize().width - 4, 51, rightBg:getPositionX(), rightBg:getPositionY() + rightBg:getContentSize().height / 2 - 27)

	--
	if(CHECK_VER_FLAG_CONTROL == nil)then
		xui.addLabel(m_BaseNode, "首局游戏未完成解散房间，不扣除房卡", cc.c3b(255, 255, 255), 24, sys.center.x, 22, sys.ap.center)
	end
	

	--show node
	m_ShowNode = xui.addNode(m_BaseNode, 0, 0)

	local m_UserDefault = cc.UserDefault:getInstance()
	local defaultSet = m_UserDefault:getStringForKey("ruleset_" .. m_GameType, "")
	if(defaultSet ~= nil and defaultSet ~= "")then
		local setting = json.decode(defaultSet)
		if(setting ~= nil)then
			CCLOG("createRuleUI with setting !!")
			if(m_GameInfo[setting.m_GameTypeIndex] ~= nil)then
				createRuleUI(setting.m_GameTypeIndex, setting)
			else
				createRuleUI(1)
			end
		else
			createRuleUI(1)
		end
	else
		createRuleUI(1)
	end

	
end

local function DelayTimeEnterGame()
	tips.creMaskTips()
	CCLOG("消息阻塞")
	sys.LockMsg()

	Pt_M_setIsGetZhanji(false)
	
	Pt_Public_EnterGame(m_GameInfo[m_GameTypeIndex].gameid)
end

RegisterEventListen = function ( )
	local function _CreatTableSuccess( key, s )
		local gameID = Pt_Get_SelectGameID()
		Pt_Save_SelectAutoTableID(s["index"])
		Pt_Save_SelecDifenID(s["configid"])
		CCLOG("进游戏time = " .. os.time())
		DelayTimeEnterGame()

		Pt_JMessageCreateGroup(s["deskcode"], "xxcnqproom" .. s["deskcode"], tostring(Pt_GetPlayerInfo()[Def_Player_PlayerId]))
	end
	Pt_EventMgr:register( Pt_Events.SC_MODE2_CREATE_ROOM, "GameRuleUI", _CreatTableSuccess )

	local function _EnterTableSuccess( key, s )
		local gameID = Pt_Get_SelectGameID()
		Pt_Save_SelectAutoTableID(s["index"])
		local tableInfo =Pt_Get_SelectGameMode2TableInfo(s["index"])
		if tableInfo ~= nil then
			Pt_Save_SelecDifenID(tableInfo["configid"])
		end 
		DelayTimeEnterGame()
	end
	Pt_EventMgr:register( Pt_Events.SC_MODE2_ENTER_ROOM, "GameRuleUI", _EnterTableSuccess )
end

unRegisterEventListen = function ( )
	Pt_EventMgr:unregisterByKey( "GameRuleUI" )
end