<?php

namespace Drupal\restaurant\Service;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* calendar methods
*/
class Calendar implements HttpKernelInterface
{

  protected $conn;
  
  protected $httpKernel;
  
  /**
   * 
   * @var int
   */
  private $year;
  
  /**
   * 
   * @var int
   */
  private $month;
  
  /**
   * 
   * @var int
   */
  private $startDay;
  
  /**
   * Days in $this->month 
   * @var int
   */
  private $daysInMonth;
  
  
  public function __construct(HttpKernelInterface $kernel)
  {
      $this->conn = \Drupal::database();
      $this->httpKernel = $kernel;
      
      $this->year = date('Y');
      $this->month = date('m');
      $this->setStartDay();
  }
  
  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
  {
      return $this->httpKernel->handle($request, $type, true);
  }
  
  public function setYear($year) 
  {
      $this->year = $year;
      $this->setStartDay();
      return $this;
  }
  
  public function setMonth($month)
  {
      $this->month = $month;
      $this->setStartDay();
      return $this;
  }
  
  private function setStartDay()
  {
     $this->startDay = date('w', strtotime($this->year . '-' . $this->month . '-01'));
     $this->daysInMonth = date('t', strtotime($this->year . '-' . $this->month . '-01'));
  }
  
  /**
   * Return the html for a calendar
   * @return string
   */  
  public function getCalendar()
  {
      $offset = range(1, $this->startDay);
      
      $days = range(1, $this->daysInMonth);
      
      $dates = array_merge($offset, $days);
      
      foreach ($dates as $key => $val) {
          $str .= '<div';
          $classes = array('calendar-div');
          if ($key < sizeof ($offset)) { $classes[] = 'hide-div'; }
          $str .= ' class="';
          foreach ($classes as $v) {
              //$str .= ' ' . $v;
              if ($key < sizeof ($offset)) { $str .= ' '; }
              $str .= $v;
          }
          
          $str .= '">';
          $str .= $val . '</div>' . "\n";
      } 
    return $str;
  }


  

  
}
