package com.longtu.sample;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.acegameproject.burstshooter.R;
import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.bean.LTRoleInfo;

import java.io.UnsupportedEncodingException;


/**
 * 模拟游戏进行创建角色业务。并调用角色绑定接口
 */
public class LTBase_Demo_CreatUserView extends LinearLayout implements OnClickListener {

    private Context mContext;
    private Button mButton_createrole;
    private Button mButton_entergame;

    private CheckBox mCheckBox_role1;
    private CheckBox mCheckBox_role2;
    private CheckBox mCheckBox_role3;
    private int roleCount = 0;

    private InGameCallback mInGameCallback;

    public LTBase_Demo_CreatUserView(Context context, InGameCallback inGameCallback) {
        super(context);
        // TODO Auto-generated constructor stub
        mInGameCallback = inGameCallback;
        mContext = context;
        roleCount = 0;
        init();
    }

    public static interface InGameCallback {

        public void InGame();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.longtu_demo_chooserole, this);
        this.mCheckBox_role1 = (CheckBox) this.findViewById(R.id.longtu_demo_role_1);
        this.mCheckBox_role1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    mCheckBox_role2.setChecked(false);
                    mCheckBox_role3.setChecked(false);
                }
            }
        });
        this.mCheckBox_role2 = (CheckBox) this.findViewById(R.id.longtu_demo_role_2);
        this.mCheckBox_role2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    mCheckBox_role1.setChecked(false);
                    mCheckBox_role3.setChecked(false);
                }
            }
        });
        this.mCheckBox_role3 = (CheckBox) this.findViewById(R.id.longtu_demo_role_3);
        this.mCheckBox_role3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    mCheckBox_role1.setChecked(false);
                    mCheckBox_role2.setChecked(false);
                }
            }
        });

        this.mButton_createrole = (Button) this.findViewById(R.id.longtu_demo_button_createrole);
        this.mButton_createrole.setOnClickListener(this);
        this.mButton_entergame = (Button) this.findViewById(R.id.longtu_demo_button_entergame);
        this.mButton_entergame.setOnClickListener(this);

        // 读取角色信息
        byte[] b_roleInfo = LTBase_Demo_MainActivity.GetDataFromPhone(LTBase_Demo_MainActivity.mMainActivity,
                "longtu_demo_role_info_" + LTBase_Demo_MainActivity.LONGTU_UserId);
        if (b_roleInfo != null) {
            try {
                String s_roleInfo = new String(b_roleInfo, "UTF-8");
                String[] ss_roleInfo = s_roleInfo.split("\\|");
                roleCount = ss_roleInfo.length;
                switch (roleCount) {
                    case 1:
                        this.mCheckBox_role1.setText(ss_roleInfo[0]);
                        break;
                    case 2:
                        this.mCheckBox_role1.setText(ss_roleInfo[0]);
                        this.mCheckBox_role2.setText(ss_roleInfo[1]);
                        break;
                    case 3:
                        this.mCheckBox_role1.setText(ss_roleInfo[0]);
                        this.mCheckBox_role2.setText(ss_roleInfo[1]);
                        this.mCheckBox_role3.setText(ss_roleInfo[2]);
                        break;
                }
                this.mCheckBox_role1.setChecked(true);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

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

        if (view == this.mButton_createrole) {
            // 创建角色，创建成功后----->>发送角色注册日志需要游戏注意
            if (this.roleCount >= 2) {
                Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "已拥有2个角色不能继续创建", Toast.LENGTH_LONG).show();
                return;
            }
            LayoutInflater inflater = (LayoutInflater) LTBase_Demo_MainActivity.mMainActivity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View view_libao = inflater.inflate(R.layout.longtu_demo_createrole_dialog, null);

            final EditText edit = (EditText) view_libao.findViewById(R.id.editText_libaoma);
            final EditText edit_rolelv = (EditText) view_libao.findViewById(R.id.editText_rolelv);
            final EditText edit_roleID = (EditText) view_libao.findViewById(R.id.editText_roleid);
            final EditText edit_roleserver = (EditText) view_libao.findViewById(R.id.editText_roleserver);

            Builder builder = new Builder(LTBase_Demo_MainActivity.mMainActivity);
            builder.setTitle("请输入角色名称");
            builder.setView(view_libao);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    try {
                        // DEMO逻辑判断，游戏无需关注
                        if (edit.getText().toString() == null || "".equals(edit.getText().toString())) {
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "请输入角色名称", Toast.LENGTH_LONG)
                                    .show();
                            return;
                        }
                        // DEMO注册角色，游戏无需关注
                        String rolename = edit.getText().toString();
                        LTBase_Demo_MainActivity.RoleLv = edit_rolelv.getText().toString();
                        LTBase_Demo_MainActivity.LONGTU_RoleId = edit_roleID.getText().toString();
                        LTBase_Demo_MainActivity.ServerId = edit_roleserver.getText().toString();
                        String ss = rolename + "|";
                        boolean isok = LTBase_Demo_MainActivity.SaveDataToPhone(
                                LTBase_Demo_MainActivity.mMainActivity, ss.getBytes("UTF-8"),
                                "longtu_demo_role_info_" + LTBase_Demo_MainActivity.LONGTU_UserId);
                        if (isok) {
                            switch (roleCount) {
                                case 0:
                                    mCheckBox_role1.setText(rolename);
                                    mCheckBox_role1.setChecked(true);
                                    break;
                                case 1:
                                    mCheckBox_role2.setText(rolename);
                                    mCheckBox_role2.setChecked(true);
                                    break;
                                case 2:
                                    mCheckBox_role3.setText(rolename);
                                    mCheckBox_role3.setChecked(true);
                                    break;
                            }
                            roleCount++;
                            String roleid = LTBase_Demo_MainActivity.LONGTU_RoleId;

//							// 如果角色创建成功，发送角色注册日志，游戏必须实现(SDK接口)
                            LTRoleInfo roleInfo = new LTRoleInfo();
                            roleInfo.setSendtype(LTRoleInfo.LTROLE_SENDTYPE_REGISTER);
                            roleInfo.setRoleName(rolename);
                            roleInfo.setPlayerid(roleid);
                            roleInfo.setServerid(LTBase_Demo_MainActivity.ServerId);
                            roleInfo.setServername(LTBase_Demo_MainActivity.ServerName);
                            roleInfo.setRolelevel("88");
                            roleInfo.setViplevel("9");
                            // 角色注册成功需调用此接口设置角色信息，接口第一个参数为“1”表示注册，正常情况下，注册只在新角色注册成功后调用一次
                            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKRoleInfo(roleInfo);

                        } else {
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "创建角色失败", Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "创建角色失败", Toast.LENGTH_LONG).show();
                    }
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });
            builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_BACK:
                            return true;
                        case KeyEvent.KEYCODE_MENU:
                            return true;
                        case KeyEvent.KEYCODE_SEARCH:
                            return true;
                    }
                    return false;
                }
            });
            builder.setCancelable(false);
            builder.show();
        } else if (view == this.mButton_entergame) {
            // 选择角色后，进入游戏----->>发送角色登陆日志需要游戏注意
            String rolename = "";
            String roleid = "";
            if (this.mCheckBox_role1.isChecked()) {
                if (this.mCheckBox_role1.getText() != null && !"空".equals(this.mCheckBox_role1.getText().toString())) {
                    rolename = this.mCheckBox_role1.getText().toString();
                    roleid = "1";
                } else {
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "请选择角色进入游戏", Toast.LENGTH_LONG).show();
                    return;
                }
            } else if (this.mCheckBox_role2.isChecked()) {
                if (this.mCheckBox_role2.getText() != null && !"空".equals(this.mCheckBox_role2.getText().toString())) {
                    rolename = this.mCheckBox_role2.getText().toString();
                    roleid = "2";
                } else {
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "请选择角色进入游戏", Toast.LENGTH_LONG).show();
                    return;
                }
            } else if (this.mCheckBox_role3.isChecked()) {
                if (this.mCheckBox_role3.getText() != null && !"空".equals(this.mCheckBox_role3.getText().toString())) {
                    rolename = this.mCheckBox_role3.getText().toString();
                    roleid = "3";
                } else {
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "请选择角色进入游戏", Toast.LENGTH_LONG).show();
                    return;
                }
            } else {
                Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "请选择角色进入游戏", Toast.LENGTH_LONG).show();
                return;
            }

            // 发送角色登陆日志，需要游戏必须调用(SDK接口)
            LTRoleInfo roleInfo = new LTRoleInfo();
            roleInfo.setSendtype(LTRoleInfo.LTROLE_SENDTYPE_LOGIN);
            roleInfo.setRoleName(rolename);
            roleInfo.setPlayerid(LTBase_Demo_MainActivity.LONGTU_RoleId);
            roleInfo.setServerid(LTBase_Demo_MainActivity.ServerId);
            roleInfo.setServername(LTBase_Demo_MainActivity.ServerName);
            roleInfo.setRolelevel(LTBase_Demo_MainActivity.RoleLv);
            roleInfo.setViplevel(LTBase_Demo_MainActivity.RoleVipLv);
            // 角色登陆成功并成功进入游戏后需调用此接口设置角色信息，接口第一个参数为“2”表示登陆，在每次角色登陆成功后都要调用
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKRoleInfo(roleInfo);

            // 初始化游戏场景，游戏不需要关注
            mInGameCallback.InGame();

        }
    }

}
