package com.sec.tool;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.secspace.lib.common.utils.ToastUtil;

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

public class LoginActivity extends AppCompatActivity {
    private EditText mEditPwd;
    private boolean isRequest = false;
    private AlertDialog mTipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEditPwd = findViewById(R.id.edit_pwd);
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isAllowed = PermissionsHelper.checkPermissions(this, PermissionsHelper.requestPermissions);
        Log.i("LoginActivity", "isAllowed: " + isAllowed);
        if (!isAllowed) {
            //  禁止在访问时只能禁止一次  否则会一直弹界面
            if (!isRequest) {
                PermissionsHelper.requestPermissions(this, 10001, PermissionsHelper.requestPermissions);
            }
            return;
        }
    }

    public void login(View view) {
        String pwd = mEditPwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.showShort(this, "密码不能为空");
            return;
        }
        if (!TextUtils.equals(pwd, "181917!a")) {
            ToastUtil.showShort(this, "密码错误，请重新输入");
            return;
        }
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        List<String> notRemindPermissions = new ArrayList<>();
        List<String> remindPermissions = new ArrayList<>();
        for (String permission : permissions) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)
                    && ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED) {
                // 当前状态为拒绝，并且不再提醒
                notRemindPermissions.add(permission);
            } else {
                remindPermissions.add(permission);
            }
        }
        if (remindPermissions.size() > 0) {
            Log.i("LoginActivity", "remindPermissions: " + remindPermissions.toString());
            // 需要提示的权限
//            PermissionsHelper.requestPermissions(this,10001, (String[]) remindPermissions.toArray());
            return;
        }
        if (notRemindPermissions.size() > 0) {
            // 需要申请的权限
            isRequest = true;
            Log.i("LoginActivity", "notRemindPermissions: " + notRemindPermissions.toString());
            showPwdDialog(getString(R.string.not_user_prmission), getString(R.string.work_sure), null, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mTipDialog = null;
                    dialog.dismiss();
                    finish();
                }
            }, null);
            return;

        }
    }
    /**
     * dialog
     *
     * @param title
     * @param positiveText
     * @param negativeText
     * @param positiveClickListener
     * @param negativeClickListener
     */
    private void showPwdDialog(String title, String positiveText, String negativeText, DialogInterface.OnClickListener positiveClickListener, DialogInterface.OnClickListener negativeClickListener) {
        if (mTipDialog != null && mTipDialog.isShowing()) {
            mTipDialog.dismiss();
        }
        View view = LayoutInflater.from(this).inflate(R.layout.item_ll, null);
        LinearLayout ll = view.findViewById(R.id.item_ll);
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(title)
                .setCancelable(false)
                .setView(view);
        if (positiveClickListener != null) {
            builder.setPositiveButton(positiveText, positiveClickListener);
        }
        if (negativeClickListener != null) {
            builder.setNegativeButton(negativeText, negativeClickListener);
        }
        if (mTipDialog == null) {
            mTipDialog = builder.create();
        }
        if (!mTipDialog.isShowing()) {
            mTipDialog.show();
        }
    }

}
