package {
import com.funcell.sdk.extension.FuncellGameSdkProxy;
import com.funcell.sdk.extension.data.FuncellDataTypes;
import com.funcell.sdk.extension.exit.FuncellExitEvent;
import com.funcell.sdk.extension.init.FuncellInitEvent;
import com.funcell.sdk.extension.pay.FuncellPayEvent;
import com.funcell.sdk.extension.pay.FuncellPayParams;
import com.funcell.sdk.extension.pay.FuncellRoleInfo;
import com.funcell.sdk.extension.session.FuncellSessionEvent;

import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;

public class Main extends Sprite {
    private var WIDTH:int;
    private var HEIGHT:int;
    private var text:TextField;

    public function Main() {

        stage.align = StageAlign.TOP_LEFT;
        stage.scaleMode = StageScaleMode.NO_SCALE;
        this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);

        FuncellGameSdkProxy.getInstance().Init();
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellInitEvent.onInitSuccess,onInitSuccess);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellInitEvent.onInitFailure,onInitFailure);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellSessionEvent.onLoginSuccess,onLoginSuccess);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellSessionEvent.onLoginCancel,onLoginCancel);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellSessionEvent.onLoginFailed,onLoginFailed);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellSessionEvent.onLogout,onLogout);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellPayEvent.onCancel,onCancel);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellPayEvent.onFail,onFail);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellPayEvent.onClosePayPage,onClosePayPage);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellPayEvent.onSuccess,onSuccess);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellExitEvent.onChannelExit,onChannelExit);
        FuncellGameSdkProxy.getInstance().addEventListener(FuncellExitEvent.onGameExit,onGameExit);
    }

    protected function onAddToStage(event:Event):void
    {
        WIDTH = stage.fullScreenWidth;
        HEIGHT = stage.fullScreenHeight;

        var loginBtn:FuncellButton = new FuncellButton("Login"); //登录
        loginBtn.x = 30;
        loginBtn.y = 30;
        this.addChild(loginBtn);

        var logoutBtn:FuncellButton = new FuncellButton("Logout");//登出
        logoutBtn.x = 30;
        logoutBtn.y = 110;
        this.addChild(logoutBtn);

        var payBtn:FuncellButton = new FuncellButton("Pay");//支付
        payBtn.x = 400;
        payBtn.y = 110;
        this.addChild(payBtn);

        var login_eventBtn:FuncellButton = new FuncellButton("登录事件");//
        login_eventBtn.x = 400;
        login_eventBtn.y = 190;
        this.addChild(login_eventBtn);

        var RoleLevelUpEvent:FuncellButton = new FuncellButton("升级事件");//
        RoleLevelUpEvent.x = 400;
        RoleLevelUpEvent.y = 270;
        this.addChild(RoleLevelUpEvent);

        var Server_RoleEvent:FuncellButton = new FuncellButton("服务器角色事件");//
        Server_RoleEvent.x = 400;
        Server_RoleEvent.y = 350;
        this.addChild(Server_RoleEvent);

        var CreatRoleEvent:FuncellButton = new FuncellButton("创建角色事件");//
        CreatRoleEvent.x = 400;
        CreatRoleEvent.y = 420;
        this.addChild(CreatRoleEvent);

        var callFunction:FuncellButton = new FuncellButton("调用方法");//
        callFunction.x = 400;
        callFunction.y = 500;
        this.addChild(callFunction);

        var exitBtn:FuncellButton = new FuncellButton("Exit");//退出
        exitBtn.x = 30;
        exitBtn.y = 190;
        this.addChild(exitBtn);

        loginBtn.addEventListener(MouseEvent.CLICK, onLoginClick);
        logoutBtn.addEventListener(MouseEvent.CLICK, onLogoutClick);
        payBtn.addEventListener(MouseEvent.CLICK, onPayClick);
        exitBtn.addEventListener(MouseEvent.CLICK, onExitClick);
        login_eventBtn.addEventListener(MouseEvent.CLICK,onLoginEvent);
        RoleLevelUpEvent.addEventListener(MouseEvent.CLICK,onRoleLevelUpEvent);
        Server_RoleEvent.addEventListener(MouseEvent.CLICK,onServer_RoleEvent);
        CreatRoleEvent.addEventListener(MouseEvent.CLICK,onCreatRoleEvent);
        callFunction.addEventListener(MouseEvent.CLICK,onCallFunction);
    }

    protected function onCallFunction(event:MouseEvent){
        var json:Object={"test":"123","test1":"456"};
        FuncellGameSdkProxy.getInstance().callFunction("functionTest",JSON.stringify(json));
    }

    protected function onLoginEvent(event:MouseEvent){
        var dataType:String = FuncellDataTypes.DataTypes.DATA_LOGIN;
        var info:Object={"usermark":"13212","serverno":"998"};//usermark为登录成功，返回的用户ID---UserID
        FuncellGameSdkProxy.getInstance().setDatas(dataType,JSON.stringify(info));
    }
    protected function onRoleLevelUpEvent(event:MouseEvent){
        var dataType:String = FuncellDataTypes.DataTypes.DATA_ROLE_LEVELUP;
        var info:Object={"usermark":"13212","serverno":"998","level":"999","role_id":"123","role_name":"test","server_name":"天下"};
        FuncellGameSdkProxy.getInstance().setDatas(dataType,JSON.stringify(info));
    }
    protected function onServer_RoleEvent(event:MouseEvent){
        var dataType:String = FuncellDataTypes.DataTypes.DATA_SERVER_ROLE_INFO;
        var info:Object={"role_id":"123","role_level":999,"role_name":"test","role_gameunion_name":"工会","role_vip_level":"VIP等级","serverno":"998","server_name":"天下"};
        FuncellGameSdkProxy.getInstance().setDatas(dataType,JSON.stringify(info));
    }
    protected function onCreatRoleEvent(event:MouseEvent){
        var dataType:String = FuncellDataTypes.DataTypes.DATA_CREATE_ROLE;
        var info:Object={"usermark":"13212","role_id":"998","serverno":"998","server_name":"天下","role_name":"test"};
        FuncellGameSdkProxy.getInstance().setDatas(dataType,JSON.stringify(info));
    }

    protected function onPayClick(event:MouseEvent):void
    {
        var Roleinfo:FuncellRoleInfo = new FuncellRoleInfo();
        Roleinfo.mRoleName = "test"; //角色名
        Roleinfo.mRoleId = "123"; //角色ID
        Roleinfo.mRoleLevel = "2";//角色等级
        Roleinfo.mServerName = "天下"; //服务器名
        Roleinfo.mServerId = "998";//服务器ID
        Roleinfo.mVipLevel = "3";//VIP等级
        Roleinfo.mGameGoldBalance = "888"; //角色剩余金币数
        Roleinfo.mGameUnionName = "游戏工会" //游戏工会

        var PayParams:FuncellPayParams = new FuncellPayParams();
        PayParams.mItemAmount = "100" //商品价格，单位：分
        PayParams.mItemCount = "10";//钻石数量 ,10个元宝
        PayParams.mItemDescription="Description"; //商品描述
        PayParams.mItemId="100002";//商品ID
        PayParams.mItemName = "10元宝";//商品名
        PayParams.mItemType = "元宝";//商品类型
        PayParams.mOrderId = "465456456" //游戏接入方订单号
        PayParams.mExtrasParams = "ExtrasParams"  //透传参数，支付成功后，返回给客户端的数据
        PayParams.mRoleInfo = Roleinfo;

        FuncellGameSdkProxy.getInstance().Pay(PayParams);
    }
    protected function onLoginClick(event:MouseEvent):void
    {
       FuncellGameSdkProxy.getInstance().Login();
    }
    protected function onLogoutClick(event:MouseEvent):void
    {
        FuncellGameSdkProxy.getInstance().Logout();
    }
    protected function onExitClick(event:MouseEvent):void
    {
        FuncellGameSdkProxy.getInstance().Exit();
    }

    //------------------------------回调接口------------------------
    protected  function onInitSuccess(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as onInitSuccess");
        trace("Main.as----onInitSuccess......");
    }
    protected  function onInitFailure(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as onInitFailure");
        trace("Main.as----onInitFailure......");
    }
    protected function onLoginSuccess(event:FuncellSessionEvent):void{
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as onLoginSuccess");
        var token:String = event.result.mToken;
        var uid:String = event.result.mUserID;
        var userName:String = event.result.mUsername;
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as token:"+token);
    }
    protected function onLoginCancel(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as onLoginCancel");
    }
    protected function onLoginFailed(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as onLoginFailed");
    }
    protected function onLogout(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as onLogout");
    }
    protected function onCancel(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as pay onCancel");
    }
    protected function onFail(event:Event):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as pay onFail");
    }
    protected function onClosePayPage(event:FuncellPayEvent):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as pay onClosePayPage");
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as pay sdkOrder:"+event.result.mSdkOrder);
        var cpOrder:String = event.result.mCpOrder;//游戏订单号
        var sdkOrder:String = event.result.mSdkOrder;//平台订单号
        var ext:String = event.result.mExtrasParams;//透传参数
    }
    protected function onSuccess(event:FuncellPayEvent):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as pay onClosePayPage");
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as pay sdkOrder:"+event.result.mSdkOrder);
        var cpOrder:String = event.result.mCpOrder;//游戏订单号
        var sdkOrder:String = event.result.mSdkOrder;//平台订单号
        var ext:String = event.result.mExtrasParams;//透传参数
    }
    protected function onChannelExit(event:FuncellExitEvent):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as exit onChannelExit");
    }
    protected function onGameExit(event:FuncellExitEvent):void
    {
        FuncellGameSdkProxy.getInstance().FuncellLog("-------Main.as exit onGameExit");
    }
}
}
