package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GamePayList;
import com.sheep.gamegroup.model.entity.OrienteeringDetail;
import com.sheep.gamegroup.model.entity.PayEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import io.reactivex.schedulers.Schedulers;

/**
 * 游戏充值账号输入
 * Created by ljy on 2018/6/13.
 */

public class DialogPayAccount {
    private Activity activity;
    private String gameId;
    private PayEntity payEntity;

    private List<GamePayList> gamePayLists = new ArrayList<>();
    private EditText game_et, game_account_et, game_pay_et, game_pwd_et, game_area_et, game_role_et, game_role_id_et, game_goods_et;
    private RadioGroup radio_group_tencent;
    //qq:1 wx:2
    private int tencentType;
    private boolean isShow;

    public DialogPayAccount(Activity activity, PayEntity payEntity) {
        this.activity = activity;
        this.payEntity = payEntity;
        this.gameId = (payEntity != null ? (payEntity.getGame_id() == null ? "0" : payEntity.getGame_id()) : "0");
    }

    public DialogPayAccount(Activity activity, String gameId) {
        this.activity = activity;
        this.gameId = gameId;
    }

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

    public void showPayaccount() {
        if (activity == null) {
            return;
        }
        View dialog_parent = View.inflate(activity, R.layout.dialog_parent, null);
        final AlertDialog 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_pay_account, dialog_center_ll, true);
        dialog_title.setText(payEntity.getDetailShow());

        TextView sureTv = view.findViewById(R.id.pay_sure_tv);
        game_account_et = view.findViewById(R.id.game_account_et);
        game_pay_et = view.findViewById(R.id.game_pay_et);

        game_pwd_et = view.findViewById(R.id.game_pwd_et);
        game_et = view.findViewById(R.id.game_et);
        game_area_et = view.findViewById(R.id.game_area_et);
        game_role_et = view.findViewById(R.id.game_role_et);
        game_role_id_et = view.findViewById(R.id.game_role_id_et);
        game_goods_et = view.findViewById(R.id.game_goods_et);

        /**
         * radio_group_tencent
         */
        radio_group_tencent = view.findViewById(R.id.radio_group_tencent);
        radio_group_tencent.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                // 获取选中的RadioButton的id
                int id = group.getCheckedRadioButtonId();
                // 通过id实例化选中的这个RadioButton
                RadioButton choise = (RadioButton) activity.findViewById(id);
                switch (id) {
                    case R.id.conmon_id_tencent:
                        tencentType = 1;
                        break;
                    case R.id.fixation_id_tencent:
                        tencentType = 2;
                        break;
                }
            }
        });
        if (payEntity.getTask_type().equals("1003")) {
            radio_group_tencent.setVisibility(View.VISIBLE);
        } else {
            radio_group_tencent.setVisibility(View.GONE);
        }

        /**
         * 游戏列表
         */

        game_et.addTextChangedListener(new TextWatcher() {
            String oldStr;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                oldStr = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isShow = false;
                    }
                }, 1000);
                if (!TextUtils.isEmpty(s) && s.length() > 0 && payEntity != null && CommonUtil.getInstance().clickTime(500) && !isShow && !s.equals(oldStr)) {
                    intitDataAccountList(s.toString(), payEntity);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String game_account_etStr = game_account_et.getText().toString().trim() + "";
                String game_pay_etStr = game_pay_et.getText().toString().trim() + "";

                if (TextUtils.isEmpty(game_account_etStr)) {
                    G.showToast("游戏账号不能为空!");
                    return;
                }
                if (payEntity == null || TextUtils.isEmpty(payEntity.getGame_id()) || gameId.equals("0")) {
                    G.showToast("请选择游戏!");
                    return;
                }
                if (showToast(game_pwd_et)) {
                    G.showToast("请输入密码");
                    return;
                }
//                if(showToast(game_area_et)){
//                    G.showToast("请输入区服");
//                    return;
//                }
                if (showToast(game_role_et)) {
                    G.showToast("请输入角色");
                    return;
                }
//                if(showToast(game_role_id_et)){
//                    G.showToast("请输入角色ID");
//                    return;
//                }
                if (showToast(game_goods_et)) {
                    G.showToast("请输入商品");
                    return;
                }
                if (radio_group_tencent.isShown() && tencentType < 1) {
                    G.showToast("请选择QQ或者微信");
                    return;
                }
                payEntity.setAccount(game_account_etStr);
                payEntity.setPwd(game_pwd_et.getText().toString().trim());
                payEntity.setService(game_area_et.getText().toString().trim());
                payEntity.setRole_name(game_role_et.getText().toString().trim());
                payEntity.setRole_id(game_role_id_et.getText().toString().trim());
                payEntity.setGoods(game_goods_et.getText().toString().trim());
                payEntity.setTencent_platform(tencentType);

                ViewUtil.showGamePay(activity, payEntity);

                dialog.dismiss();
            }
        });

        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();
        }
        initView();
    }

    public void initData() {
        SheepApp.getInstance().getNetComponent().getApiService().can_user_sheep_amount(0, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<OrienteeringDetail> newList = baseMessage.getDatas(OrienteeringDetail.class);

                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast(baseMessage);
                    }
                });
    }


    /**
     * 游戏列表显示
     */
    private void intitDataAccountList(String name, PayEntity payEntity) {

        CommonUtil.getInstance()
                .gameTaskList(activity, Integer.parseInt(payEntity.getTask_type()), name, new Action1<BaseMessage>() {
                    @Override
                    public void call(BaseMessage baseMessage) {
                        try {
                            List<GamePayList> taskReleaseEtyList = baseMessage.getDatas(GamePayList.class);
                            if (!ListUtil.isEmpty(taskReleaseEtyList)) {
                                gamePayLists.clear();
                                gamePayLists.addAll(taskReleaseEtyList);
                                showListAccount(game_et);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void showListAccount(final EditText game_account_et) {
        SelfPopupWindow.newInstance()
                .SelfPopupWindow(activity, game_account_et, new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                GamePayList entity = (GamePayList) gamePayLists.get(position);
                                isShow = true;
                                game_account_et.setText(entity.getName() + "");
                                if (payEntity == null)
                                    return;
                                payEntity.setGame_id(entity.getGame_id());
                                gameId = entity.getGame_id();
                                SelfPopupWindow.newInstance().hidePop();
                            }
                        },
                        new com.sheep.gamegroup.view.adapter.ArrayAdapter<GamePayList>(activity, R.layout.dialog_item_game_list, gamePayLists) {
                            @Override
                            public boolean convert(int position, View convertView, ViewGroup parent, GamePayList item) {
                                final TextView name_tv = convertView.findViewById(R.id.name_tv);
                                name_tv.setText(item.getName() + "");
                                return true;
                            }
                        })
                .initPopup()
                .showPop();
    }

    /**
     * task_type:1002平台游戏，1003 腾讯游戏，1004 小米游戏
     */
    private void initView() {
//        game_et,game_account_et,game_pay_et,game_pwd_et,game_area_et,game_role_et,game_role_id_et,game_goods_et;
        if (payEntity != null) {
            switch (payEntity.getTask_type()) {
                case "1002":
                    CommonUtil.getInstance().showView(gameId.equals("0") ? View.VISIBLE : View.GONE, game_et);
                    CommonUtil.getInstance().showView(View.GONE, game_pwd_et);
                    CommonUtil.getInstance().showView(View.GONE, game_area_et);
                    CommonUtil.getInstance().showView(View.GONE, game_role_et);
                    CommonUtil.getInstance().showView(View.GONE, game_role_id_et);
                    CommonUtil.getInstance().showView(View.GONE, game_goods_et);
                    break;
                case "1003":
                    CommonUtil.getInstance().showView(gameId.equals("0") ? View.VISIBLE : View.GONE, game_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_pwd_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_area_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_role_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_role_id_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_goods_et);
                    break;
                case "1004":
                    CommonUtil.getInstance().showView(gameId.equals("0") ? View.VISIBLE : View.GONE, game_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_pwd_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_area_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_role_et);
                    CommonUtil.getInstance().showView(View.VISIBLE, game_role_id_et);
                    CommonUtil.getInstance().showView(View.GONE, game_goods_et);
                    break;
            }
        }

    }

    /**
     * 是否输入
     */
    private boolean showToast(EditText view) {
        if (view.isShown() && TextUtils.isEmpty(view.getText().toString().trim())) {
            return true;
        }
        return false;
    }

}
