<?php

namespace GriffinForms\Admin\Html\FormControls;

abstract class Format extends \GriffinForms\Admin\Html\Format
{
    protected $meta;
    protected $secure;
    protected $css_class;
    protected $css_id;
    protected $required;
    protected $attr_name;
    protected $name;
    protected $value;
    protected $data;
    protected $buttons;
    protected $placeholder;
    protected $min;
    protected $step;
    
    abstract protected function html();

    protected function setButtons()
    {
        $this->buttons = \GriffinForms\Admin\Html\Elements\Buttons::getInstance();
    }
    
    protected function setCssClass()
    {
        $this->css_class = 'class="griffinforms-' . $this->purgeString($this->name) . ' griffinforms-' . strtolower($this->meta) . ' griffinforms-formcontrol"';
    }    
    
    protected function setCssId()
    {
        $this->css_id = 'id="griffinforms-' . $this->purgeString($this->name) . '-' . strtolower($this->meta) . '"';
    }

    protected function getElementId()
    {
        return 'griffinforms-' . $this->purgeString($this->name) . '-' . strtolower($this->meta);
    }
    
    public function elementId()
    {
        return 'griffinforms-' . $this->purgeString($this->name) . '-' . strtolower($this->meta);
    }
    
    public function setAttr($attr, $value)
    {
        $html = $attr . '="' . $value . '"';
        $this->$attr = $html;
    }
    
    protected function getAttr($attr)
    {
        if (isset($this->$attr)) {
            echo wp_kses_post($this->$attr);
        } else {
            echo '';
        }
    }
    
    protected function fixEmptyValue($key, $default = '', $array = '')
    {
        if (is_array($array)) {
            if (isset($this->value[$key]) and in_array($this->value[$key], array_keys($array))) {
                return $this->value[$key];
            } else {
                return array_key_first($array);
            }
        } else {
            if (isset($this->value[$key])) {
                return $this->value[$key];
            } else {
                return $default;
            }
        }
    }
    
    public function __construct($name = '', $value = '', $data = '')
    {
        $this->attr_name = 'name="' . $name . '"';
        $this->name = $name;
        $this->value = $this->maybeDecode($value);
        $this->data = $data;
        $this->setAll();
    
        if (method_exists($this, 'fieldJs')) {
            $this->fieldJs();
        }
    }
}
