package net.micode.notes.ui;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;


import android.view.View;
import android.widget.Button;
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 Activity implements View.OnClickListener {
    private LockPatternView mLockPatternView;
    private String mPasswordStr;
    private Button mRetrievePassword;


    public void onClick(View v){
        switch (v.getId()) {
            case R.id.retrieve_password:
                System.out.println("1");
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(getString(R.string.find_passcode));
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                Intent pre = getIntent();
                final Long noteId = pre.getLongExtra(Intent.EXTRA_UID, 0);
                WorkingNote mWorkingNote = WorkingNote.load(UnlockActivity.this,noteId);
                builder.setMessage(mWorkingNote.getPasscode());
                builder.setPositiveButton(android.R.string.ok, null);
                builder.show();
                break;
            default:
                break;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);

        mLockPatternView = (LockPatternView) findViewById(R.id.lockView);
        mRetrievePassword = (Button) findViewById(R.id.retrieve_password);
        mRetrievePassword.setOnClickListener(this);
        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();
                    mRetrievePassword.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });
    }
}
