<?php

namespace Reach\App\Modules;

use Reach\App\Attachments\Media;
use Reach\App\Interfaces\WordPressHooks;

/**
 * Class Styles
 *
 * @package Reach\App\Modules
 */
class Styles implements WordPressHooks {

    /**
     * Add class hooks.
     */
    public function addHooks() {
        add_action( 'reach/modules/styles', [ $this, 'outputModuleInlineStyles' ] );
    }

    /**
     * Check the module data for any style options and output the inline style attributes
     *
     * @param $data
     */
    public function outputModuleInlineStyles( $data ) {
        // set the default attribute values
        $attributes = [
            'enable_padding' => 0,
            'padding_top'    => '0',
            'padding_bottom' => '0',
            'background'     => []
        ];

        if ( is_object( $data ) ) {
            // loop through the data object and check for our attribute indexes
            foreach ( $data as $key => $value ) {
                // check for enabled padding
                if ( stristr( $key, 'enable_padding' ) ) {
                    $attributes['enable_padding'] = $value;
                }
                // check for enabled padding
                if ( stristr( $key, 'padding_top' ) ) {
                    $attributes['padding_top'] = $value;
                }
                // check for enabled padding
                if ( stristr( $key, 'padding_bottom' ) ) {
                    $attributes['padding_bottom'] = $value;
                }
                // check for enabled padding
                if ( stristr( $key, 'background' ) ) {
                    $attributes['background'] = $value;
                }
            }
        }

        // output the inline styles
        printf(
            'style="%s%s"',
            Styles::getInlinePaddingStyles( $attributes['enable_padding'], $attributes['padding_top'], $attributes['padding_bottom'] ),
            Styles::getInlineBackgroundStyles( $attributes['background'] )
        );
    }

    /**
     * Check this modules attributes for our custom padding option enabled and output the styles if set1
     *
     * @param bool $enabled
     * @param $padding_top
     * @param $padding_bottom
     *
     * @return string
     */
    public static function getInlinePaddingStyles( $enabled = false, $padding_top, $padding_bottom ) {
        if ( ! $enabled ) {
            return '';
        }

        return sprintf(
            'padding-top: %spx;padding-bottom: %spx;',
            ( $padding_top ?: '0' ),
            ( $padding_bottom ?: '0' )
        );
    }

    /**
     * Pass an array of background properties into this method to build and return a string of inline styles
     *
     * @param $background
     * @param $type
     *
     * @return string
     */
    public static function getInlineBackgroundStyles( $background, $type = 'standard' ) {
        if ( empty( $background ) ) {
            return '';
        }

        $styles = '';

        // set text color
        $styles .= ! empty( $background['text_color'] ) ? 'color: ' . $background['text_color'] . ';' : '';

        // Check if parallax is set.
        if ( 'standard' === $type && ! empty( $background['size'] ) && 'parallax_all' === $background['size'] ) {
            $styles .= 'background:transparent;';

            return $styles;
        }

        // Check if we have a video file set.
        if ( ! empty( $background['video'] ) ) {
            $styles .= 'background:transparent;';

            return $styles;
        }

        // set background color
        $styles .= ! empty( $background['background_color'] ) ? 'background-color: ' . $background['background_color'] . ';' : '';

        if ( ! empty( $background['image'] ) ) {
            // get background image URL
            $background_image = Media::getAttachmentByID( $background['image'] );

            // set background image
            $styles .= $background_image ? 'background-image: url(\'' . $background_image->sizes['featured-large'] . '\');' : '';

            // set background position
            $styles .= 'background-position: ' . ( ! empty( $background['position'] ) ? $background['position'] : 'center center' ) . ';';

            // set background repeat
            $styles .= 'background-repeat: ' . ( ! empty( $background['repeat'] ) ? $background['repeat'] : 'no-repeat' ) . ';';

            // set background size
            $styles .= 'background-size: ' . ( ! empty( $background['size'] && $background['size'] !== 'parallax' ) ? $background['size'] : 'cover' ) . ';';
        }

        // add our background image to be deferred
        // $styles .= $background_image ? 'background-="' . esc_url( $background_image->sizes['featured-large'] ) . '"' : '';

        return $styles;
    }
}
