package com.fq.channel.sdk;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.fq.channel.sdk.base.interfaces.FQPluginApi;
import com.fq.channel.sdk.base.interfaces.PluginResult;
import com.fq.channel.sdk.base.interfaces.PluginResultHandler;

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

import java.util.Map;

public class SdkPluginDemo extends FQPluginApi {

    private static final String TAG = "FQSDK_LOGSdkPluginDemo";

    /**
     * 数据统计
     */
    private int dataType;
    private String roleId, roleName, roleLevel, zoneId, zoneName, serverId, serverName, balance, vipLevel, partyName;

    /**
     * 初始化
     */
    @Override
    public void init(Context activity,final PluginResultHandler handler) {
        Log.i(TAG, "init");
        handler.onHandlePluginResult(new PluginResult(
                PluginResult.Status.OK));
    }

    /**
     * 登录
     */
    @Override
    public void login(final Context activity, final PluginResultHandler handler) {
        Log.i(TAG, "login");

        Builder builder = new Builder(activity);
        builder.setTitle("登录");
        builder.setMessage("请选择登录操作");
        builder.setCancelable(false);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                JSONObject jo = new JSONObject();
                try {
                    jo.put("uid", "uid");
                    jo.put("token", "token");
                    handler.onHandlePluginResult(new PluginResult(
                            PluginResult.Status.OK, jo));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                dialog.dismiss();
            }
        });
        builder.setNeutralButton("失败", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                handler.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.ERROR, "登录失败"));
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                handler.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.CANCEL));
                dialog.dismiss();
            }
        });
        builder.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    handler.onHandlePluginResult(new PluginResult(
                            PluginResult.Status.CANCEL));
                    dialog.dismiss();
                    return true;
                }
                return false;
            }
        });
        builder.create().show();
    }

    @Override
    public void pay(final Context activity, final Map<String, Object> map,
                    final PluginResultHandler handler) {
        Log.i(TAG, "pay map:" + (map != null ? map.toString() : ""));

        Builder builder = new Builder(activity);
        builder.setTitle("支付");
        builder.setMessage("请选择支付操作");
        builder.setCancelable(false);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                handler.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.OK));
                dialog.dismiss();
            }
        });
        builder.setNeutralButton("失败", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                handler.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.ERROR));
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                handler.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.CANCEL));
                dialog.dismiss();
            }
        });
        builder.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    handler.onHandlePluginResult(new PluginResult(
                            PluginResult.Status.CANCEL));
                    dialog.dismiss();
                    return true;
                }
                return false;
            }
        });
        builder.create().show();
    }

    /**
     * 退出
     *
     * @param context
     * @param prh
     */
    public void exit(Context context, PluginResultHandler prh) {
        Log.d(TAG, "exit");

        prh.onHandlePluginResult(new PluginResult(
                PluginResult.Status.CANCEL));
    }

    /**
     * 注销
     */
    public void logout(final Context context, final PluginResultHandler cb) {
        Log.d(TAG, "logout");

        Builder builder = new Builder(context);
        builder.setTitle("注销");
        builder.setMessage("是否注销？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "注销成功");
                cb.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.OK));
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(TAG, "注销失败");
                cb.onHandlePluginResult(new PluginResult(
                        PluginResult.Status.ERROR));
            }
        });
        builder.show();
    }

    @Override
    public void changeAccount(Activity activity, final PluginResultHandler prh) {
        Log.d(TAG, " changeAccount ");

        Builder builder = new Builder(activity);
        builder.setTitle("切换账号");
        builder.setMessage("请选择切换账号操作");
        builder.setCancelable(false);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });
        builder.setNeutralButton("失败", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                    return true;
                }
                return false;
            }
        });


        builder.create().show();
    }


    private void showTips(final String message) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {

            @Override
            public void run() {
//				Activity activity = IdsLingdoCache.get().getCurrentActivity();
//				if (null != activity){
//					Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
//				}
            }
        });
    }

    /**
     * 游戏角色信息
     *
     * @param data
     */
    public void setRoleInfo(Bundle data) {
        if (null == data) {
            data = new Bundle();
        }
        Log.i(TAG, "setExtraBundle data:" + data.toString());

        roleName = data.getString("roleName");// 角色名称
        roleLevel = data.getString("roleLevel");//当前登录的玩家角色等级
        roleId = data.getString("roleId");// 角色名称
        serverId = data.getString("serverId"); //当前登录的游戏区服ID
        serverName = data.getString("serverName");//当前登录的游戏区服名称
        balance = data.getString("balance");//当前用户游戏币余额
        vipLevel = data.getString("vipLevel");//当前用户VIP等级
        partyName = data.getString("partyName");//当前用户所属帮派
        zoneId = data.getString("zoneId");//角色所在区域唯一标示
        zoneName = data.getString("zoneName");//角色所在区域名称
        dataType = data.getInt("dataType");//统计判断类型

        StringBuffer reportData = new StringBuffer();
        reportData.append("[");
        reportData.append(" roleName:" + roleName);
        reportData.append(" roleLevel:" + roleLevel);
        reportData.append(" roleId:" + roleId);
        reportData.append(" serverId:" + serverId);
        reportData.append(" serverName:" + serverName);
        reportData.append(" balance:" + balance);
        reportData.append(" vipLevel:" + vipLevel);
        reportData.append(" partyName:" + zoneId);
        reportData.append(" zoneName:" + zoneName);
        reportData.append(" dataType:" + dataType);
        reportData.append("]");

        switch (dataType) {
            case 0://创建角色
                showTips("数据上报：创建角色 ");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 1://角色升级
                showTips("数据上报：角色升级");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 2:// 上传区服信息
                showTips("数据上报：上传区服信息");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 3://登录
                showTips("数据上报：登录");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 4://支付
                showTips("数据上报：进入游戏");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 5:// 选择服务器
                showTips("数据上报：支付");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 6://退出游戏
                showTips("数据上报：退出游戏");
                Log.i(TAG, "reportData" + reportData);
                break;

            case 7:// 备注（预留参数）
                showTips("数据上报：退出游戏");
                Log.i(TAG, "reportData" + reportData);
                break;

            default:
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    }

    /**
     * 复写生命周期方法,里面调用
     */
    public void onStart(Context context) {
        Log.i(TAG, "onStart");
    }

    public void onCreate(Context context, Bundle handler) {
        Log.i(TAG, "onCreate");
    }

    public void onRestart(Context context) {
        Log.i(TAG, "onRestart");
    }

    public void onResume(Context context) {
        Log.i(TAG, "onResume");
    }

    public void onPause(Context context) {
        Log.i(TAG, "onPause");
    }

    public void onStop(Context context) {
        Log.i(TAG, "onStop");
    }

    public void onDestroy(Context context) {
        Log.i(TAG, "onDestroy");
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG, "onActivityResult requestCode:" + requestCode + " resultCode:" + resultCode + " data:" + data);
    }

    public void onNewIntent(Intent intent) {
        Log.i(TAG, "onNewIntent");
    }

    @Override
    public void attachBaseContext(Context context) {

    }

    @Override
    public void onApplicationCreate(Context context) {

    }

    public void onSaveInstanceState(Context context, Bundle outState) {
        Log.i(TAG, "onSaveInstanceState");
    }
}
