/*
 * 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:    DateTime
  * @Package:        net.micode.notes.ui
  * @ClassName:      DateTimePickerDialog
  * @Description:    初始化对时间日期操作的变量
  * @Author:         wyf
  * @CreateDate:     2023/12/24 20:02
 */
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {

    private Calendar mDate = Calendar.getInstance();//创建一个Calendar类型的变量 mDate，方便时间的操作
    private boolean mIs24HourView;
    private OnDateTimeSetListener mOnDateTimeSetListener;//声明一个时间日期滚动选择控件 mOnDateTimeSetListener
    private DateTimePicker mDateTimePicker; //DateTimePicker控件，控件一般用于让用户可以从日期列表中选择单个值。运行时，单击控件边上的下拉箭头，会显示为两个部分：一个下拉列表，一个用于选择日期的

    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.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);//设置设置按钮
        setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);//设置取消按钮
        set24HourView(DateFormat.is24HourFormat(this.getContext()));//时间标准化打印
        updateTitle(mDate.getTimeInMillis());
    }
     /**
      * @method:     set24HourView
      * @description:实现时间标准化打印
      * @date:       2023/12/24 20:15
      * @author:     wyf
      * @param:      boolean
      * @return:     void
      */
    public void set24HourView(boolean is24HourView) {
        mIs24HourView = is24HourView;
    }
     /**
      * @method:     setOnDateTimeSetListener
      * @description:将时间日期滚动选择控件实例化
      * @date:       2023/12/24 20:14
      * @author:     wyf
      * @param:      interface
      * @return:     void
      */
    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
        mOnDateTimeSetListener = callBack;
    }
     /**
      * @method:     updateTitle
      * @description:更新标题显示的时间日期
      * @date:       2023/12/24 20:15
      * @author:     wyf
      * @param:      long
      * @return:     void
      */
    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));
    }
     /**
      * @method:     onClick
      * @description:点击事件的对话框
      * @date:       2023/12/24 20:17
      * @author:     wyf
      * @param:      interface\int
      * @return:     void
      */
    public void onClick(DialogInterface arg0, int arg1) {
        if (mOnDateTimeSetListener != null) {
            mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
        }
    }

}