<?php
/*************************************************************************************/
/*      This file is part of the "ArtabanMarketplace" Thelia 2 module.                       */
/*                                                                                   */
/*      email : gilles@artaban.fr                                                       */
/*      web : http://www.artaban.fr                                                  */
/*                                                                                   */
/*      For the full copyright and license information, please view the LICENSE.txt  */
/*      file that was distributed with this source code.                             */
/*************************************************************************************/

namespace ArtabanMarketplace\Form\Base;

use ArtabanMarketplace\ArtabanMarketplace;
use Thelia\Form\BaseForm;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * Class SellerImageCreateForm
 * @package ArtabanMarketplace\Form\Base
 * @author TheliaStudio, Artaban
 */
class SellerImageCreateForm extends BaseForm
{
    const FORM_NAME = "amp_seller_image_create";

    public function buildForm()
    {
        $translationKeys = $this->getTranslationKeys();
        $fieldsIdKeys = $this->getFieldsIdKeys();

        $this->addSellerIdField($translationKeys, $fieldsIdKeys);
        $this->addFileField($translationKeys, $fieldsIdKeys);
        $this->addVisibleField($translationKeys, $fieldsIdKeys);
        $this->addTitleField($translationKeys, $fieldsIdKeys);
        $this->addDescriptionField($translationKeys, $fieldsIdKeys);
        $this->addChapoField($translationKeys, $fieldsIdKeys);
        $this->addPostscriptumField($translationKeys, $fieldsIdKeys);
        $this->addLocaleField();
    }

    public function addLocaleField()
    {
        $this->formBuilder->add(
            'locale',
            'hidden',
            [
                'constraints' => [ new NotBlank() ],
                'required'    => true,
            ]
        );
    }

    protected function addSellerIdField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add("seller_id", "integer", array(
            "label" => $this->trans($this->readKey("seller_id", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("seller_id", $fieldsIdKeys)],
            "required" => true,
            "constraints" => array(
                new NotBlank(),
            ),
            "attr" => array(
            )
        ));
    }

    protected function addFileField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add('file', 'file', array(
            "label" => $this->trans($this->readKey("file", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("file", $fieldsIdKeys)],
            "required" => true,
            "constraints" => array(
                new NotBlank(),
            ),
            "attr" => array(
            )
        ));
    }

    protected function addVisibleField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add("visible", "checkbox", array(
            "label" => $this->trans($this->readKey("visible", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("visible", $fieldsIdKeys)],
            "required" => false,
            "constraints" => array(
            ),
            "attr" => array(
            )
        ));
    }

    protected function addTitleField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add("title", "text", array(
            "label" => $this->trans($this->readKey("title", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("title", $fieldsIdKeys)],
            "required" => false,
            "constraints" => array(
            ),
            "attr" => array(
            )
        ));
    }

    protected function addDescriptionField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add("description", "textarea", array(
            "label" => $this->trans($this->readKey("description", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("description", $fieldsIdKeys)],
            "required" => false,
            "constraints" => array(
            ),
            "attr" => array(
            )
        ));
    }

    protected function addChapoField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add("chapo", "textarea", array(
            "label" => $this->trans($this->readKey("chapo", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("chapo", $fieldsIdKeys)],
            "required" => false,
            "constraints" => array(
            ),
            "attr" => array(
            )
        ));
    }

    protected function addPostscriptumField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder->add("postscriptum", "textarea", array(
            "label" => $this->trans($this->readKey("postscriptum", $translationKeys)),
            "label_attr" => ["for" => $this->readKey("postscriptum", $fieldsIdKeys)],
            "required" => false,
            "constraints" => array(
            ),
            "attr" => array(
            )
        ));
    }

    public function getName()
    {
        return static::FORM_NAME;
    }

    public function readKey($key, array $keys, $default = '')
    {
        if (isset($keys[$key])) {
            return $keys[$key];
        }

        return $default;
    }

    public function getTranslationKeys()
    {
        return array(
            "seller_id" => "seller_image.create.seller_id",
            "file" => "seller_image.create.file",
            "visible" => "seller_image.create.visible",
            "position" => "seller_image.create.position",
            "title" => "seller_image.create.title",
            "description" => "seller_image.create.description",
            "chapo" => "seller_image.create.chapo",
            "postscriptum" => "seller_image.create.postscriptum",
        );
    }

    public function getFieldsIdKeys()
    {
        return array(
            "seller_id" => "seller_image_seller_id",
            "file" => "seller_image_file",
            "visible" => "seller_image_visible",
            "position" => "seller_image_position",
            "title" => "seller_image_title",
            "description" => "seller_image_description",
            "chapo" => "seller_image_chapo",
            "postscriptum" => "seller_image_postscriptum",
        );
    }

    public function trans($key, array $parameters = array(), $domain = ArtabanMarketplace::BO_MESSAGE_DOMAIN)
    {
        return $this->translator->trans($key, $parameters, $domain);
    }
}
