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

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
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.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.beans.AreaBean;
import com.tenlionsoft.baselib.core.retrofit_net.RetrofitManager;
import com.tenlionsoft.baselib.core.retrofit_net.api.BaseApiService;
import com.tenlionsoft.baselib.utils.UIUtil;
import com.tenlionsoft.baselib.utils.UserLgUtils;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 修改密码dialog
 */
public class ChangePwdDialog extends Dialog {

    private View view;
    private Context context;
    private EditText mEtOriginPwd;
    private EditText mEtNewPwd;
    private EditText mEtNewConfirmPwd;
    private boolean isShowOrgPwd;
    private boolean isShowNewPwd;
    private boolean isShowNewConfirmPwd;
    private OnChangeListener mChangeListener;

    private ChangePwdDialog(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.change_pwd_dialog, null);
        setContentView(view);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        mEtOriginPwd = view.findViewById(R.id.et_origin_pwd);
        mEtNewPwd = view.findViewById(R.id.et_new_pwd);
        mEtNewConfirmPwd = view.findViewById(R.id.et_new_confirm_pwd);
        ImageView ivShowOriPwd = view.findViewById(R.id.iv_show_pwd_origin);
        ImageView ivShowNewPwd = view.findViewById(R.id.iv_show_new_pwd);
        ImageView ivShowNewConfirmPwd = view.findViewById(R.id.iv_pwd_new_confirm);


        ivShowOriPwd.setOnClickListener(v -> {
            Editable etext = mEtOriginPwd.getText();
            Selection.setSelection(etext, etext.length());
            if (isShowOrgPwd) {
                isShowOrgPwd = false;
                mEtOriginPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivShowOriPwd.setBackgroundResource(R.drawable.ic_hide_pwd);
            } else {
                isShowOrgPwd = true;
                ivShowOriPwd.setBackgroundResource(R.drawable.ic_show_pwd);
                mEtOriginPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });


        ivShowNewPwd.setOnClickListener(v -> {
            Editable etext = mEtNewPwd.getText();
            Selection.setSelection(etext, etext.length());
            if (isShowNewPwd) {
                isShowNewPwd = false;
                mEtNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivShowNewPwd.setBackgroundResource(R.drawable.ic_hide_pwd);
            } else {
                isShowNewPwd = true;
                mEtNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivShowNewPwd.setBackgroundResource(R.drawable.ic_show_pwd);
            }
        });

        ivShowNewConfirmPwd.setOnClickListener(v -> {
            Editable etext = mEtNewConfirmPwd.getText();
            Selection.setSelection(etext, etext.length());
            if (isShowNewConfirmPwd) {
                isShowNewConfirmPwd = false;
                mEtNewConfirmPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivShowNewConfirmPwd.setBackgroundResource(R.drawable.ic_hide_pwd);
            } else {
                isShowNewConfirmPwd = true;
                mEtNewConfirmPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivShowNewConfirmPwd.setBackgroundResource(R.drawable.ic_show_pwd);
            }
        });

        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 orgPwd = mEtOriginPwd.getText().toString().trim();
                String newPwd = mEtNewPwd.getText().toString().trim();
                String confirmPwd = mEtNewConfirmPwd.getText().toString().trim();
                mChangeListener.doChange(orgPwd, newPwd, confirmPwd);
            }
        }
    }


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

    public interface OnChangeListener {
        void doChange(String org, String nPwd, String nCPwd);
    }
    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) {


        }
    }
    /**
     * 校验参数
     */
    private boolean checkParams() {
        String originPwd = mEtOriginPwd.getText().toString().trim();
        if (TextUtils.isEmpty(originPwd)) {
            ToastUtils.show("请输入原始密码");
            return false;
        }
        String newPwd = mEtNewPwd.getText().toString().trim();
        if (TextUtils.isEmpty(newPwd)) {
            ToastUtils.show("请输入新密码");
        }
        String newConfirmPwd = mEtNewConfirmPwd.getText().toString().trim();
        if (TextUtils.isEmpty(newConfirmPwd) || !newPwd.equals(newConfirmPwd)) {
            ToastUtils.show("请确认新密码是否一致");
            return false;
        }
        return true;
    }


    public static class DialogBuilder {
        private Context mContext;

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


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