package net.micode.notes.ui;


import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

import net.micode.notes.model.WorkingNote;
import net.micode.notes.R;
import net.micode.notes.ui.LockPatternView;

public class UnlockActivity extends AppCompatActivity {

    private LockPatternView mLockPatternView;
    private String mPasswordStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);

        mLockPatternView = (LockPatternView) findViewById(R.id.lockView);
        Intent pre = getIntent();
        final Long noteId = pre.getLongExtra(Intent.EXTRA_UID, 0);

        mLockPatternView.setLockListener(new LockPatternView.OnLockListener() {
            WorkingNote mWorkingNote = WorkingNote.load(UnlockActivity.this,noteId);
            String password = mWorkingNote.getPasscode();
            @Override
            public void getStringPassword(String password) {
                mPasswordStr = password;
            }

            @Override
            public boolean isPassword() {
                if (mPasswordStr.equals(password)) {
                    Toast.makeText(UnlockActivity.this, "密码正确", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UnlockActivity.this, NoteEditActivity.class);
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.putExtra("lock",0);
                    intent.putExtra(Intent.EXTRA_UID, noteId);
                    startActivity(intent);
                    UnlockActivity.this.finish();
                    //TODO comment or not
                    //return true;
                } else {
                    Toast.makeText(UnlockActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

    }

}
