/**
   * Sets the minimal date supported by this DatePicker. Dates after (but not including) the
   * specified date will be disallowed from being selected.
   *
   * @param calendar a Calendar object set to the year, month, day desired as the maxdate.
   */
  @SuppressWarnings("unused")
  public void setMaxDate(Calendar calendar) {
    mMaxDate = calendar;

    if (mDayPickerView != null && mDayPickerViewEnd != null) {
      mDayPickerView.onChange();
      mDayPickerViewEnd.onChange();
    }
  }
  @SuppressWarnings("unused")
  public void setYearRange(int startYear, int endYear) {
    if (endYear < startYear) {
      throw new IllegalArgumentException("Year end must be larger than or equal to year start");
    }

    mMinYear = startYear;
    mMaxYear = endYear;
    if (mDayPickerView != null && mDayPickerViewEnd != null) {
      mDayPickerView.onChange();
      mDayPickerViewEnd.onChange();
    }
  }
  @SuppressWarnings("unused")
  public void setFirstDayOfWeek(int startOfWeek, int startWeekEnd) {
    if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) {
      throw new IllegalArgumentException(
          "Value must be between Calendar.SUNDAY and " + "Calendar.SATURDAY");
    }
    mWeekStart = startOfWeek;
    mWeekStartEnd = startWeekEnd;

    if (mDayPickerView != null) {
      mDayPickerView.onChange();
    }

    if (mDayPickerViewEnd != null) {
      mDayPickerViewEnd.onChange();
    }
  }
  @Override
  public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(KEY_SELECTED_YEAR, mCalendar.get(Calendar.YEAR));
    outState.putInt(KEY_SELECTED_MONTH, mCalendar.get(Calendar.MONTH));
    outState.putInt(KEY_SELECTED_DAY, mCalendar.get(Calendar.DAY_OF_MONTH));
    outState.putInt(KEY_WEEK_START, mWeekStart);
    outState.putInt(KEY_YEAR_START, mMinYear);
    outState.putInt(KEY_YEAR_END, mMaxYear);
    outState.putInt(KEY_CURRENT_VIEW, mCurrentView);
    outState.putInt(KEY_SELECTED_YEAR_END, mCalendarEnd.get(Calendar.YEAR));
    outState.putInt(KEY_SELECTED_MONTH_END, mCalendarEnd.get(Calendar.MONTH));
    outState.putInt(KEY_SELECTED_DAY_END, mCalendarEnd.get(Calendar.DAY_OF_MONTH));
    outState.putInt(KEY_WEEK_START_END, mWeekStartEnd);
    outState.putInt(KEY_YEAR_START_END, mMinYear);
    outState.putInt(KEY_YEAR_END_END, mMaxYear);
    outState.putInt(KEY_CURRENT_VIEW_END, mCurrentViewEnd);
    int listPosition = -1;
    int listPositionEnd = -1;
    if (mCurrentView == MONTH_AND_DAY_VIEW || mCurrentViewEnd == MONTH_AND_DAY_VIEW) {
      listPosition = mDayPickerView.getMostVisiblePosition();
      listPositionEnd = mDayPickerViewEnd.getMostVisiblePosition();

    } else if (mCurrentView == YEAR_VIEW || mCurrentViewEnd == YEAR_VIEW) {
      listPosition = mYearPickerView.getFirstVisiblePosition();
      listPositionEnd = mYearPickerViewEnd.getFirstVisiblePosition();
      outState.putInt(KEY_LIST_POSITION_OFFSET, mYearPickerView.getFirstPositionOffset());
      outState.putInt(KEY_LIST_POSITION_OFFSET_END, mYearPickerViewEnd.getFirstPositionOffset());
    }
    outState.putInt(KEY_LIST_POSITION, listPosition);
    outState.putInt(KEY_LIST_POSITION_END, listPositionEnd);
    outState.putSerializable(KEY_MIN_DATE, mMinDate);
    outState.putSerializable(KEY_MAX_DATE, mMaxDate);
    outState.putSerializable(KEY_MIN_DATE_END, mMinDateEnd);
    outState.putSerializable(KEY_MAX_DATE_END, mMaxDateEnd);
    outState.putSerializable(KEY_HIGHLIGHTED_DAYS, highlightedDays);
    outState.putSerializable(KEY_SELECTABLE_DAYS, selectableDays);
    outState.putSerializable(KEY_HIGHLIGHTED_DAYS_END, highlightedDaysEnd);
    outState.putSerializable(KEY_SELECTABLE_DAYS_END, selectableDaysEnd);
    outState.putBoolean(KEY_THEME_DARK, mThemeDark);
    outState.putInt(KEY_ACCENT, mAccentColor);
    outState.putBoolean(KEY_VIBRATE, mVibrate);
    outState.putBoolean(KEY_DISMISS, mDismissOnPause);
  }
  private void setCurrentView(final int viewIndex) {
    long millis = mCalendar.getTimeInMillis();
    long millisEnd = mCalendarEnd.getTimeInMillis();

    switch (viewIndex) {
      case MONTH_AND_DAY_VIEW:
        ObjectAnimator pulseAnimator = Utils.getPulseAnimator(mMonthAndDayView, 0.9f, 1.05f);
        ObjectAnimator pulseAnimatorTwo = Utils.getPulseAnimator(mMonthAndDayViewEnd, 0.9f, 1.05f);
        if (mDelayAnimation) {
          pulseAnimator.setStartDelay(ANIMATION_DELAY);
          pulseAnimatorTwo.setStartDelay(ANIMATION_DELAY);
          mDelayAnimation = false;
        }
        mDayPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
          mMonthAndDayView.setSelected(true);
          mMonthAndDayViewEnd.setSelected(true);
          mYearView.setSelected(false);
          mYearViewEnd.setSelected(false);
          mAnimator.setDisplayedChild(MONTH_AND_DAY_VIEW);
          mAnimatorEnd.setDisplayedChild(MONTH_AND_DAY_VIEW);
          mCurrentView = viewIndex;
        }
        pulseAnimator.start();
        pulseAnimatorTwo.start();

        int flags = DateUtils.FORMAT_SHOW_DATE;
        String dayString = DateUtils.formatDateTime(getActivity(), millis, flags);
        String dayStringEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags);
        mAnimator.setContentDescription(mDayPickerDescription + ": " + dayString);
        mAnimatorEnd.setContentDescription(mDayPickerDescription + ": " + dayStringEnd);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay);
        Utils.tryAccessibilityAnnounce(mAnimatorEnd, mSelectDay);
        break;
      case YEAR_VIEW:
        pulseAnimator = Utils.getPulseAnimator(mYearView, 0.85f, 1.1f);
        pulseAnimatorTwo = Utils.getPulseAnimator(mYearViewEnd, 0.85f, 1.1f);
        if (mDelayAnimation) {
          pulseAnimator.setStartDelay(ANIMATION_DELAY);
          pulseAnimatorTwo.setStartDelay(ANIMATION_DELAY);
          mDelayAnimation = false;
        }
        mYearPickerView.onDateChanged();
        mYearPickerViewEnd.onDateChanged();
        if (mCurrentView != viewIndex) {
          mMonthAndDayView.setSelected(false);
          mYearView.setSelected(true);
          mAnimator.setDisplayedChild(YEAR_VIEW);
          mCurrentView = viewIndex;

          mMonthAndDayViewEnd.setSelected(false);
          mYearViewEnd.setSelected(true);
          mAnimatorEnd.setDisplayedChild(YEAR_VIEW);
          mCurrentViewEnd = viewIndex;
        }
        pulseAnimator.start();
        pulseAnimatorTwo.start();

        CharSequence yearString = YEAR_FORMAT.format(millis);
        CharSequence yearStringEnd = YEAR_FORMAT.format(millisEnd);
        mAnimator.setContentDescription(mYearPickerDescription + ": " + yearString);
        mAnimatorEnd.setContentDescription(mYearPickerDescription + ": " + yearStringEnd);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear);
        Utils.tryAccessibilityAnnounce(mAnimatorEnd, mSelectYear);
        break;
    }
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView: ");
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    View view = inflater.inflate(R.layout.mdtp_date_picker_dialog, null);

    tabHost = (TabHost) view.findViewById(R.id.tabHost);
    tabHost.findViewById(R.id.tabHost);
    tabHost.setup();
    TabHost.TabSpec startDatePage = tabHost.newTabSpec("start");
    startDatePage.setContent(R.id.start_date_group);
    startDatePage.setIndicator("FROM");

    TabHost.TabSpec endDatePage = tabHost.newTabSpec("end");
    endDatePage.setContent(R.id.end_date_group);
    endDatePage.setIndicator("TO");

    tabHost.addTab(startDatePage);
    tabHost.addTab(endDatePage);

    mDayOfWeekView = (TextView) view.findViewById(R.id.date_picker_header);
    mMonthAndDayView = (LinearLayout) view.findViewById(R.id.date_picker_month_and_day);
    mMonthAndDayViewEnd = (LinearLayout) view.findViewById(R.id.date_picker_month_and_day_end);
    mMonthAndDayView.setOnClickListener(this);
    mMonthAndDayViewEnd.setOnClickListener(this);

    mSelectedMonthTextView = (TextView) view.findViewById(R.id.date_picker_month);
    mSelectedMonthTextViewEnd = (TextView) view.findViewById(R.id.date_picker_month_end);

    mSelectedDayTextView = (TextView) view.findViewById(R.id.date_picker_day);
    mSelectedDayTextViewEnd = (TextView) view.findViewById(R.id.date_picker_day_end);

    mYearView = (TextView) view.findViewById(R.id.date_picker_year);
    mYearViewEnd = (TextView) view.findViewById(R.id.date_picker_year_end);
    mYearView.setOnClickListener(this);
    mYearViewEnd.setOnClickListener(this);

    int listPosition = -1;
    int listPositionOffset = 0;
    int listPositionEnd = -1;
    int listPositionOffsetEnd = 0;
    int currentView = MONTH_AND_DAY_VIEW;
    int currentViewEnd = MONTH_AND_DAY_VIEW;
    if (savedInstanceState != null) {
      mWeekStart = savedInstanceState.getInt(KEY_WEEK_START);
      mWeekStartEnd = savedInstanceState.getInt(KEY_WEEK_START_END);
      mMinYear = savedInstanceState.getInt(KEY_YEAR_START);
      mMaxYear = savedInstanceState.getInt(KEY_YEAR_END);
      currentView = savedInstanceState.getInt(KEY_CURRENT_VIEW);
      currentViewEnd = savedInstanceState.getInt(KEY_CURRENT_VIEW_END);
      listPosition = savedInstanceState.getInt(KEY_LIST_POSITION);
      listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET);
      listPositionEnd = savedInstanceState.getInt(KEY_LIST_POSITION_END);
      listPositionOffsetEnd = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET_END);
      mMinDate = (Calendar) savedInstanceState.getSerializable(KEY_MIN_DATE);
      mMaxDate = (Calendar) savedInstanceState.getSerializable(KEY_MAX_DATE);
      mMinDateEnd = (Calendar) savedInstanceState.getSerializable(KEY_MIN_DATE_END);
      mMaxDateEnd = (Calendar) savedInstanceState.getSerializable(KEY_MAX_DATE_END);
      highlightedDays = (Calendar[]) savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS);
      selectableDays = (Calendar[]) savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS);
      highlightedDaysEnd =
          (Calendar[]) savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS_END);
      selectableDaysEnd = (Calendar[]) savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS_END);
      mThemeDark = savedInstanceState.getBoolean(KEY_THEME_DARK);
      mAccentColor = savedInstanceState.getInt(KEY_ACCENT);
      mVibrate = savedInstanceState.getBoolean(KEY_VIBRATE);
      mDismissOnPause = savedInstanceState.getBoolean(KEY_DISMISS);
    }

    final Activity activity = getActivity();
    mDayPickerView =
        new com.borax12.materialdaterangepicker.date.SimpleDayPickerView(activity, this);
    mYearPickerView = new com.borax12.materialdaterangepicker.date.YearPickerView(activity, this);
    mDayPickerViewEnd =
        new com.borax12.materialdaterangepicker.date.SimpleDayPickerView(activity, this);
    mYearPickerViewEnd =
        new com.borax12.materialdaterangepicker.date.YearPickerView(activity, this);

    Resources res = getResources();
    mDayPickerDescription = res.getString(R.string.mdtp_day_picker_description);
    mSelectDay = res.getString(R.string.mdtp_select_day);
    mYearPickerDescription = res.getString(R.string.mdtp_year_picker_description);
    mSelectYear = res.getString(R.string.mdtp_select_year);

    int bgColorResource =
        mThemeDark
            ? R.color.mdtp_date_picker_view_animator_dark_theme
            : R.color.mdtp_date_picker_view_animator;
    view.setBackgroundColor(activity.getResources().getColor(bgColorResource));

    mAnimator =
        (com.borax12.materialdaterangepicker.date.AccessibleDateAnimator)
            view.findViewById(R.id.animator);
    mAnimatorEnd =
        (com.borax12.materialdaterangepicker.date.AccessibleDateAnimator)
            view.findViewById(R.id.animator_end);

    mAnimator.addView(mDayPickerView);
    mAnimator.addView(mYearPickerView);
    mAnimator.setDateMillis(mCalendar.getTimeInMillis());
    // TODO: Replace with animation decided upon by the design team.
    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(ANIMATION_DURATION);
    mAnimator.setInAnimation(animation);
    // TODO: Replace with animation decided upon by the design team.
    Animation animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(ANIMATION_DURATION);
    mAnimator.setOutAnimation(animation2);

    mAnimatorEnd.addView(mDayPickerViewEnd);
    mAnimatorEnd.addView(mYearPickerViewEnd);
    mAnimatorEnd.setDateMillis(mCalendarEnd.getTimeInMillis());
    // TODO: Replace with animation decided upon by the design team.
    Animation animationEnd = new AlphaAnimation(0.0f, 1.0f);
    animationEnd.setDuration(ANIMATION_DURATION);
    mAnimatorEnd.setInAnimation(animation);
    // TODO: Replace with animation decided upon by the design team.
    Animation animation2End = new AlphaAnimation(1.0f, 0.0f);
    animation2End.setDuration(ANIMATION_DURATION);
    mAnimatorEnd.setOutAnimation(animation2);

    Button okButton = (Button) view.findViewById(R.id.ok);
    okButton.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            tryVibrate();
            if (mCallBack != null) {
              mCallBack.onDateSet(
                  DatePickerDialog.this,
                  mCalendar.get(Calendar.YEAR),
                  mCalendar.get(Calendar.MONTH),
                  mCalendar.get(Calendar.DAY_OF_MONTH),
                  mCalendarEnd.get(Calendar.YEAR),
                  mCalendarEnd.get(Calendar.MONTH),
                  mCalendarEnd.get(Calendar.DAY_OF_MONTH));
            }
            dismiss();
          }
        });
    okButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium"));

    Button cancelButton = (Button) view.findViewById(R.id.cancel);
    cancelButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            tryVibrate();
            if (getDialog() != null) getDialog().cancel();
          }
        });
    cancelButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium"));
    cancelButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE);

    // If an accent color has not been set manually, try and get it from the context
    if (mAccentColor == -1) {
      int accentColor = Utils.getAccentColorFromThemeIfAvailable(getActivity());
      if (accentColor != -1) {
        mAccentColor = accentColor;
      }
    }
    if (mAccentColor != -1) {
      if (mDayOfWeekView != null)
        mDayOfWeekView.setBackgroundColor(Utils.darkenColor(mAccentColor));
      view.findViewById(R.id.day_picker_selected_date_layout).setBackgroundColor(mAccentColor);
      view.findViewById(R.id.day_picker_selected_date_layout_end).setBackgroundColor(mAccentColor);
      okButton.setTextColor(mAccentColor);
      cancelButton.setTextColor(mAccentColor);
      mYearPickerView.setAccentColor(mAccentColor);
      mDayPickerView.setAccentColor(mAccentColor);
      mYearPickerViewEnd.setAccentColor(mAccentColor);
      mDayPickerViewEnd.setAccentColor(mAccentColor);
    }

    updateDisplay(false);
    setCurrentView(currentView);

    if (listPosition != -1) {
      if (currentView == MONTH_AND_DAY_VIEW) {
        mDayPickerView.postSetSelection(listPosition);
      } else if (currentView == YEAR_VIEW) {
        mYearPickerView.postSetSelectionFromTop(listPosition, listPositionOffset);
      }
    }

    if (listPositionEnd != -1) {
      if (currentViewEnd == MONTH_AND_DAY_VIEW) {
        mDayPickerViewEnd.postSetSelection(listPositionEnd);
      } else if (currentViewEnd == YEAR_VIEW) {
        mYearPickerViewEnd.postSetSelectionFromTop(listPositionEnd, listPositionOffsetEnd);
      }
    }

    mHapticFeedbackController = new HapticFeedbackController(activity);

    tabHost.setOnTabChangedListener(
        new TabHost.OnTabChangeListener() {
          @Override
          public void onTabChanged(String tabId) {
            com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay calendarDay;
            if (tabId == "start") {
              calendarDay =
                  new com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay(
                      mCalendar.getTimeInMillis());
              mDayPickerView.goTo(calendarDay, true, true, false);
            } else {
              calendarDay =
                  new com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay(
                      mCalendarEnd.getTimeInMillis());
              mDayPickerViewEnd.goTo(calendarDay, true, true, false);
            }
          }
        });
    return view;
  }