<?php

namespace Planaday\Utility\FormBuilder;

use JsonSerializable;
use Planaday\Utility\FormBuilder\Structures\BasicStructure;
use Planaday\Utility\FormBuilder\SubmitStrategies\NoSubmit;

class Form implements JsonSerializable, Structure {
	use BasicStructure;

	protected string $name = '';
	protected SubmitStrategy $submitBehaviour;
	protected bool $writeDefaultData = true;
	protected bool $useHiddenFieldForJson = false;
	protected bool $forceDivElement = false;
	protected string $isDefaultText = 'Value is following the default or global value.';
	protected string $isChangedText = 'Value was changed.';
	protected string $resetButtonText = 'Reset value to default?';

	protected ?string $index = null;

	public function __construct() {
		$this->submitBehaviour = new NoSubmit();
	}

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

	public function setSubmitBehaviour(SubmitStrategy $behaviour): self {
		$this->submitBehaviour = $behaviour;
		return $this;
	}

	public function setWriteDefaultData(bool $writeDefaultData): self {
		$this->writeDefaultData = $writeDefaultData;
		return $this;
	}

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

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

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

	public function forceDivElement(): static {
		$this->forceDivElement = true;
		return $this;
	}

	public function outputJsonToHiddenField(): static {
		$this->useHiddenFieldForJson = true;
		return $this;
	}

	public function getName(): string {
		return $this->name;
	}

	public function getImplJson(): array {
		return [
			"type" => 'form',
			"name" => $this->name,
			"submitBehaviour" => $this->submitBehaviour,
			"writeDefaultData" => $this->writeDefaultData,
			"isDefaultText" => $this->isDefaultText,
			"isChangedText" => $this->isChangedText,
			"resetButtonText" => $this->resetButtonText,
			"forceDivElement" => $this->forceDivElement,
			"useHiddenFieldForJson" => $this->useHiddenFieldForJson
		];
	}

	public function getIndex(): ?string {
		return null;
	}

	public function getOwner(): ?Form {
		return $this;
	}
}