<?php
/**
* This class has been generated by TheliaStudio
* For more information, see https://github.com/thelia-modules/TheliaStudio
*/

namespace StripePayment\Form\Base;

use StripePayment\StripePayment;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Thelia\Form\BaseForm;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * Class StripePaymentConfigForm
 * @package StripePayment\Form\Base
 * @author TheliaStudio
 */
class StripePaymentConfigForm extends BaseForm
{
    const FORM_NAME = "stripepayment_config_form";

    /**
     *
     * in this function you add all the fields you need for your Form.
     * Form this you have to call add method on $this->formBuilder attribute :
     *
     * $this->formBuilder->add("name", "text")
     *   ->add("email", "email", array(
     *           "attr" => array(
     *               "class" => "field"
     *           ),
     *           "label" => "email",
     *           "constraints" => array(
     *               new \Symfony\Component\Validator\Constraints\NotBlank()
     *           )
     *       )
     *   )
     *   ->add('age', 'integer');
     *
     * @return null
     */
    protected function buildForm()
    {
        $translationKeys = $this->getTranslationKeys();
        $fieldsIdKeys = $this->getFieldsIdKeys();

        $this->addEnabledField($translationKeys, $fieldsIdKeys);
        $this->addStripeElementField($translationKeys, $fieldsIdKeys);
        $this->addOneClickPaymentField($translationKeys, $fieldsIdKeys);
        $this->addSecretKeyField($translationKeys, $fieldsIdKeys);
        $this->addPublishableKeyField($translationKeys, $fieldsIdKeys);
        $this->addWebhooksKeyField($translationKeys, $fieldsIdKeys);
        $this->addSecureUrlField($translationKeys, $fieldsIdKeys);
    }

    protected function addEnabledField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("enabled", CheckboxType::class, array(
                "label" => $this->readKey("enabled", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("enabled", $fieldsIdKeys),
                    "help" => $this->readKey("help.enabled", $translationKeys)
                ],
                "required" => false,
                "constraints" => array(
                ),
                "value" => StripePayment::getConfigValue(StripePayment::ENABLED, false),
            ))
        ;
    }

    protected function addStripeElementField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("stripe_element", CheckboxType::class, array(
                "label" => $this->readKey("stripe_element", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("stripeelementch", $fieldsIdKeys),
                    "help" => $this->readKey("help.stripe_element", $translationKeys)
                ],
                "required" => false,
                "constraints" => array(
                ),
                "value" => StripePayment::getConfigValue(StripePayment::STRIPE_ELEMENT, false),
            ))
        ;
    }

    protected function addOneClickPaymentField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("one_click_payment", CheckboxType::class, array(
                "label" => $this->readKey("one_click_payment", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("one_click_payment", $fieldsIdKeys)
                ],
                "required" => false,
                "constraints" => array(
                ),
                "value" => StripePayment::getConfigValue(StripePayment::ONE_CLICK_PAYMENT, false),
            ))
        ;
    }

    protected function addSecretKeyField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("secret_key", TextType::class, array(
                "label" => $this->readKey("secret_key", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("secret_key", $fieldsIdKeys),
                    "help" => $this->readKey("help.secret_key", $translationKeys)
                ],
                "required" => true,
                "constraints" => array(
                    new NotBlank(),
                ),
                "data" => StripePayment::getConfigValue(StripePayment::SECRET_KEY),
            ))
        ;
    }

    protected function addPublishableKeyField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("publishable_key", TextType::class, array(
                "label" => $this->readKey("publishable_key", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("publishable_key", $fieldsIdKeys),
                    "help" => $this->readKey("help.publishable_key", $translationKeys)
                ],
                "required" => true,
                "constraints" => array(
                    new NotBlank(),
                ),
                "data" => StripePayment::getConfigValue(StripePayment::PUBLISHABLE_KEY),
            ))
        ;
    }

    protected function addWebhooksKeyField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("webhooks_key", TextType::class, array(
                "label" => $this->readKey("webhooks_key", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("webhooks_key", $fieldsIdKeys),
                    "help" => $this->readKey("help.webhooks_key", $translationKeys)
                ],
                "required" => true,
                "constraints" => array(
                    new NotBlank(),
                ),
                "data" => StripePayment::getConfigValue(StripePayment::WEBHOOKS_KEY),
            ))
        ;
    }
    protected function addSecureUrlField(array $translationKeys, array $fieldsIdKeys)
    {
        $this->formBuilder
            ->add("secure_url", TextType::class, array(
                "label" => $this->readKey("secure_url", $translationKeys),
                "label_attr" => [
                    "for" => $this->readKey("secure_url", $fieldsIdKeys),
                    "help" => $this->readKey("help.secure_url", $translationKeys)
                ],
                "required" => true,
                "constraints" => array(
                    new NotBlank(),
                ),
                "data" => StripePayment::getConfigValue(StripePayment::SECURE_URL),
            ))
        ;
    }

    public static 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();
    }

    public function getFieldsIdKeys()
    {
        return array(
            "enabled" => "enabled",
            "secret_key" => "secret_key",
            "publishable_key" => "publishable_key",
            "webhooks_key" => "webhooks_key",
            "secure_url" => "secure_url"
        );
    }
}
