package com.bonait.bnframework.common.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.model.mode.BomTypeInfo;
import com.bonait.bnframework.common.model.mode.FoodMenuInfo;
import com.bonait.bnframework.common.model.mode.GoodsParentTypeInfo;
import com.bonait.bnframework.common.model.mode.GoodsTypeInfo;
import com.bonait.bnframework.common.model.mode.GoodsUintInfo;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AddUserDialog extends Dialog {
    private Button yes, no;//确定按钮

    //用户名称  账户名称 用户密码 再次确认密码 用户角色
    private EditText edittext, edittext1, edittext2, edittext3, edittext4;

    //确定文本和取消文本的显示内容
    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public AddUserDialog(Context context) {
        super(context, R.style.Dialog_Msg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_user);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DataVerification()) {
                    if (yesOnclickListener != null) {
                        yesOnclickListener.onYesClick(
                                edittext.getText().toString()
                                , edittext1.getText().toString()
                                , edittext2.getText().toString()
                                , edittext4.getText().toString()
                        );
                    }
                }
            }
        });

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });

        edittext4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkboxEdit(edittext4);
            }
        });
    }

    /**
     * 多选下拉框
     */
    private void checkboxEdit(EditText edit) {
        final String[] items = ConfigName.getInstance().UserRole.toArray(new String[0]);
        final int checkIndex = 0;
        new QMUIDialog.CheckableDialogBuilder(getContext())
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        edit.setText(items[which]);
                        dialog.dismiss();
                    }
                })
                .setCheckedIndex(checkIndex)
                .show();
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果设置按钮的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        edittext = (EditText) findViewById(R.id.edittext);
        edittext1 = (EditText) findViewById(R.id.edittext1);
        edittext2 = (EditText) findViewById(R.id.edittext2);
        edittext3 = (EditText) findViewById(R.id.edittext3);
        edittext4 = (EditText) findViewById(R.id.edittext4);

    }

    /**
     * 数据验证
     *
     * @return
     */
    public boolean DataVerification() {
        boolean IsSucees = false;
        try {
            if (edittext.getText().toString().isEmpty()) {
                ToastUtils.info("用户名称不能为空!");
                return false;
            }
            if (edittext1.getText().toString().isEmpty()) {
                ToastUtils.info("账户名称不能为空!");
                return false;
            }
            if (edittext2.getText().toString().isEmpty()) {
                ToastUtils.info("用户密码不能为空!");
                return false;
            }
            if (edittext3.getText().toString().isEmpty()) {
                ToastUtils.info("用户密码不能为空!");
                return false;
            }

            if (!edittext3.getText().toString().equals(edittext2.getText().toString())) {
                ToastUtils.info("两次密码输入不一致!");
                return false;
            }
            if (edittext4.getText().toString().isEmpty()) {
                ToastUtils.info("用户角色不能为空!");
                return false;
            }
            return true;
        } catch (Exception ex) {
            ToastUtils.error("数据验证异常!" + ex.getMessage());
            return IsSucees;
        }
    }

    public void SetAdapter(Spinner spinner, Map<String, String> map) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_text_item, new ArrayList<>(map.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick(String name, String acc, String pass, String qx);
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }
}

