package com.tenlionsoft.baselib.core.widget.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;

import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.utils.RegexUtils;

import androidx.appcompat.widget.SwitchCompat;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 添加人员
 */
public class AddPersonDialog extends Dialog {

    private View view;
    private Context context;
    private EditText mEtName;
    private EditText mEtIdCard;
    private OnChangeListener mChangeListener;
    private SwitchCompat mSwIsKey;
    private int mIsKey = 0;

    private AddPersonDialog(Context context) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_add_person, null);
        setContentView(view);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        mEtName = view.findViewById(R.id.et_name);
        mEtIdCard = view.findViewById(R.id.et_id_card);
        mSwIsKey = view.findViewById(R.id.sw_is_key);
        mSwIsKey.setOnCheckedChangeListener((buttonView, isChecked) -> mIsKey = isChecked ? 1 : 0);

        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> this.dismiss());
        view.findViewById(R.id.tv_confirm).setOnClickListener(v -> doConfirm());
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.dialog_center_alpha);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (displayMetrics.widthPixels * 0.9);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    private void doConfirm() {
        if (checkParams()) {
            if (mChangeListener != null) {
                String name = mEtName.getText().toString().trim();
                String idcard = mEtIdCard.getText().toString().trim();
                mChangeListener.doChange(name, idcard, mIsKey + "");
            }
        }
    }

    public void HideSoftKeyBoardDialog(Activity activity) {
        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        } catch (Exception ex) {


        }
    }

    public void addOnChangeListener(OnChangeListener listener) {
        this.mChangeListener = listener;
    }

    public interface OnChangeListener {
        void doChange(String org, String nPwd, String nCPwd);
    }

    /**
     * 校验参数
     */
    private boolean checkParams() {
        String originPwd = mEtName.getText().toString().trim();
        if (TextUtils.isEmpty(originPwd)) {
            ToastUtils.show("请输入姓名");
            return false;
        }
        String idcard = mEtIdCard.getText().toString().trim();
        if (TextUtils.isEmpty(idcard) || !RegexUtils.isIDNumber(idcard)) {
            ToastUtils.show("请输入正确的身份证号码");
            return false;
        }
        return true;
    }


    public static class DialogBuilder {
        private Context mContext;

        public DialogBuilder(Context context) {
            this.mContext = context;
        }


        public AddPersonDialog build() {
            return new AddPersonDialog(mContext);
        }
    }
}