package de.tudarmstadt.informatik.hostage.ui2.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;

@SuppressLint("ValidFragment")
public class DateTimeDialogFragment extends DialogFragment implements OnDateChangedListener, OnTimeChangedListener {
    // Define constants for date-time picker.
    public final static int DATE_PICKER = 1;
    public final static int TIME_PICKER = 2;
    public final static int DATE_TIME_PICKER = 3;

    // DatePicker reference
    public DatePicker datePicker;

    // TimePicker reference
    public TimePicker timePicker;

    // Calendar reference
    private Calendar mCalendar;

    // Define activity
    private Activity activity;

    // Define Dialog type
    private int DialogType;

    // Define Dialog view
    private View mView;

    // Constructor start
    public DateTimeDialogFragment(Activity activity) {
        this(activity, DATE_TIME_PICKER);
    }

    public DateTimeDialogFragment(Activity activity, int DialogType) {
        this.activity = activity;
        this.DialogType = DialogType;

        // Inflate layout for the view
        // Pass null as the parent view because its going in the dialog layout
        LayoutInflater inflater = activity.getLayoutInflater();
        mView = inflater.inflate(R.layout.date_time_dialog, null);  

        // Grab a Calendar instance
        mCalendar = Calendar.getInstance();

        // Init date picker
        datePicker = (DatePicker) mView.findViewById(R.id.DatePicker);
        datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);

        // Init time picker
        timePicker = (TimePicker) mView.findViewById(R.id.TimePicker);

        // Set default Calendar and Time Style
        setIs24HourView(true);
        setCalendarViewShown(false);

        switch (DialogType) {
        case DATE_PICKER:
            timePicker.setVisibility(View.GONE);
            break;
        case TIME_PICKER:
            datePicker.setVisibility(View.GONE);
            break;
        }
    }

    public void setDate(long timeInMillis){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis (timeInMillis);

        int year    = calendar.get(Calendar.YEAR) ;
        int month   = calendar.get(Calendar.MONTH);
        int day     = calendar.get(Calendar.DATE);
        int hour    = calendar.get(Calendar.HOUR);
        int min     = calendar.get(Calendar.MINUTE);

        datePicker.updateDate(year, month, day);
        timePicker.setCurrentHour(hour);
        timePicker.setCurrentMinute(min);
    }

   public long getDate(){
	   
       int day = datePicker.getDayOfMonth();
       int month = datePicker.getMonth();
       int year =  datePicker.getYear();
       
       int hourOfDay = timePicker.getCurrentHour();
       int minute = timePicker.getCurrentMinute();

       Calendar calendar = Calendar.getInstance();

       calendar.set(year, month, day, hourOfDay, minute);

       return calendar.getTime().getTime();
   }
    
    public interface DateTimeDialogFragmentListener {
        public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog);
        public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog);
    }
    private DateTimeDialogFragmentListener mListener;

    public void setDateChangeListener(DateTimeDialogFragmentListener listener){
        this.mListener = listener;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            if (this.mListener == null){
                if (activity.getClass().equals(MainActivity.class)){
                    mListener = (DateTimeDialogFragmentListener) (((MainActivity)activity).getDisplayedFragment());
                } else {
                    mListener = (DateTimeDialogFragmentListener) activity;
                }
            }
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement DateTimeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Builder builder = new AlertDialog.Builder(activity);

        builder.setView(mView);

        builder.setMessage("Set Date")
                .setPositiveButton("Set",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mListener.onDateTimePickerPositiveClick(DateTimeDialogFragment.this);
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mListener.onDateTimePickerNegativeClick(DateTimeDialogFragment.this);
                                DateTimeDialogFragment.this.getDialog().cancel();
                            }
                        }); 

        return builder.create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        timePicker.setOnTimeChangedListener(this);
    }

    public int get(final int field) {
        return mCalendar.get(field);
    }

    public long getDateTimeMillis() {
        return mCalendar.getTimeInMillis();
    }

    public void setIs24HourView(boolean is24HourView) {
        timePicker.setIs24HourView(is24HourView);
    }

    public boolean is24HourView() {
        return timePicker.is24HourView();
    }

    public void setCalendarViewShown(boolean calendarView) {
        datePicker.setCalendarViewShown(calendarView);
    }

    public boolean CalendarViewShown() {
        return datePicker.getCalendarViewShown();
    }

    public void updateDate(int year, int monthOfYear, int dayOfMonth) {
        datePicker.updateDate(year, monthOfYear, dayOfMonth);
    }

    public void updateTime(int currentHour, int currentMinute) {
        timePicker.setCurrentHour(currentHour);
        timePicker.setCurrentMinute(currentMinute);
    }

    public String getDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        return sdf.format(mCalendar.getTime());
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mCalendar.set(year, monthOfYear, dayOfMonth, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
    }

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
    }
}