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

public class DateTimePickerDialog extends AlertDialog implements OnClickListener {

    private Calendar mDate = Calendar.getInstance();
    private boolean mIs24HourView;
    private OnDateTimeSetListener mOnDateTimeSetListener;
    private DateTimePicker mDateTimePicker;

    public interface OnDateTimeSetListener {
        void OnDateTimeSet(AlertDialog dialog, long date);
    }
    
    public DateTimePickerDialog(Context context, long date) {
        super(context);
        mDateTimePicker = new DateTimePicker(context);
        setView(mDateTimePicker);
        
        // 设置日期时间选择器的监听器，以在日期时间变化时更新标题和数据
        mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
            public void onDateTimeChanged(DateTimePicker view, int year, int month,
                    int dayOfMonth, int hourOfDay, int minute) {
                // 更新内部日期对象 mDate 的值
                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，并将其秒数设置为 0
        mDate.setTimeInMillis(date);
        mDate.set(Calendar.SECOND, 0);
        // 设置日期时间选择器的当前日期和时间
        mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
        
        // 设置对话框的“确定”按钮文本和点击监听器
        setButton(context.getString(R.string.datetime_dialog_ok), this);
        // 设置对话框的“取消”按钮文本，并将其点击监听器设置为 null
        setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
        
        // 设置是否使用24小时制时间显示
        set24HourView(DateFormat.is24HourFormat(this.getContext()));
        
        // 更新对话框的标题
        updateTitle(mDate.getTimeInMillis());
    }
    
    public void set24HourView(boolean is24HourView) {
        // 设置是否使用24小时制时间显示
        mIs24HourView = is24HourView;
    }
    
    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;
        // 根据是否使用24小时制时间显示来更新标志
        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());
        }
    }
    
}