<?php
declare(strict_types=1);

namespace AuxCF\Entities\Field;

use AuxCF\Entities\Field\Enum\FieldTypeEnum;
use AuxCF\Entities\Field\Enum\TaxonomyFieldReturnFormatEnum;
use AuxCF\Entities\Field\Enum\TaxonomyFieldTypeEnum;
use AuxCF\Entities\Field\Traits\AllowNullTraits;
use AuxCF\Entities\FieldEntity;
use AuxCF\Entities\Settings\Enum\IntSwitchEnum;
use AuxCF\Entities\Settings\Traits\EnumPropertyTraits;

class TaxonomyFieldEntity extends FieldEntity
{
  use AllowNullTraits, EnumPropertyTraits;

  protected static array $arrayPropertiesMethods = [
    'getAllowNullArray',
  ];

  protected FieldTypeEnum $type = FieldTypeEnum::Taxonomy;

  protected string $taxonomy = '';
  protected TaxonomyFieldTypeEnum $fieldType = TaxonomyFieldTypeEnum::Checkbox;
  protected TaxonomyFieldReturnFormatEnum $returnFormat = TaxonomyFieldReturnFormatEnum::Id;
  protected IntSwitchEnum $addTerms = IntSwitchEnum::On;
  protected IntSwitchEnum $saveTerms = IntSwitchEnum::Off;
  protected IntSwitchEnum $loadTerms = IntSwitchEnum::Off;

  public function setFieldType(string|TaxonomyFieldTypeEnum $fieldType): self
  {
    $this->setEnumProperty(TaxonomyFieldTypeEnum::class, $fieldType, 'field_type');
    return $this;
  }

  public function setReturnFormat(string|TaxonomyFieldReturnFormatEnum $returnFormat): self
  {
    $this->setEnumProperty(TaxonomyFieldReturnFormatEnum::class, $returnFormat, 'return_format');
    return $this;
  }

  public function setAddTerms(int|IntSwitchEnum $addTerms): self
  {
    $this->setEnumProperty(IntSwitchEnum::class, $addTerms, 'add_terms');
    return $this;
  }

  public function setSaveTerms(int|IntSwitchEnum $saveTerms): self
  {
    $this->setEnumProperty(IntSwitchEnum::class, $saveTerms, 'save_terms');
    return $this;
  }

  public function setLoadTerms(int|IntSwitchEnum $loadTerms): self
  {
    $this->setEnumProperty(IntSwitchEnum::class, $loadTerms, 'load_terms');
    return $this;
  }

  public function setTaxonomy(string $taxonomy): self
  {
    $this->taxonomy = $taxonomy;
    return $this;
  }

  public function getFieldArray(): array
  {
    $array = [
      'taxonomy' => $this->taxonomy,
      'field_type' => $this->fieldType->value,
      'return_format' => $this->returnFormat->value,
      'add_term' => $this->addTerms->value,
      'save_terms' => $this->saveTerms->value,
      'load_terms' => $this->loadTerms->value,
      'bidirectional' => 0,
    ];

    return $array;
  }
}
