<?php

namespace GriffinForms\Frontend\Html\Forms;

class Format extends \GriffinForms\Frontend\Html\Format
{
    protected $meta;
    protected $sql;
    protected $lang;
    protected $alerts;
    protected $antispam;
    protected $id;
    protected $data = array();
    protected $item;
    protected $html_ids;
        
    protected function setSql()
    {
        $sql_class = '\GriffinForms\Frontend\Sql\\' . $this->meta;
        $this->sql = $sql_class::getInstance();
    }        
    
    protected function setLang()
    {
        $lang_class = '\GriffinForms\Frontend\Language\\' . $this->meta;
        $this->lang = $lang_class::getInstance();
    }
    
    protected function setAlerts()
    {
        $alerts_class = '\GriffinForms\Frontend\Html\Alerts\\' . $this->meta;
        $this->alerts = $alerts_class::getInstance();
    }
    
    protected function setAntiSpam()
    {
        $this->antispam = \GriffinForms\Frontend\Html\Security\AntiSpam::getInstance();
    }
    
    protected function setData()
    {
        $this->data[strtolower($this->meta)] = $this->id;
    }
    
    public function setItem()
    {    
        $item_class = '\GriffinForms\Items\\' . $this->meta;
        $this->item = new $item_class();
        $this->item->loadItem($this->id);
    }
    
    protected function setHtmlIds()
    {
        $prefix = 'griffinforms-' . strtolower($this->meta) . '-';
        $this->html_ids = array(
            'container' => $prefix . $this->id,
            'script' => $prefix . $this->id . '-js',
            'action_btn' => $prefix . 'nextbtn-' . $this->id,
            'back_btn' => $prefix . 'backbtn-' . $this->id
        );
    }
    
    protected function getJsAlertsObj()
    {
        return 'formpage' . $this->data['formpage'] . '-field-alerts';
    }
        
    final public function __construct($id, $data = array())
    {
        $this->id = $id;
        $this->data = array_merge($this->data, $data);
        $this->setAll();
        
        if (method_exists($this, 'itemJs')) {
            $this->itemJs();
        }
    }
}