<?php

class formEditBase {

	protected $name;
	protected $type;
	protected $value;
	protected $caption;
	protected $required;
	protected $parent;

	function __construct($parent) {
		$this->parent = $parent;
	}
	
	public function Parent() {
		if (func_num_args()) {
			$this->parent = func_get_arg(0);
			return $this;
		}
		return $this->parent;
	}
	public function Name() {
		if (func_num_args()) {
			$this->name = func_get_arg(0);
			return $this;
		}
		return $this->name;
	}
	public function Caption() {
		if (func_num_args()) {
			$this->caption = func_get_arg(0);
			return $this;
		}
		return $this->caption;
	}
	public function Type() {
		if (func_num_args()) {
			$this->type = func_get_arg(0);
			return $this;
		}
		return $this->type;
	}
	public function Value() {
		if (func_num_args()) {
			$this->value = func_get_arg(0);
			return $this;
		}
		return $this->value;
	}
	public function Required() {
		if (func_num_args()) {
			$this->required = func_get_arg(0);
			return $this;
		}
		return $this->required;
	}
	
	public function AsJson() {
		$res = [
			'name'	=> $this->name,
			'type'	=> $this->type,
			'caption'	=> $this->caption,
		];
		$res = hook_execute('form.field.json', $res, $this);
		return $res;
	}
	
	public function AsDraggableObject() {
		$fieldname = $this->name;
		$caption = $this->caption;
		$dcaption = htmlspecialchars($caption);
		return <<<BLOCK
<div class="formwizard_item" id="fe_{$fieldname}" data-name="{$fieldname}">{$dcaption}<div class="formwizard_itemedit" onclick="formwizard_remove('{$fieldname}')"></div></div>

BLOCK;
	}

	public function asHTML() {
		// hook
	}
	
	static
	public function Create($parent, $type, $name, $caption) {
		switch($type) {
			case 'string':
				$res = new formEditString($parent);
				break;
			case 'email':
				$res = new formEditEmail($parent);
				break;
			case 'tel':
				$res = new formEditPhone($parent);
				break;
			case 'text':
				$res = new formEditText($parent);
				break;
			case 'submit':
				$res = new formEditButton($parent);
				break;
		}
		return $res->Name($name)
					->Caption($caption);
	}
}


class formEditString extends formEditBase {
	
	function __construct($parent) {
		parent::__construct($parent);
		$this->Type('string');
	}
}

class formEditEmail extends formEditBase {
	
	function __construct($parent) {
		parent::__construct($parent);
		$this->Type('email');
	}
}

class formEditPhone extends formEditBase {
	
	function __construct($parent) {
		parent::__construct($parent);
		$this->Type('tel');
	}
}

class formEditText extends formEditBase {
	function __construct($parent) {
		parent::__construct($parent);
		$this->Type('text');
	}
}

class formEditBoolean extends formEditBase {
	function __construct($parent) {
		parent::__construct($parent);
		$this->Type('boolean');
	}
}

class formEditButton extends formEditBase {
	function __construct($parent) {
		parent::__construct($parent);
		$this->Type('submit');
	}

	public function AsJson() {
		$data = is_object($this->parent) ? $this->parent?->x_data : NULL;
		if (empty($data['preview'])) {
			$button = hook_execute_late('html.button', '', $this->caption, '', 'cc-blue');
			$res = [
				'name'	=> $this->name,
				'type'	=> 'button',
				'custom'	=> "<a class=\"submit\" href=\"#\">$button</a>",
			];
		} else {
			$res = [
				'name'	=> $this->name,
				'type'	=> 'button',
				'custom'	=> "<button onclick=\"javascript:return false\" title=\"non-functioning placeholder\">{$this->caption}</button>",
			];
			
		}
		$res = hook_execute('form.field.json', $res, $this);
		return $res;
	}

}

