package net.micode.notes.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import net.micode.notes.R;
import net.micode.notes.data.PrivacyPasswordManager;

public class PrivacyPasswordDialog {
    private Context mContext;
    private PrivacyPasswordManager mPasswordManager;
    private OnPasswordVerifiedListener mListener;

    public interface OnPasswordVerifiedListener {
        void onPasswordVerified();
    }

    public PrivacyPasswordDialog(Context context, OnPasswordVerifiedListener listener) {
        mContext = context;
        mPasswordManager = new PrivacyPasswordManager(context);
        mListener = listener;
    }

    /**
     * 显示设置密码对话框
     */
    public void showSetPasswordDialog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_privacy_password, null);
        final EditText passwordEdit = view.findViewById(R.id.password_edit);
        final EditText confirmPasswordEdit = view.findViewById(R.id.confirm_password_edit);

        new AlertDialog.Builder(mContext)
            .setTitle(R.string.set_privacy_password)
            .setView(view)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String password = passwordEdit.getText().toString();
                    String confirmPassword = confirmPasswordEdit.getText().toString();

                    if (password.isEmpty()) {
                        Toast.makeText(mContext, R.string.password_cannot_be_empty, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (!password.equals(confirmPassword)) {
                        Toast.makeText(mContext, R.string.passwords_do_not_match, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (mPasswordManager.setPassword(password)) {
                        Toast.makeText(mContext, R.string.password_set_success, Toast.LENGTH_SHORT).show();
                        if (mListener != null) {
                            mListener.onPasswordVerified();
                        }
                    } else {
                        Toast.makeText(mContext, R.string.password_set_failed, Toast.LENGTH_SHORT).show();
                    }
                }
            })
            .setNegativeButton(android.R.string.cancel, null)
            .show();
    }

    /**
     * 显示验证密码对话框
     */
    public void showVerifyPasswordDialog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_verify_privacy_password, null);
        final EditText passwordEdit = view.findViewById(R.id.password_edit);

        new AlertDialog.Builder(mContext)
            .setTitle(R.string.verify_privacy_password)
            .setView(view)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String password = passwordEdit.getText().toString();

                    if (password.isEmpty()) {
                        Toast.makeText(mContext, R.string.password_cannot_be_empty, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (mPasswordManager.verifyPassword(password)) {
                        if (mListener != null) {
                            mListener.onPasswordVerified();
                        }
                    } else {
                        Toast.makeText(mContext, R.string.incorrect_password, Toast.LENGTH_SHORT).show();
                    }
                }
            })
            .setNegativeButton(android.R.string.cancel, null)
            .show();
    }

    /**
     * 检查是否需要设置密码
     */
    public boolean needsPasswordSetup() {
        return !mPasswordManager.hasPassword();
    }
} 