package com.android.launcher3;


import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.android.launcher3.function.setting.AdminSettingActivity;
import com.android.launcher3.settings.ErrorTimeHelper;
import com.secspace.lib.common.utils.InputUtil;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.lib.common.utils.ToastUtil;


public class ManagerAuthActivity extends BaseActivity {
    protected Context mAppContext;
    AlertDialog mPwdDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_auth);
        mAppContext = this.getApplicationContext();
        showPwdDialog();
    }

    private void showPwdDialog() {
        if (mPwdDialog == null) {
            final EditText editText = new EditText(this);
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//            TextView textView = new TextView(this);
//            textView.setText("验证密保");
//            textView.setTextColor(getColor(R.color.activation_black));
//            textView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
//            textView.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
//            textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
//            textView.setPadding(15, 15, 15, 15);
            View view = LayoutInflater.from(mAppContext).inflate(R.layout.item_ll, null);
            LinearLayout ll = view.findViewById(R.id.item_ll);
            ll.addView(editText);
//            ll.addView(textView);
//            textView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(ManagerAuthActivity.this, VerifyPwdProtectionActivity.class);
//                    intent.putExtra(FROM_WHERE, ManagerAuthActivity.class.getSimpleName());
//                    startActivity(intent);
//                    if (mPwdDialog != null && mPwdDialog.isShowing()) {
//                        InputUtil.closeSoftInput(getApplicationContext(), editText);
//                        mPwdDialog.dismiss();
//                        finish();
//                    }
//                }
//            });
            mPwdDialog = new AlertDialog.Builder(this)
                    .setTitle("请输入密码")
                    .setCancelable(false)
                    .setView(view)
                    .setNegativeButton(R.string.work_cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    })
                    .setPositiveButton(R.string.work_sure, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            InputUtil.closeSoftInput(getApplicationContext(), editText);
                            boolean canInputPwd = ErrorTimeHelper.getInstance(mAppContext).canInputPwd();
                            if (canInputPwd){
                                if (TextUtils.equals(editText.getText().toString(), Prefs.getLoginPwd(mAppContext))) {
                                    ToastUtil.showLong(mAppContext, "密码正确！");
                                    finish();
                                    ErrorTimeHelper.getInstance(mAppContext).resetLockTimeAndCount();
                                    Intent intent = new Intent();
                                    intent.setClass(mAppContext, AdminSettingActivity.class);
                                    startActivity(intent);
                                } else {
                                    ToastUtil.showLong(mAppContext, "密码错误！");
                                    ErrorTimeHelper.getInstance(mAppContext).addErrorCount();
                                    finish();
                                }
                            }else {
                                ToastUtil.showShort(mAppContext,"请在"+ ErrorTimeHelper.getInstance(mAppContext).getCountDownTime()+"秒后重试");
                                finish();
                            }
                        }
                    })
                    .create();
        }
        if (!mPwdDialog.isShowing()) {
            mPwdDialog.show();
        }
    }
}
