/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.micode.notes.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import net.micode.notes.R;
import net.micode.notes.tool.PasswordUtils;
import net.micode.notes.ui.NoteEditActivity;

/**
 * 密码输入Activity
 * 用于输入加密便签的密码
 */
public class PasswordInputActivity extends Activity {
    private static final String TAG = "PasswordInputActivity";
    
    public static final String EXTRA_NOTE_ID = "note_id";
    
    private EditText mPasswordEditText;
    private Button mReturnButton;
    private Button mConfirmButton;
    private long mNoteId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_input);
        
        mNoteId = getIntent().getLongExtra(EXTRA_NOTE_ID, 0);
        if (mNoteId <= 0) {
            Toast.makeText(this, R.string.error_note_not_exist, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        initViews();
    }

    private void initViews() {
        mPasswordEditText = (EditText) findViewById(R.id.et_password);
        mReturnButton = (Button) findViewById(R.id.btn_return);
        mConfirmButton = (Button) findViewById(R.id.btn_confirm);
        
        mReturnButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回主界面
                finish();
            }
        });
        
        mConfirmButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = mPasswordEditText.getText().toString();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(PasswordInputActivity.this, 
                            R.string.password_empty, Toast.LENGTH_SHORT).show();
                    return;
                }
                
                // 验证密码
                if (PasswordUtils.verifyPassword(PasswordInputActivity.this, mNoteId, password)) {
                    // 密码正确，跳转到便签编辑界面
                    Intent intent = new Intent(PasswordInputActivity.this, NoteEditActivity.class);
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.putExtra(Intent.EXTRA_UID, mNoteId);
                    startActivity(intent);
                    finish();
                } else {
                    // 密码错误
                    Toast.makeText(PasswordInputActivity.this, 
                            R.string.password_error, Toast.LENGTH_SHORT).show();
                    mPasswordEditText.setText("");
                }
            }
        });
    }
}
