package com.longtu.sample;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bh.sdk.Interface.LTBaseSDK;
import com.bh.sdk.LTGameUserData;
import com.bh.sdk.LTProduct;
import com.longtu.sdk.base.LTBaseConstant;
import com.longtu.sdk.base.Listener.LTBaseCallbackCode;
import com.longtu.sdk.base.Listener.LTBaseSDKListener;
import com.longtu.sdk.base.bean.LTRoleInfo;
import com.longtu.sdk.utils.Log.Logs;
import com.longtugame.globalsample.R;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

public class MainActivity extends Activity implements OnClickListener {

    private Button mButton_login;
    private Button mButton_pay;
    private Button mButton_exit;
    private Button mButton_switchaccount;
    private Button mButton_logout;
    private Button mButton_ucenter;
    private boolean canswitchaccount;
    private boolean canlogout;
    private boolean cangocenter;
    private final static String gameResVer = "0001";
    private final static String RoleId = "1000";
    private final static String RoleName = "测试角色";
    private final static String RoleLv = "10";
    private final static String RoleVipLv = "3";
    private final static String ServerName = "游戏1服";
    private final static String ServerId = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置无标题
        // this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 设置全屏幕
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // 强制使屏幕一直处于活动状态，不关闭背景光
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        // 设置打开日志输出，出正式包时务必注释掉，或者传false
        LTBaseSDK.getInstance(this).setLogs(true);
        setContentView(R.layout.activity_main_new);

        // 初始化SDK
        LTBaseSDK.getInstance(this).LTBaseSDKInit(gameResVer, new LTBaseSDKListener() {

            @Override
            public void LTBaseSDKExitgameCallBack(int arg0, String arg1) {
                // TODO Auto-generated method stub
                MainActivity.this.finish();
            }

            @Override
            public void LTBaseSDKInitCallBack(int code, String arg1) {
                // TODO Auto-generated method stub
                if (LTBaseCallbackCode.CODE_INIT_SUCCESS == code) {
                    // 游戏实现
                    Toast.makeText(MainActivity.this, "初始化成功 , code ==" + code, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "初始化失败, code == " + code, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void LTBaseSDKLoginCallBack(int code, String msg, LTGameUserData ltGameUserData) {
                // TODO Auto-generated method stub
                if (code == LTBaseCallbackCode.CODE_LOGIN_SUCCESS) {
                    Logs.i(LTBaseConstant.LTLogTag, "MainActivity, tokenId == " + msg + ", user data == " + ltGameUserData.toString());

                    LTRoleInfo roleInfo = new LTRoleInfo();
                    roleInfo.setSendtype(LTRoleInfo.LTROLE_SENDTYPE_REGISTER);
                    roleInfo.setRoleName(RoleName);
                    roleInfo.setPlayerid(RoleId);
                    roleInfo.setServerid(ServerId);
                    roleInfo.setServername(ServerName);
                    roleInfo.setRolelevel(RoleLv);
                    roleInfo.setViplevel(RoleVipLv);

                    // 角色注册成功需调用此接口设置角色信息，接口第一个参数为“1”表示注册，正常情况下，注册只在新角色注册成功后调用一次
                    LTBaseSDK.getInstance(MainActivity.this).LTBaseSDKRoleInfo(roleInfo);
                    // 角色登陆成功并成功进入游戏后需调用此接口设置角色信息，接口第一个参数为“2”表示登陆，在每次角色登陆成功后都要调用
                    roleInfo.setSendtype(LTRoleInfo.LTROLE_SENDTYPE_LOGIN);
                    LTBaseSDK.getInstance(MainActivity.this).LTBaseSDKRoleInfo(roleInfo);
                    Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();

                } else {
                    Toast.makeText(MainActivity.this, "登陆失败了, code == " + code, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void LTBaseSDKSwitchLoginCallBack(int code, String msg, LTGameUserData ltGameUserData) {
                // TODO Auto-generated method stub
                if (code == LTBaseCallbackCode.CODE_LOGIN_SUCCESS) {
                    Logs.i(LTBaseConstant.LTLogTag, "MainActivity, tokenId == " + msg + ", user data == " + ltGameUserData.toString());

                    LTRoleInfo roleInfo = new LTRoleInfo();
                    roleInfo.setSendtype(LTRoleInfo.LTROLE_SENDTYPE_REGISTER);
                    roleInfo.setRoleName(RoleName);
                    roleInfo.setPlayerid(RoleId);
                    roleInfo.setServerid(ServerId);
                    roleInfo.setServername(ServerName);
                    roleInfo.setRolelevel(RoleLv);
                    roleInfo.setViplevel(RoleVipLv);

                    // 角色注册成功需调用此接口设置角色信息，接口第一个参数为“1”表示注册，正常情况下，注册只在新角色注册成功后调用一次
                    LTBaseSDK.getInstance(MainActivity.this).LTBaseSDKRoleInfo(roleInfo);
                    // 角色登陆成功并成功进入游戏后需调用此接口设置角色信息，接口第一个参数为“2”表示登陆，在每次角色登陆成功后都要调用
                    roleInfo.setSendtype(LTRoleInfo.LTROLE_SENDTYPE_LOGIN);
                    LTBaseSDK.getInstance(MainActivity.this).LTBaseSDKRoleInfo(roleInfo);
                    Toast.makeText(MainActivity.this, "切换账号成功了", Toast.LENGTH_LONG).show();

                } else {
                    Toast.makeText(MainActivity.this, "切换账号失败了, code == " + code, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void LTBaseSDKLogoutCallBack(int arg0, String arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "注销成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void LTBaseSDKPayCallBack(int code, String pbid, String orderID) {
                // TODO Auto-generated method stub
                if (code == LTBaseCallbackCode.CODE_PAY_SUCCESS) {
                    Logs.i(LTBaseConstant.LTLogTag, "CODE_PAY_SUCCESS code == " + code + " , pbid = " + pbid + " orderID = " + orderID);
                    Toast.makeText(MainActivity.this, "花钱成功了。。。*_*", Toast.LENGTH_SHORT).show();
                } else if (code == LTBaseCallbackCode.CODE_PAY_FAIL) {
                    Logs.i(LTBaseConstant.LTLogTag, "CODE_PAY_FAIL code == " + code + " , pbid = " + pbid + " orderID = " + orderID);
                    Toast.makeText(MainActivity.this, "支付失败了", Toast.LENGTH_SHORT).show();
                } else if (code == LTBaseCallbackCode.CODE_PAY_CANCEL) {
                    Logs.i(LTBaseConstant.LTLogTag, "CODE_PAY_CANCEL code == " + code + " , pbid = " + pbid + " orderID = " + orderID);
                    Toast.makeText(MainActivity.this, "支付取消了", Toast.LENGTH_SHORT).show();
                }
            }
        });
        initGame();

    }

    /**
     * 初始化游戏
     */
    private void initGame() {
        // 获取当前渠道SDK可选接口中哪些接口需要调用,必须放到SDK初始化后调用

        LinearLayout layout = (LinearLayout) this.findViewById(R.id.linearlayout_demo);

        int index = new Random().nextInt() % 2;
        if (index == 0) {
            layout.setBackgroundResource(R.drawable.longtu_sample_background1);
        } else if (index == 1) {
            layout.setBackgroundResource(R.drawable.longtu_sample_background2);
        }

        this.mButton_login = (Button) this.findViewById(R.id.button_login);
        this.mButton_login.setOnClickListener(this);
        this.mButton_pay = (Button) this.findViewById(R.id.button_pay);
        this.mButton_pay.setOnClickListener(this);
        this.mButton_exit = (Button) this.findViewById(R.id.button_exit);
        this.mButton_exit.setOnClickListener(this);
        this.mButton_switchaccount = (Button) this.findViewById(R.id.button_switch);
        this.mButton_switchaccount.setOnClickListener(this);
        this.mButton_logout = (Button) this.findViewById(R.id.button_logout);
        this.mButton_logout.setOnClickListener(this);
        this.mButton_ucenter = (Button) this.findViewById(R.id.button_center);
        this.mButton_ucenter.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LTBaseSDK.getInstance(this).LTBaseSDKExitGame();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        if (view == this.mButton_login) {
            LTBaseSDK.getInstance(MainActivity.this).LTBaseSDKShowLoginView();
//            testJson();
        } else if (view == this.mButton_pay) {

            LTProduct product = new LTProduct();
            product.setBuyNum("100");
            product.setCurrency("钻石");
            product.setCurrencyType("1");
            product.setExtension("我是扩展字段");
            product.setGamedDeliverUrl("http://www.longtugame.com");

            product.setPrice("1");
            product.setProductDesc("测试商品");
            product.setProductId("1000");
            product.setProductName("100个测试商品");
            LTBaseSDK.getInstance(this).LTBaseSDKPay(product);

        } else if (view == this.mButton_exit) {
            LTBaseSDK.getInstance(this).LTBaseSDKExitGame();
        } else if (view == this.mButton_switchaccount) {
            testPay();
            if (this.canswitchaccount) {
                LTBaseSDK.getInstance(this).LTBaseSDKSwitchAccount();
            } else {
                Toast.makeText(MainActivity.this, "当前渠道SDK不需要切换账号功能", Toast.LENGTH_SHORT).show();
            }
        } else if (view == this.mButton_logout) {
            if (this.canlogout) {
                LTBaseSDK.getInstance(this).LTBaseSDKLogout();
            } else {
                Toast.makeText(MainActivity.this, "当前渠道SDK不需要账号注销功能", Toast.LENGTH_SHORT).show();
            }
        } else if (view == this.mButton_ucenter) {
            if (this.cangocenter) {
                LTBaseSDK.getInstance(this).LTBaseSDKShowUserCenter();
            } else {
                Toast.makeText(MainActivity.this, "当前渠道SDK不需要用户中心或者论坛功能", Toast.LENGTH_SHORT).show();
            }
        }

    }

    private void testJson() {
        JSONObject jsonTest = new JSONObject();
        try {
            jsonTest.put("user_id", "1111111");
            jsonTest.put("user_name", "22222");
            jsonTest.put("user_token", "33333");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String jsonStr = jsonTest.toString() + "11111111111111";
        Logs.fi(LTBaseConstant.LTLogTag, " jsonStr = " + jsonStr);
        try {
            JSONObject jsonEX = new JSONObject(jsonStr);
            String uid = jsonEX.getString("user_id");
            String uname = jsonEX.getString("user_name");
            String utoken = jsonEX.getString("user_token");
            Logs.fi(LTBaseConstant.LTLogTag, " uid = " + uid + " uname = " + uname + " utoken = " + utoken);
        } catch (JSONException e) {
            e.printStackTrace();
            Logs.fi(LTBaseConstant.LTLogTag, " e = " + e.getMessage());
        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Logs.i(LTBaseConstant.LTLogTag, "onPause");
        // 必须调用
        LTBaseSDK.getInstance(this).onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Logs.i(LTBaseConstant.LTLogTag, "onRestart");
        // 必须调用
        LTBaseSDK.getInstance(this).onRestart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Logs.i(LTBaseConstant.LTLogTag, "onResume");
        // 必须调用
        LTBaseSDK.getInstance(this).onResume();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Logs.i(LTBaseConstant.LTLogTag, "onStart");
        // 必须调用
        LTBaseSDK.getInstance(this).onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Logs.i(LTBaseConstant.LTLogTag, "onStop");
        // 必须调用
        LTBaseSDK.getInstance(this).onStop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Logs.i(LTBaseConstant.LTLogTag, "onDestroy");
        // 必须调用
        LTBaseSDK.getInstance(this).onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Logs.i(LTBaseConstant.LTLogTag, "onActivityResult");
        // 必须调用
        LTBaseSDK.getInstance(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        Logs.i(LTBaseConstant.LTLogTag, "onConfigurationChanged");
        // 必须调用
        LTBaseSDK.getInstance(this).onConfigurationChanged(newConfig);
    }


    /**
     * 模拟测试信息。
     */
    public void testPay() {


    }
}
