/*
 * 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;//这个类包含在 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 {//日期时间选择器对话框，继承于AlertDialog类，实现了OnClickListener接口

    private Calendar mDate = Calendar.getInstance();//创建一个Calendar类型的变量 mDate，方便时间的操作
    private boolean mIs24HourView;//判断当前设置是否为24小时制
    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) {//将时间设置为系统时间,分别设置年、月、日、小时、分钟信息，并更新title时间
                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());//设置当前日期
        setButton(context.getString(R.string.datetime_dialog_ok), this);//设置ok按钮
        setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);//设置cancel按钮
        set24HourView(DateFormat.is24HourFormat(this.getContext()));设置为24小时制显示
        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;//通过DataUtils按照24时制显示
        flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;/判断是否为24小时
        setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));//设置标题
    }

    public void onClick(DialogInterface arg0, int arg1) {//当用户点击的时候进行时间的设置
        if (mOnDateTimeSetListener != null) {
            mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());//设置日期时间
        }
    }

}