<?php
namespace SabaiApps\Directories\Component\Field\Filter;

use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Field\Query;
use SabaiApps\Directories\Component\Entity;

class TextRangeFilter extends AbstractFilter
{
    protected function _fieldFilterInfo()
    {
        return [
            'label' => __('Text range input field', 'directories'),
            'field_types' => ['number', 'range'],
        ];
    }
    
    public function fieldFilterForm(IField $field, $filterName, array $settings, $request = null, Entity\Type\Query $query = null, array $current = null, array $parents = [])
    {
        $field_settings = $field->getFieldSettings();
        $min = isset($field_settings['min']) ? $field_settings['min'] : 0;
        $max = isset($field_settings['max']) ? $field_settings['max'] : 100;
        $ret = [
            '#entity_filter_form_type' => 'textfield',
            '#group' => true,
            'min' => [
                '#type' => 'text',
                '#placeholder' => __('Min', 'directories'),
                '#class' => 'drts-view-filter-ignore',
                '#weight' => 1,
                '#prefix' => '<div class="drts-row drts-gutter-none"><div class="drts-col-10 drts-view-filter-trigger-main"><div class="drts-row drts-gutter-xs"><div class="drts-col-6">',
                '#suffix' => '</div>',
                '#min_value' => $min,
                '#max_value' => $max,
                '#numeric' => true,
            ],
            'max' => [
                '#type' => 'text',
                '#placeholder' => __('Max', 'directories'),
                '#class' => 'drts-view-filter-ignore',
                '#weight' => 2,
                '#prefix' => '<div class="drts-col-6">',
                '#suffix' => '</div></div></div>',
                '#min_value' => $min,
                '#max_value' => $max,
                '#numeric' => true,
            ],
            'button' => array(
                '#prefix' => '<div class="drts-col-2 drts-view-filter-trigger-btn">',
                '#suffix' => '</div></div>',
                '#type' => 'markup',
                '#markup' => '<button type="button" class="' . DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-link ' . DRTS_BS_PREFIX . 'btn-block drts-view-filter-trigger">' .
                    '<i class="fas fa-fw fa-search"></i></button>',
                '#weight' => 3,
            ),
        ];

        return $ret;
    }
    
    public function fieldFilterIsFilterable(IField $field, array $settings, &$value, array $requests = null)
    {
        return (isset($value['min']) && strlen($value['min']))
            || (isset($value['max']) && strlen($value['max']));
    }
    
    public function fieldFilterDoFilter(Query $query, IField $field, array $settings, $value, array &$sorts)
    {
        if (!isset($value['min'])
            || !strlen($value['min'])
            || !is_numeric($value['min'])
        ) {
            $field_settings = $field->getFieldSettings();
            $value['min'] = isset($field_settings['min']) ? $field_settings['min'] : 0;
        }
        if (!isset($value['max'])
            || !strlen($value['max'])
            || !is_numeric($value['max'])
        ) {
            if (!isset($field_settings)) $field_settings = $field->getFieldSettings();
            $value['max'] = isset($field_settings['max']) ? $field_settings['max'] : 100;
        }
        if ($field->getFieldType() === 'number') {
            $query->fieldIsOrGreaterThan($field, $value['min'])
                ->fieldIsOrSmallerThan($field, $value['max']);
        } else {
            $query->fieldIsOrGreaterThan($field, $value['min'], 'min')
                ->fieldIsOrSmallerThan($field, $value['max'], 'max');
        }
    }

    public function fieldFilterLabels(IField $field, array $settings, $value, $form, $defaultLabel)
    {
        $field_settings = $field->getFieldSettings();
        $prefix = isset($field_settings['prefix']) && strlen($field_settings['prefix']) ? $field_settings['prefix'] : '';
        $suffix = isset($field_settings['suffix']) && strlen($field_settings['suffix']) ? $field_settings['suffix'] : '';
        if (!isset($value['min']) || !strlen($value['min'])) {
            $value['min'] = isset($field_settings['min']) ? $field_settings['min'] : 0;
        }
        if (!isset($value['max']) || !strlen($value['max'])) {
            $value['max'] = isset($field_settings['max']) ? $field_settings['max'] : 100;
        }

        return ['' => $this->_application->H($defaultLabel . ': ' . $prefix . $value['min'] . $suffix . ' - ' . $prefix . $value['max'] . $suffix)];
    }
}