package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameAccountEntity;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import org.greenrobot.eventbus.EventBus;

import rx.functions.Action1;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_GAME_ACCOUNT_LOOK_MODIFY;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_GAME_ACCOUNT_LOOK_MODIFY_COMMIT;


/**
 * 修改游戏账号密码
 * Created by ljy on 2018/7/11.
 */

public class DialogModifyAccount {
    private Activity activity;
    private EditText game_new_pwd_et;
    private View modify_pwd_tv_line;
    private TextView modify_pwd_tv;
    private TextView sureTv;
    private int id;
    private GameAccountEntity gameAccountEntity;
    private AlertDialog dialog;

    private TextView game_title_et;
    private TextView game_account_et;
    private TextView game_old_pwd_et;

    public DialogModifyAccount(Activity activity) {
        this.activity = activity;
    }

    public DialogModifyAccount(Activity activity, int id) {
        this.activity = activity;
        this.id = id;
    }
    public DialogModifyAccount(Activity activity, GameAccountEntity item) {
        this.activity = activity;
        this.gameAccountEntity = item;
        this.id = item.getId();
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                switch (msg.what) {
                    case 0:
                        if (game_new_pwd_et == null)
                            return;

                        game_new_pwd_et.setVisibility((Integer) msg.obj);
                        sureTv.setText("立即修改");
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    public void showAddAccount() {
        if (activity == null)
            return;

        View dialog_parent = View.inflate(activity, R.layout.dialog_parent, null);
        dialog = new AlertDialog.Builder(activity, R.style.MyDialogActivityTheme)
                .setView(dialog_parent)
                .create();
        TextView dialog_title = dialog_parent.findViewById(R.id.dialog_title);
        final View dialog_close = dialog_parent.findViewById(R.id.dialog_close);
        LinearLayout dialog_center_ll = dialog_parent.findViewById(R.id.dialog_center_ll);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_modify_account, dialog_center_ll, true);
        dialog_title.setText("修改密码");

        sureTv = view.findViewById(R.id.sure_tv);
        modify_pwd_tv_line = view.findViewById(R.id.modify_pwd_tv_line);
        modify_pwd_tv = view.findViewById(R.id.modify_pwd_tv);
        game_title_et = view.findViewById(R.id.game_title_et);
        game_account_et = view.findViewById(R.id.game_account_et);
        game_old_pwd_et = view.findViewById(R.id.game_old_pwd_et);
        game_new_pwd_et = view.findViewById(R.id.game_new_pwd_et);

        //立即修改,我知道了
        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (sureTv.getText().toString()){
                    case "我知道了":
                    default:
                        dialog.dismiss();
                        break;
                    case "立即修改":
                        USER_GAME_ACCOUNT_LOOK_MODIFY_COMMIT.onEvent();
                        String game_new_pwd_ets = game_new_pwd_et.getText().toString().trim() + "";
                        if(TextUtils.isEmpty(game_new_pwd_ets)){
                            G.showToast("请输入新密码");
                            return;
                        }

                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", gameAccountEntity.getId());
                        jsonObject.put("password", game_new_pwd_ets);
                        CommonUtil.getInstance()
                                .modifyGameAccount(activity, jsonObject, new Action1<Integer>() {
                                    @Override
                                    public void call(Integer integer) {
                                        if (integer == 1){
                                            G.showToast("修改成功!");
                                            EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DELETE_GAME_ACCOUNT_REFRESH));
                                            dialog.dismiss();
                                        }else {
                                            G.showToast("修改成功!");
                                        }
                                    }
                                });
                        break;
                }
            }
        });
        modify_pwd_tv_line.setVisibility(View.GONE);
        modify_pwd_tv.setVisibility(View.GONE);
        modify_pwd_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                USER_GAME_ACCOUNT_LOOK_MODIFY.onEvent();
                Message message = new Message();
                message.what = 0;
                message.obj = View.VISIBLE;
                mHandler.sendMessage(message);
            }
        });

        dialog_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
            }
        });
        try {
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(gameAccountEntity == null)
            initData();
        else
            initView();
    }

    /**
     * initData
     */
    private void initData(){
        CommonUtil.getInstance()
                .getGameAccountDetail(activity, id, new Action1<BaseMessage>() {
                    @Override
                    public void call(BaseMessage baseMessage) {
                        if(baseMessage != null){
                            gameAccountEntity = baseMessage.getData(GameAccountEntity.class);
                            if(gameAccountEntity != null){
                                initView();
                            }
                        }else {

                        }

                    }
                });
    }

    private void initView(){
        game_title_et.setText(gameAccountEntity.getDetailShowForGameType()+"");
        game_account_et.setText("账号："+gameAccountEntity.getAccount()+"");
        game_old_pwd_et.setText("原密码："+gameAccountEntity.getPassword()+"");
        modify_pwd_tv_line.setVisibility(gameAccountEntity.isXiaoMiAccount() ? View.GONE : View.VISIBLE);
        modify_pwd_tv.setVisibility(gameAccountEntity.isXiaoMiAccount() ? View.GONE : View.VISIBLE);
    }
}
