/*
 * 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.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;

import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils;

import java.io.IOException;

/**
 * AlarmAlertActivity 类用于处理笔记提醒,当笔记提醒时间到达时,
 * 显示提醒对话框并播放提醒声音。
 */
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
    // 存储笔记的 ID
    private long mNoteId;
    // 存储笔记的摘要信息
    private String mSnippet;
    // 摘要信息显示的最大长度
    private static final int SNIPPET_PREW_MAX_LEN = 60;
    // 用于播放提醒声音的媒体播放器
    MediaPlayer mPlayer;

    /**
     * Activity 创建时调用的方法,进行初始化操作。
     * @param savedInstanceState 如果 Activity 是重新创建的,则包含之前保存的状态信息。
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 请求不显示标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 获取当前窗口
        final Window win = getWindow();
        // 添加标志,使窗口在锁屏状态下也能显示
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        // 如果屏幕处于关闭状态
        if (!isScreenOn()) {
            // 添加标志,保持屏幕常亮、点亮屏幕、允许在屏幕开启时锁定等
            win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
        }

        // 获取启动该 Activity 的 Intent
        Intent intent = getIntent();

        try {
            // 从 Intent 的数据路径中获取笔记的 ID
            mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
            // 根据笔记 ID 获取笔记的摘要信息
            mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
            // 如果摘要信息长度超过最大显示长度,则进行截取并添加省略号
            mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
                    SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
                    : mSnippet;
        } catch (IllegalArgumentException e) {
            // 处理异常,打印堆栈信息
            e.printStackTrace();
            return;
        }

        // 创建媒体播放器实例
        mPlayer = new MediaPlayer();
        // 检查笔记是否在数据库中可见
        if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
            // 显示提醒对话框
            showActionDialog();
            // 播放提醒声音
            playAlarmSound();
        } else {
            // 如果笔记不可见,则关闭该 Activity
            finish();
        }
    }

    /**
     * 检查屏幕是否处于开启状态。
     * @return 如果屏幕开启返回 true,否则返回 false。
     */
    private boolean isScreenOn() {
        // 获取电源管理服务
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        // 检查屏幕是否开启
        return pm.isScreenOn();
    }

    /**
     * 播放提醒声音。
     */
    private void playAlarmSound() {
        // 获取默认的闹钟铃声 URI
        Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);

        // 获取静音模式下受影响的音频流设置
        int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);

        // 根据静音模式设置音频流类型
        if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
            mPlayer.setAudioStreamType(silentModeStreams);
        } else {
            mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        }
        try {
            // 设置音频数据源
            mPlayer.setDataSource(this, url);
            // 准备媒体播放器
            mPlayer.prepare();
            // 设置循环播放
            mPlayer.setLooping(true);
            // 开始播放
            mPlayer.start();
        } catch (IllegalArgumentException e) {
            // 处理异常,打印堆栈信息
            e.printStackTrace();
        } catch (SecurityException e) {
            // 处理异常,打印堆栈信息
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // 处理异常,打印堆栈信息
            e.printStackTrace();
        } catch (IOException e) {
            // 处理异常,打印堆栈信息
            e.printStackTrace();
        }
    }

    /**
     * 显示提醒对话框。
     */
    private void showActionDialog() {
        // 创建对话框构建器
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        // 设置对话框标题
        dialog.setTitle(R.string.app_name);
        // 设置对话框显示的消息,即笔记摘要信息
        dialog.setMessage(mSnippet);
        // 设置确认按钮及其点击监听器
        dialog.setPositiveButton(R.string.notealert_ok, this);
        // 如果屏幕处于开启状态,设置进入笔记编辑页面的按钮
        if (isScreenOn()) {
            dialog.setNegativeButton(R.string.notealert_enter, this);
        }
        // 显示对话框并设置对话框关闭监听器
        dialog.show().setOnDismissListener(this);
    }

    /**
     * 处理对话框按钮点击事件。
     * @param dialog 触发点击事件的对话框。
     * @param which 被点击的按钮标识。
     */
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_NEGATIVE:
                // 创建一个 Intent 用于启动笔记编辑 Activity
                Intent intent = new Intent(this, NoteEditActivity.class);
                // 设置 Intent 的操作类型为查看
                intent.setAction(Intent.ACTION_VIEW);
                // 传递笔记的 ID 到笔记编辑 Activity
                intent.putExtra(Intent.EXTRA_UID, mNoteId);
                // 启动笔记编辑 Activity
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    /**
     * 处理对话框关闭事件。
     * @param dialog 关闭的对话框。
     */
    public void onDismiss(DialogInterface dialog) {
        // 停止播放提醒声音
        stopAlarmSound();
        // 关闭当前 Activity
        finish();
    }

    /**
     * 停止播放提醒声音并释放媒体播放器资源。
     */
    private void stopAlarmSound() {
        if (mPlayer != null) {
            // 停止播放
            mPlayer.stop();
            // 释放媒体播放器资源
            mPlayer.release();
            mPlayer = null;
        }
    }
}