/*
 * 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 net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.model.WorkingNote;
import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashSet;

/**
 * 便签预览界面（只读模式，用于回收站中的便签）
 */
public class NotePreviewActivity extends Activity implements OnClickListener {
    private static final String TAG = "NotePreviewActivity";
    
    private Long mNoteId;
    private WorkingNote mWorkingNote;
    
    private View mNoteLayout;
    private ScrollView mScrollView;
    private TextView mNoteContent;
    private Button mBtnRestore;
    private Button mBtnDelete;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_preview);
        
        initViews();
        
        Intent intent = getIntent();
        mNoteId = intent.getLongExtra(Intent.EXTRA_UID, 0);
        
        if (mNoteId != 0) {
            loadNote();
        } else {
            finish();
        }
    }
    
    private void initViews() {
        mNoteLayout = findViewById(R.id.note_layout);
        mScrollView = (ScrollView) findViewById(R.id.note_scroller);
        mNoteContent = (TextView) findViewById(R.id.note_content);
        mBtnRestore = (Button) findViewById(R.id.btn_restore);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        
        mBtnRestore.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }
    
    private void loadNote() {
        // 从数据库中加载便签
        Cursor cursor = getContentResolver().query(
                Notes.CONTENT_NOTE_URI,
                null,
                NoteColumns.ID + "=? AND " + NoteColumns.STATUS + "=" + Notes.NOTE_STATUS_DELETED,
                new String[] { String.valueOf(mNoteId) },
                null);
        
        if (cursor != null && cursor.moveToFirst()) {
            // 获取便签内容
            String snippet = cursor.getString(cursor.getColumnIndex(NoteColumns.SNIPPET));
            int bgColorId = cursor.getInt(cursor.getColumnIndex(NoteColumns.BG_COLOR_ID));
            long deleteDate = cursor.getLong(cursor.getColumnIndex(NoteColumns.DELETE_DATE));
            
            // 设置便签内容
            if (!TextUtils.isEmpty(snippet)) {
                mNoteContent.setText(snippet);
            } else {
                mNoteContent.setText(R.string.trash_empty_note);
            }
            
            // 设置背景颜色
            int bgResId = ResourceParser.getNoteBgResource(bgColorId);
            mNoteLayout.setBackgroundResource(bgResId);
            
            // 设置标题
            setTitle(R.string.trash_note_preview);
            
            cursor.close();
        } else {
            // 便签不存在或已被删除
            Toast.makeText(this, R.string.trash_note_not_found, Toast.LENGTH_SHORT).show();
            finish();
        }
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_restore:
                // 恢复便签
                restoreNote();
                break;
                
            case R.id.btn_delete:
                // 彻底删除便签
                deleteNote();
                break;
        }
    }
    
    /**
     * 恢复便签
     */
    private void restoreNote() {
        HashSet<Long> ids = new HashSet<Long>();
        ids.add(mNoteId);
        
        if (DataUtils.batchRestoreFromTrash(getContentResolver(), ids)) {
            Toast.makeText(this, R.string.trash_restore_success, Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        } else {
            Toast.makeText(this, R.string.trash_restore_failed, Toast.LENGTH_SHORT).show();
        }
    }
    
    /**
     * 彻底删除便签
     */
    private void deleteNote() {
        HashSet<Long> ids = new HashSet<Long>();
        ids.add(mNoteId);
        
        if (DataUtils.batchPermanentlyDeleteNotes(getContentResolver(), ids)) {
            Toast.makeText(this, R.string.trash_delete_success, Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        } else {
            Toast.makeText(this, R.string.trash_delete_failed, Toast.LENGTH_SHORT).show();
        }
    }
}