package com.qhclh.ytzh.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.qhclh.ytzh.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by luanhui on 2016/7/5.
 */
public class CalendarDialogFragment extends DialogFragment implements OnDateSelectedListener {

//    private TextView textView;
    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();


    public interface CalendarClickListener
    {
        void onStartCalendarClick(Date date);
        void onEndCalendarClick(Date date);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //去掉标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.calendar_dialog_basic, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        textView = (TextView) view.findViewById(R.id.textView);

        MaterialCalendarView widget = (MaterialCalendarView) view.findViewById(R.id.calendarView);

        widget.setOnDateChangedListener(this);
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
//        textView.setText(FORMATTER.format(date.getDate()));

        CalendarClickListener listener = (CalendarClickListener) getActivity();
        String tag=this.getTag();
        if(tag.equals("end"))
        {
            listener.onEndCalendarClick(date.getDate());
        }
        else{
            listener.onStartCalendarClick(date.getDate());
        }

        this.dismiss();
    }
}
