<?php
/**
 * Copyright (C) 2023  Jaap Jansma (jaap.jansma@civicoop.org)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace Civi\DataProcessor\FilterHandler;

use Civi\DataProcessor\DataFlow\SqlDataFlow;
use CRM_Dataprocessor_ExtensionUtil as E;

class AgeFilterHandler extends AbstractFieldFilterHandler {

  /**
   * Initialize the filter
   *
   * @throws \Civi\DataProcessor\Exception\DataSourceNotFoundException
   * @throws \Civi\DataProcessor\Exception\InvalidConfigurationException
   * @throws \Civi\DataProcessor\Exception\FieldNotFoundException
   */
  protected function doInitialization() {
    parent::doInitialization();
    $this->fieldSpecification->type = 'Int';
  }


  /**
   * @param array $filter
   *   The filter settings
   * @return mixed
   */
  public function setFilter($filter) {
    $this->resetFilter();
    $dataFlow  = $this->dataSource->ensureField($this->inputFieldSpecification);
    $tableAlias = $this->getTableAlias($dataFlow);
    if ($dataFlow && $dataFlow instanceof SqlDataFlow && $this->filterCollection->useWhere()) {
      $value = null;
      if (isset($filter['value']) && is_array($filter['value'])) {
        $value = array();
        foreach($filter['value'] as $v) {
          $dateTime = new \DateTime();
          $dateTime->modify($v . ' years ago');
          $value[] = $dateTime->format('Y-m-d');
        }
        asort($value);
      } elseif (isset($filter['value'])) {
        $dateTime = new \DateTime();
        $dateTime->modify($filter['value'] . ' years ago');
        $value = $dateTime->format('Y-m-d');
      }

      $this->whereClause = new SqlDataFlow\SimpleWhereClause($tableAlias, $this->inputFieldSpecification->getName(), $filter['op'], $value, 'String');
      $this->filterCollection->addWhere($this->whereClause);
    }
  }

  protected function getFieldTitle(): string {
    return E::ts('Date Field');
  }

  /**
   * Returns true when this filter has additional configuration
   *
   * @return bool
   */
  public function hasConfiguration() {
    return true;
  }

  /**
   * When this filter type has configuration specify the template file name
   * for the configuration form.
   *
   * @return false|string
   */
  public function getConfigurationTemplateFileName() {
    return "CRM/Dataprocessor/Form/Filter/Configuration/AgeFilter.tpl";
  }

  /**
   * Get a list of possible operator options.
   *
   * @param \Civi\DataProcessor\DataSpecification\FieldSpecification $fieldSpec
   *
   * @return array
   */
  protected function getOperatorOptions(\Civi\DataProcessor\DataSpecification\FieldSpecification $fieldSpec) {
    return parent::getOperatorOptions($fieldSpec); // TODO: Change the autogenerated stub
  }


}
