<?php
namespace Dreamfox\DeliveryDate\Premium\Views;

use Dreamfox\DeliveryDate\Helpers\DateHelper;
use Dreamfox\DeliveryDate\Interfaces\ApplicationInterface;
use Dreamfox\DeliveryDate\Premium\Facades\SystemFacade;

class DeliveryDateView extends PremiumView
{

  public function __construct(ApplicationInterface $app)
  {
    parent::__construct($app);
    $this->setTemplate('delivery_date');
  }

  public function getDescription()
  {
    $setting = SystemFacade::getInstance()->loadOtherSettings();
    return $setting->getDescriptionOnCheckout();
  }

  public function getMinDate()
  {
    $currentDate = DateHelper::toJsDateObject(date('Y'), date('m'), date('d'));
    return $currentDate;
  }

  public function getHolidays()
  {
    $setting = SystemFacade::getInstance()->loadHolidaySettings();
    $pairDays = $setting->getHolidays();
    $result = [];
    foreach ($pairDays as $pairDay) {
      $days = $this->_getDayList($pairDay);
      $result = array_merge($result, $days);
    }
    return $result;
  }

  private function _getDayList($pairDay)
  {
    $result = [];
    $startDate = $pairDay->startDate;
    $endDate = $pairDay->endDate;
    if (strlen($startDate)>=8 && strlen($endDate)>=8) {
      $startDate = DateHelper::toDateYmd($pairDay->startDate);
      $endDate = DateHelper::toDateYmd($pairDay->endDate);
      $result = DateHelper::createDateRange($startDate, $endDate, '+1 day');
    }
    return $result;
  }

  public function getDisableDays()
  {
    $setting = SystemFacade::getInstance()->loadHolidaySettings();
    $days = $setting->getDisableDays();
    $result = [];
    foreach ($days as $day) {
      if ($day->checked) {
        $result[] =$day->id;
      }
    }
    return $result;
  }
}