require "AudioEngine" 

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end

-- 业务逻辑处理相关
-- 初始化
function FuncellLuaInit()
	print("------FuncellLuaInit")
	doFuncellInit()
end

-- 登录
function FuncellLuaLogin()
	print("------FuncellLuaLogin")
	doFuncellLogin()
end

-- 帐号退出
function FuncellLuaLogout()
	print("------FuncellLuaLogout")
	doFuncellLogout()
end

-- 支付
function FuncellLuaPay(RoleName,RoleId,RoleLevel,ServerName,ServerId,VipLevel,GameGoldBalance,GameUnionName,ItemName,ItemId,ItemCount,ItemDescription,ItemAmount,ExtrasParams,ItemType,OrderId)
	print("------FuncellLuaPay")
	doFuncellPay(RoleName,RoleId,RoleLevel,ServerName,ServerId,VipLevel,GameGoldBalance,GameUnionName,ItemName,ItemId,ItemCount,ItemDescription,ItemAmount,ExtrasParams,ItemType,OrderId)
end

-- 游戏退出
function FuncellLuaExit()
	print("------FuncellLuaExit")
	doFuncellExit()
end

-- 获取是否有退出界面
function FuncellLuaGetExitUI()
	print("------FuncellLuaGetExitUI")
	doFuncellGetExitUI()
end

-- 统计相关
-- LoginEvent
function FuncellLuaLoginEvent(json)
	print("------FuncellLuaLoginEvent")
	--json数据字段为:usermark(平台登录成功后返回的用户ID)、serverno(服务器ID)
	setLoginEventCallback(json)
end

--角色升级事件
function FuncellLuaRoleLevelUpEvent(json)
	print("------FuncellLuaRoleLevelUpEvent")
	--json数据字段为:usermark(平台登录成功后返回的用户ID)、serverno(服务器ID)、level(角色等级)、role_id(角色ID)、role_name(角色名)、server_name(服务器名)
	setRoleLevelUpEventCallback(json)
end

--创建角色事件
function FuncellLuaCreatRoleEvent(json)
	print("------FuncellLuaCreatRoleEvent")
	--json数据字段为:usermark(平台登录成功后返回的用户ID)、serverno(服务器ID)、role_id(角色ID)、role_name(角色名)、server_name(服务器名)
	setCreatRoleEventCallback(json)
end

--服务器与角色事件
function FuncellLuaServer_RoleEvent(json)
	print("------FuncellLuaServer_RoleEvent")
	--json数据字段为:usermark(平台登录成功后返回的用户ID)、role_level(角色等级)、role_id(角色ID)、role_name(角色名)、server_name(服务器名)、role_vip_level(VIP等级)、role_gameunion_name(工会)
	setServer_RoleEventCallback(json)
end

-- 回调接口部分
-- 初始化相关
function onFuncellLuaInitSuccess()
	--Init Success Callback
	print("+++onLuaInitSuccess")
end

function onFuncellLuaInitFailed(msg)
	--Init Failed Callback
	print("+++onLuaInitFailed")
end

-- 登录相关
function onFuncellLuaLoginSuccess(userId,userName,token)
	--Login Success Callback
	print("+++onFuncellLuaLoginSuccess")
end

function onFuncellLuaLoginCancel()
	--Login Cancel Callback
	print("+++onFuncellLuaLoginCancel")
end

function onFuncellLuaLoginFailed(msg)
	--Login Failed Callback
	print("+++onFuncellLuaLoginFailed")
end

function onFuncellLuaLogout()
	--Logout Callback
	print("+++onFuncellLuaLogout")
end

-- 支付相关
function onFuncellLuaPaySuccess(cpOrder,sdkOrder,extrasParams)
	--Pay Success Callback
	print("+++onFuncellLuaPaySuccess")
end

function onFuncellLuaPayFail(msg)
	--Pay Fail Callback
	print("+++onFuncellLuaPayFail")
end

function onFuncellLuaPayCancel(msg)
	--Pay Cancel Callback
	print("+++onFuncellLuaPayCancel")
end

-- 游戏退出相关
function onFuncellLuaChannelExit()
	print("+++onFuncellLuaChannelExit")
end

function onFuncellLuaGameExit()
	print("+++onFuncellLuaGameExit")
end




local function main()
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)

    local cclog = function(...)
        print(string.format(...))
    end

    ---------------

    local visibleSize = CCDirector:sharedDirector():getVisibleSize()
    local origin = CCDirector:sharedDirector():getVisibleOrigin()

    -- create farm
    local function createLayerFarm()
        local layerFarm = CCLayer:create()

        -- add in farm background
        local bg = CCSprite:create("bg.png")
        bg:setPosition(origin.x + visibleSize.width / 2 , origin.y + visibleSize.height / 2)
        layerFarm:addChild(bg)
        
        return layerFarm
    end


    -- create menu
    local function createLayerMenu()
        local layerMenu = CCLayer:create()
		local menuBtn
		
		local function menuCallbackInit()
			FuncellLuaInit()
		end
		
		local function menuCallbackLogin()
			FuncellLuaLogin()
		end
		
		local function menuCallbackLogout()
			FuncellLuaLogout()
		end	
		
		local function menuCallbackPay()
			RoleName="test1"
			RoleId="123"
			RoleLevel="2"
			ServerName="test"
			ServerId="998"
			VipLevel="3"
			GameGoldBalance="888"
			GameUnionName="游戏工会"
			ItemName="10元宝"
			ItemId="100002"
			ItemCount=10
			ItemDescription="商品描述"
			ItemAmount=100
			ExtrasParams="test"
			ItemType="元宝"
			OrderId="游戏订单号"
			FuncellLuaPay(RoleName,RoleId,RoleLevel,ServerName,ServerId,VipLevel,GameGoldBalance,GameUnionName,ItemName,ItemId,ItemCount,ItemDescription,ItemAmount,ExtrasParams,ItemType,OrderId)
		end
		
		local function menuCallbackExit()
			FuncellLuaExit()
		end
		
		local menuItemInit = CCMenuItemImage:create("init.png", "init.png")
		menuItemInit:setPosition(0,0)
		menuItemInit:registerScriptTapHandler(menuCallbackInit)
		menuPopup = CCMenu:createWithItem(menuItemInit)
		menuPopup:setPosition(origin.x + visibleSize.width / 2-100, origin.y + visibleSize.height-30)
		menuPopup:setVisible(true)
		layerMenu:addChild(menuPopup)
		
		local menuItemLogin = CCMenuItemImage:create("funcell_demo_login_button_click.png", "funcell_demo_login_button_click.png")
		menuItemLogin:setPosition(0,0)
		menuItemLogin:registerScriptTapHandler(menuCallbackLogin)
		menuPopup = CCMenu:createWithItem(menuItemLogin)
		menuPopup:setPosition(origin.x + visibleSize.width / 2-100, origin.y + visibleSize.height - 80)
		menuPopup:setVisible(true)
		layerMenu:addChild(menuPopup)
		
		local menuItemLogout = CCMenuItemImage:create("funcell_demo_logout_button_click.png", "funcell_demo_logout_button_click.png")
		menuItemLogout:setPosition(0,0)
		menuItemLogout:registerScriptTapHandler(menuCallbackLogout)
		menuPopup = CCMenu:createWithItem(menuItemLogout)
		menuPopup:setPosition(origin.x + visibleSize.width / 2-100, origin.y + visibleSize.height-130)
		menuPopup:setVisible(true)
		layerMenu:addChild(menuPopup)
		
		local menuItemPay = CCMenuItemImage:create("funcell_demo_pay_button_click.png", "funcell_demo_pay_button_click.png")
		menuItemPay:setPosition(0,0)
		menuItemPay:registerScriptTapHandler(menuCallbackPay)
		menuPopup = CCMenu:createWithItem(menuItemPay)
		menuPopup:setPosition(origin.x + visibleSize.width / 2-100, origin.y + visibleSize.height-180)
		menuPopup:setVisible(true)
		layerMenu:addChild(menuPopup)
		
		local menuItemExit = CCMenuItemImage:create("funcell_demo_exit_button_click.png", "funcell_demo_exit_button_click.png")
		menuItemExit:setPosition(0,0)
		menuItemExit:registerScriptTapHandler(menuCallbackExit)
		menuPopup = CCMenu:createWithItem(menuItemExit)
		menuPopup:setPosition(origin.x + visibleSize.width / 2-100, origin.y + visibleSize.height-230)
		menuPopup:setVisible(true)
		layerMenu:addChild(menuPopup)
		
		--menuBtn = CCMenu:create(menuItemInit,menuItemLogin,menuItemLogout,menuItemPay,menuItemExit)
		--menuBtn:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)
		--menuBtn:setVisible(true)
		--layerMenu:addChild(menuBtn)
		
		
		
        return layerMenu
    end

    -- run
    local sceneGame = CCScene:create()
    sceneGame:addChild(createLayerFarm())
    sceneGame:addChild(createLayerMenu())
    CCDirector:sharedDirector():runWithScene(sceneGame)
end

xpcall(main, __G__TRACKBACK__)
