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


/**
 * @projectName（项目名称）: xiaomi label
 * @package（包）: ui
 * @className（类名称）: DateTimePickerDialog
 * @description（类描述）: 日期时间选择器对话框，继承于AlertDialog，实现了OnClickListener接口
 * @author（创建人）: zhangchaoqun
 * @createDate（创建时间）: datetime
 * @updateUser（修改人）: user
 * @updateDate（修改时间）: datetime
 * @updateRemark（修改备注）: 说明本次修改内容
 * @version（版本）: v1.0
 */
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {

    //创建一个Calendar类型的变量mDate方便时间操作
    private Calendar mDate = Calendar.getInstance();
    //是否是24小时视图
    private boolean mIs24HourView;
    //声明一个日期设置监听器
    private OnDateTimeSetListener mOnDateTimeSetListener;
    //DateTimePicker控件一般用于让用户可以从日期列表中选择单个值
    private DateTimePicker mDateTimePicker;

    //接口：日期时间设置监听器
    public interface OnDateTimeSetListener {
        void OnDateTimeSet(AlertDialog dialog, long date);
    }


    /**
     * @description 描述:实例化日期时间选择对话框
     * @param 参数1:context
     * @param 参数2:date
     * @param 参数3:
     * @return 返回值:创建一个DateTimePickerDialog的实例对象
     * @author zhangchaoqun
     */
    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.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);
        mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
        setButton(context.getString(R.string.datetime_dialog_ok), this);
        setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
        set24HourView(DateFormat.is24HourFormat(this.getContext()));
        updateTitle(mDate.getTimeInMillis());
    }


    /**
     * @description 描述:设置成24小时制
     * @param 参数1:is24HourView
     * @param 参数2:
     * @param 参数3:
     * @return 返回值:void
     * @author zhangchaoqun
     */
    public void set24HourView(boolean is24HourView) {
        mIs24HourView = is24HourView;
    }


    /**
     * @description 描述:设置日期时间监听器
     * @param 参数1:callBack
     * @param 参数2:
     * @param 参数3:
     * @return 返回值:void
     * @author zhangchaoqun
     */
    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
        mOnDateTimeSetListener = callBack;
    }


    /**
     * @description 描述:更新安卓开发中常见的日期管理工具类 DateUtils
     * @param 参数1:date
     * @param 参数2:
     * @param 参数3:
     * @return 返回值:void
     * @author zhangchaoqun
     */
    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));
    }


    /**
     * @description 描述:处理点击事件
     * @param 参数1:arg0（接收到点击事件的对话框）
     * @param 参数2:arg1（arg0对话框上的按钮）
     * @param 参数3:
     * @return 返回值:
     * @author zhangchaoqun
     */
    public void onClick(DialogInterface arg0, int arg1) {
        if (mOnDateTimeSetListener != null) {
            mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
        }
    }

}