/*
 * Copyright (C) 2009 University of Washington
 *
 * 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 org.odk.collect.android.widgets;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.text.format.DateUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TableLayout;
import android.widget.TextView;

import org.javarosa.core.model.data.DateData;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.form.api.FormEntryPrompt;
import org.joda.time.DateTime;
import org.odk.collect.android.R;

import java.util.Calendar;
import java.util.Date;

/**
 * Displays a DatePicker widget. DateWidget handles leap years and does not allow dates that do not exist.
 *
 * @author Carl Hartung (carlhartung@gmail.com)
 * @author Yaw Anokwa (yanokwa@gmail.com)
 */
public class DateWidget extends QuestionWidget {

  private static final int DATE_UNSET = -1;
  private TextView datePickerField;
  private long date;


  @SuppressLint("NewApi")
  public DateWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    date = DATE_UNSET;

    datePickerField = (TextView) LayoutInflater.from(context).inflate(R.layout.date_picker_field, this, false);
    datePickerField.setId(QuestionWidget.newUniqueId());
    final boolean readOnly = prompt.isReadOnly();
    datePickerField.setEnabled(!readOnly);
    datePickerField.setFocusable(false);
    datePickerField.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.answer_text));
    if (readOnly) {
      datePickerField.setBackgroundDrawable(null);
    }
    datePickerField.setClickable(false);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      datePickerField.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      datePickerField.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
    }

    TableLayout.LayoutParams params = new TableLayout.LayoutParams();

    params.setMargins(getResources().getDimensionPixelSize(R.dimen.edittext_padding_start), 0, 0, 0);
    datePickerField.setPadding(datePickerField.getPaddingLeft(), -1, datePickerField.getPaddingRight(),
        datePickerField.getPaddingBottom());
    datePickerField.setLayoutParams(params);
    datePickerField.setSingleLine(true);

    datePickerField.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(final View v) {
        showDatePickerDialog();
      }
    });

    setGravity(Gravity.START);
    addView(datePickerField);

    // If there's an answer, use it.
    setAnswer();
  }

  private void setAnswer() {

    if (mPrompt.getAnswerValue() != null) {
      DateTime ldt = new DateTime(((Date) mPrompt.getAnswerValue().getValue()).getTime());
      final long time = ldt.toDate().getTime();
      date = time;
      formatDate(time);
    } else {
      // create date widget with current time as of right now
      clearAnswer();
    }
  }

  private void formatDate(final long date) {
    final int flags = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
    final String formattedDate = DateUtils.formatDateTime(getContext(), date, flags);
    datePickerField.setText(formattedDate);
  }


  /**
   * Resets date to today.
   */
  @Override
  public void clearAnswer() {
    date = DATE_UNSET;
    datePickerField.setText("");
  }


  @Override
  public IAnswerData getAnswer() {
    clearFocus();
    return new DateData(new Date(date));
  }


  @Override
  public void setFocus(Context context) {
    // Hide the soft keyboard if it's showing.
    InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getWindowToken(), 0);
  }


  @Override
  public void setOnLongClickListener(OnLongClickListener l) {
    datePickerField.setOnLongClickListener(l);
  }


  @Override
  public void cancelLongPress() {
    super.cancelLongPress();
    datePickerField.cancelLongPress();
  }

  private void showDatePickerDialog() {
    final Calendar now = Calendar.getInstance();
    final Calendar defaultDate = Calendar.getInstance();

    if (date != DATE_UNSET) {
      defaultDate.setTimeInMillis(date);
    } else {
      defaultDate.setTime(now.getTime());
    }

    final int yearNow = defaultDate.get(Calendar.YEAR);
    final int monthNow = defaultDate.get(Calendar.MONTH); // Note: zero based!
    final int dayNow = defaultDate.get(Calendar.DAY_OF_MONTH);

    final DatePickerDialog dialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

      // This event is fired when the date is set on the
// date picker.
      @Override
      public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
        final Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        calendar.set(Calendar.MONTH, monthOfYear);
        calendar.set(Calendar.YEAR, year);

        date = calendar.getTimeInMillis();
        final int flags = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
        final String formattedDate = DateUtils.formatDateTime(getContext(), calendar.getTime().getTime(), flags);
        datePickerField.setText(formattedDate);
      }
    }, yearNow, monthNow, dayNow);

    dialog.setButton(DialogInterface.BUTTON_NEUTRAL, getContext().getString(R.string.date_picker_clear),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(final DialogInterface dialog, final int which) {
            if (which == DialogInterface.BUTTON_NEUTRAL) {
              datePickerField.setText("");
              date = DATE_UNSET;
            }
          }
        });

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
      final CalendarView cal = dialog.getDatePicker().getCalendarView();
      if (cal != null) {
        final long current = cal.getDate();
        cal.setDate(cal.getMaxDate(), false, true);
        cal.setDate(current, false, true);
      }
    }

    dialog.show();
  }
}
