<?php
namespace WPT\MLSL\Blocks;

/**
 * Styles.
 */
class Styles
{
    protected $entries;
    protected $styles;
    protected $attributes;
    protected $selectors;

    /**
     * constructor
     */
    public function __construct(
        $selectors,
        $attributes
    ) {
        $this->entries    = [];
        $this->selectors  = $selectors;
        $this->attributes = $attributes;
    }

    /**
     * Add style entry
     */
    public function add(
        $selectorName,
        $style
    ) {
        $selector                   = $this->selectors->get($selectorName);
        $this->entries[$selector][] = $style;
    }

    public function addStyle(
        $selectorName,
        $attributeName,
        $stylePrefix,
        $options = []
    ) {
        $attribute = $this->attributes[$attributeName];

        if (isset($options['postfix'])) {
            $attribute = $attribute . $options['postfix'];
        }

        $this->add(
            $selectorName,
            "{$stylePrefix} : {$attribute}"
        );
    }

    /**
     * Compute the CSS style string
     */
    public function compute()
    {
        $styles = '';

        foreach ($this->entries as $selector => $selector_styles) {
            $styles = $styles . $selector . '{' . implode(';', $selector_styles) . ';}';
        }

        return $styles;
    }

    /**
     * Add margin / padding
     */
    public function addSpacing(
        $selectorName,
        $attributeName,
        $styleName = 'margin'
    ) {
        $attribute = $this->attributes[$attributeName];

        $this->add(
            $selectorName,
            "{$styleName} : {$attribute['top']} {$attribute['right']} {$attribute['bottom']} {$attribute['left']}"
        );

    }

    /**
     * Add margin styles
     */
    public function addMargin(
        $selectorName,
        $attributeName
    ) {
        $this->addSpacing($selectorName, $attributeName, 'margin');
    }

    /**
     * Add padding styles
     */
    public function addPadding(
        $selectorName,
        $attributeName
    ) {
        $this->addSpacing($selectorName, $attributeName, 'padding');
    }

    /**
     * Add background styles.
     */
    public function addBackground(
        $selectorName,
        $attributeName,
        $options = []
    ) {
        $attribute = $this->attributes[$attributeName];
        $this->add(
            $selectorName,
            sprintf(
                "background-color: %s;
                background-image: %s;
                background-repeat: %s;
                background-size: %s;
                background-position: %s",
                $attribute['color'],
                isset($attribute['image']['url']) ? 'url(' . $attribute['image']['url'] . ')' : 'none',
                $attribute['repeat'],
                $attribute['size'],
                $attribute['position']
            )
        );

    }

    public function addBackgroundColor(
        $selectorName,
        $attributeName,
        $options = []
    ) {
        $attribute = $this->attributes[$attributeName];

        $this->add(
            $selectorName,
            sprintf(
                "background-color: %s;",
                $attribute['default']
            )
        );

        if (isset($options['hover']) && $options['hover']) {
            $this->add(
                $options['hover']['selectorName'],
                sprintf(
                    "background-color: %s;",
                    $attribute['hover']
                )
            );
        }

    }

    /**
     * Add border styles
     */
    public function addBorder(
        $selectorName,
        $attributeName,
        $options = []
    ) {
        $attribute = $this->attributes[$attributeName];

        $this->add(
            $selectorName,
            sprintf(
                "border-width: %s %s %s %s;
                border-style: %s;
                border-color: %s",
                $attribute['width']['top'],
                $attribute['width']['right'],
                $attribute['width']['bottom'],
                $attribute['width']['left'],
                $attribute['style'],
                $attribute['color']
            )
        );

        if (isset($options['hover']) && $options['hover']) {
            $this->addBorder(
                $options['hover']['selectorName'],
                $attributeName . 'Hover'
            );
        }
    }

    /**
     * Border radius style
     */
    public function addBorderRadius(
        $selectorName,
        $attributeName,
        $options = []
    ) {
        $attribute = $this->attributes[$attributeName];

        $styleValue = sprintf(
            '%s %s %s %s',
            $attribute['radius']["topLeft"],
            $attribute['radius']["topRight"],
            $attribute['radius']["bottomRight"],
            $attribute['radius']["bottomLeft"]
        );

        $this->add(
            $selectorName,
            "-webkit-border-radius:{$styleValue};-moz-border-radius:{$styleValue};border-radius:{$styleValue}"
        );

        if (isset($options['hover']) && $options['hover']) {
            $this->addBorderRadius(
                $options['hover']['selectorName'],
                $attributeName . 'Hover'
            );
        }

    }

    /**
     * Add typography styles
     */
    public function addTypography(
        $selectorName,
        $attributeName,
        $options = []
    ) {
        $attribute = $this->attributes[$attributeName];

        $style = sprintf(
            'font-family: %s;
            font-size: %s;
            font-weight: %s;
            font-style: %s;
            line-height: %s;
            letter-spacing: %s;
            word-spacing: %s;
            text-align: %s;
            text-transform: %s;
            color: %s',
            isset($attribute["fontFamily"]['value']) ? $attribute["fontFamily"]['value'] : '',
            $attribute["fontSize"],
            $attribute["fontWeight"],
            $attribute["fontStyle"],
            $attribute["lineHeight"],
            $attribute["letterSpacing"],
            $attribute["wordSpacing"],
            $attribute["textAlign"],
            $attribute["textTransform"],
            $attribute["color"]
        );

        $this->add(
            $selectorName,
            $style
        );

        if (isset($options['hoverColor'])) {
            $this->add(
                $options['hoverColor']['selectorName'],
                "color:" . $attribute['colorHover']
            );
        }
    }

}
