var Wick = Wick || {};
Wick.DateSelection = (function(){
  var $self = $('.date-selection'),
    cell = '.calendar__cell_cnt',
    selectedCell = 'calendar__cell_selected',
    isSelected = 'date-selection_selected',
    backToCalendar = '.delivery-time__show-calendar-link',
    isDateSelected = false;

  var selectTimeSlot = function(e){
    $(cell).removeClass(selectedCell);
    $(e.target).addClass(selectedCell);
    $self.addClass(isSelected);
    $self.trigger('selected');
    isDateSelected = true;
  };

  var mobileView = function(){
    var isTimeSlotsVisible = 'date-selection_time-visible';

    $self
    .off('.time')
    .removeClass(isTimeSlotsVisible)
    .on('click.time', cell, function(){
      $self.addClass(isTimeSlotsVisible);
    })
    .on('click.time', backToCalendar, function(){
      $self.removeClass(isTimeSlotsVisible);
    });
  };

  var init = function () {
    if (!$self.length) {
      return;
    }

    $self.on('click', cell, selectTimeSlot);
    new SimpleBar($self.find('.delivery-types__inner')[0]);

    if (Wick.Responsive.getCurrentBreakpoint() === 'down-sm') {
      mobileView();
    }
    Wick.Responsive.onBreakpoint(mobileView, 'down-sm');
  }

  init();

  return {
    isDateSelected: function(){
      return isDateSelected;
    }
  }
})();
