<?php

class tripal_views_handler_field_boolean extends tripal_views_handler_field {

  function option_definition() {
    $options = parent::option_definition();
    $options['type'] = ['default' => 'yes-no'];
    $options['type_custom_true'] = ['default' => '', 'translatable' => TRUE];
    $options['type_custom_false'] = ['default' => '', 'translatable' => TRUE];
    $options['not'] = ['definition bool' => 'reverse'];

    return $options;
  }

  function init(&$view, &$options) {
    parent::init($view, $options);

    $default_formats = [
      'yes-no' => [t('Yes'), t('No')],
      'true-false' => [t('True'), t('False')],
      'on-off' => [t('On'), t('Off')],
      'enabled-disabled' => [t('Enabled'), t('Disabled')],
      'boolean' => [1, 0],
      'unicode-yes-no' => ['✔', '✖'],
    ];
    $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : [];
    $custom_format = ['custom' => [t('Custom')]];
    $this->formats = array_merge($default_formats, $output_formats, $custom_format);
  }

  function options_form(&$form, &$form_state) {
    foreach ($this->formats as $key => $item) {
      $options[$key] = implode('/', $item);
    }

    $form['type'] = [
      '#type' => 'select',
      '#title' => t('Output format'),
      '#options' => $options,
      '#default_value' => $this->options['type'],
    ];

    $form['type_custom_true'] = [
      '#type' => 'textfield',
      '#title' => t('Custom output for TRUE'),
      '#default_value' => $this->options['type_custom_true'],
      '#states' => [
        'visible' => [
          'select[name="options[type]"]' => ['value' => 'custom'],
        ],
      ],
    ];

    $form['type_custom_false'] = [
      '#type' => 'textfield',
      '#title' => t('Custom output for FALSE'),
      '#default_value' => $this->options['type_custom_false'],
      '#states' => [
        'visible' => [
          'select[name="options[type]"]' => ['value' => 'custom'],
        ],
      ],
    ];

    $form['not'] = [
      '#type' => 'checkbox',
      '#title' => t('Reverse'),
      '#description' => t('If checked, true will be displayed as false.'),
      '#default_value' => $this->options['not'],
    ];
    parent::options_form($form, $form_state);
  }

  function render($values) {
    $value = $this->get_value($values);
    if (!empty($this->options['not'])) {
      $value = !$value;
    }

    if ($this->options['type'] == 'custom') {
      return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']);
    }
    else {
      if (isset($this->formats[$this->options['type']])) {
        return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
      }
      else {
        return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
      }
    }
  }
}