/*
 * 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.tool;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.NotesDatabaseHelper;

/**
 * 密码管理工具类
 * 用于管理加密便签的密码
 */
public class PasswordUtils {
    private static final String TAG = "PasswordUtils";

    /**
     * 保存便签密码
     * @param context 上下文对象
     * @param noteId 便签ID
     * @param password 密码
     * @return 是否保存成功
     */
    public static boolean savePassword(Context context, long noteId, String password) {
        if (noteId <= 0 || password == null || password.isEmpty()) {
            Log.e(TAG, "Invalid noteId or password");
            return false;
        }

        SQLiteDatabase db = NotesDatabaseHelper.getInstance(context).getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(NoteColumns.ID, noteId);
        values.put("password", password);

        // 先删除旧密码（如果存在）
        db.delete(NotesDatabaseHelper.TABLE.ENCRYPTED_NOTE_PASSWORD, 
                NoteColumns.ID + "=?", new String[]{String.valueOf(noteId)});
        
        // 插入新密码
        long result = db.insert(NotesDatabaseHelper.TABLE.ENCRYPTED_NOTE_PASSWORD, null, values);
        return result != -1;
    }

    /**
     * 获取便签密码
     * @param context 上下文对象
     * @param noteId 便签ID
     * @return 密码，如果不存在返回null
     */
    public static String getPassword(Context context, long noteId) {
        if (noteId <= 0) {
            return null;
        }

        SQLiteDatabase db = NotesDatabaseHelper.getInstance(context).getReadableDatabase();
        Cursor cursor = db.query(NotesDatabaseHelper.TABLE.ENCRYPTED_NOTE_PASSWORD,
                new String[]{"password"},
                NoteColumns.ID + "=?",
                new String[]{String.valueOf(noteId)},
                null, null, null);

        String password = null;
        if (cursor != null && cursor.moveToFirst()) {
            password = cursor.getString(0);
            cursor.close();
        }
        return password;
    }

    /**
     * 验证密码
     * @param context 上下文对象
     * @param noteId 便签ID
     * @param inputPassword 输入的密码
     * @return 密码是否正确
     */
    public static boolean verifyPassword(Context context, long noteId, String inputPassword) {
        String savedPassword = getPassword(context, noteId);
        if (savedPassword == null) {
            return false;
        }
        return savedPassword.equals(inputPassword);
    }

    /**
     * 删除便签密码
     * @param context 上下文对象
     * @param noteId 便签ID
     * @return 是否删除成功
     */
    public static boolean deletePassword(Context context, long noteId) {
        if (noteId <= 0) {
            return false;
        }

        SQLiteDatabase db = NotesDatabaseHelper.getInstance(context).getWritableDatabase();
        int result = db.delete(NotesDatabaseHelper.TABLE.ENCRYPTED_NOTE_PASSWORD,
                NoteColumns.ID + "=?",
                new String[]{String.valueOf(noteId)});
        return result > 0;
    }

    /**
     * 检查便签是否加密
     * @param context 上下文对象
     * @param noteId 便签ID
     * @return 是否加密
     */
    public static boolean isNoteEncrypted(Context context, long noteId) {
        if (noteId <= 0) {
            return false;
        }

        Cursor cursor = context.getContentResolver().query(
                android.content.ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
                new String[]{NoteColumns.IS_ENCRYPTED},
                null, null, null);

        boolean isEncrypted = false;
        if (cursor != null && cursor.moveToFirst()) {
            isEncrypted = cursor.getInt(0) == 1;
            cursor.close();
        }
        return isEncrypted;
    }
}
