/*
 * 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 java.util.Calendar;

import net.micode.notes.R;
import net.micode.notes.ui.DateTimePicker;
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat;
import android.text.format.DateUtils;

//继承自 AlertDialog，包含日期和时间选择的功能
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {

    //用于存储当前选择日期和时间
    private Calendar mDate = Calendar.getInstance();
    //标记是否使用24小时视图展示时间
    private boolean mIs24HourView;
    //回调接口定义，当日期和时间设定后激活
    private OnDateTimeSetListener mOnDateTimeSetListener;
    //本对话框包含的日期和时间选择器视图
    private DateTimePicker mDateTimePicker;
    //定义回调接口，用于通知调用者日期和时间已经设定
    public interface OnDateTimeSetListener {
        void OnDateTimeSet(AlertDialog dialog, long date);
    }

    //DateTimePickerDialog构造函数
    public DateTimePickerDialog(Context context, long date) {
        super(context); //调用AlertDiaglog构造函数
        mDateTimePicker = new DateTimePicker(context);//创建一个新的DateTimePicker实例
        setView(mDateTimePicker);//将DateTimePicker视图添加到对话框中
        //注册一个回调，以在日期和时间改变时更新内部的Calendar实例
        mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
            public void onDateTimeChanged(DateTimePicker view, int year, int month,
                    int dayOfMonth, int hourOfDay, int minute) {
                //更新Calendar实例的年、月、日、时、分
                mDate.set(Calendar.YEAR, year);
                mDate.set(Calendar.MONTH, month);
                mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
                mDate.set(Calendar.MINUTE, minute);
                updateTitle(mDate.getTimeInMillis()); //更新对话框标题显示的日期
            }
        });
        mDate.setTimeInMillis(date); // 将对话框初始化为传入的日期和时间值
        mDate.set(Calendar.SECOND, 0); // 秒字段设为0
        mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 将DateTimePicker初始化到当前日期
        //设置对话框的'确定'和'取消'按钮
        setButton(context.getString(R.string.datetime_dialog_ok), this);
        setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
        //设置是否使用24小时视图
        set24HourView(DateFormat.is24HourFormat(this.getContext()));
        updateTitle(mDate.getTimeInMillis()); // 初始对话框标题显示的日期和时间
    }

    //设置时间是否显示为24小时视图
    public void set24HourView(boolean is24HourView) {
        mIs24HourView = is24HourView;
    }

    //设置DateTimeSet监听回调
    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
        mOnDateTimeSetListener = callBack;
    }

    //更新对话框标题显示的日期和时间
    private void updateTitle(long date) {
        int flag =
            DateUtils.FORMAT_SHOW_YEAR |
            DateUtils.FORMAT_SHOW_DATE |
            DateUtils.FORMAT_SHOW_TIME;
        flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
        setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); // 格式化时间并设置标题
    }

    //点击对话框按钮调用，通知监听者时间已设置
    public void onClick(DialogInterface arg0, int arg1) {
        if (mOnDateTimeSetListener != null) {
            mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); // 激活回调传递当前设置的日期和时间
        }
    }

}