<?php

namespace Blazemedia\Affiliation\Rendering;

use Blazemedia\Affiliation\API\CachedApi;

use Blazemedia\Affiliation\Rendering\Utils\RandomClassGenerator;

class Styles  {


    function __construct() {

        $this->insertCSS();        
    }

    

    function insertCSS() {

        $css = CachedApi::getInstance();
        $pageData = new PageData;

        $stylesBaseUrl = str_replace('/wp-json/api/v1/','', BLZ_API_FORMAT_PROVIDER) . '/app/themes/format_provider/views/assets/styles';

        $grid   = $css->get( "{$stylesBaseUrl}/blz_grid.css",    24 );
        $table  = $css->get( "{$stylesBaseUrl}/blz_tables.css",  24 );
        $btn    = $css->get( "{$stylesBaseUrl}/blz_buttons.css", 24 );
        $link   = $css->get( "{$stylesBaseUrl}/blz_link.css",    24 );
        $cards  = $css->get( "{$stylesBaseUrl}/blz_cards.css",   24 );

        $custom = $css->get( BLZ_API_FORMAT_PROVIDER . "styles/{$pageData->getDomain()}" , 24);

        if( $grid != '') add_action('wp_head', function() use( $grid ) {


            $random_class_css = RandomClassGenerator::getInstance();

            $grid = str_replace('blz_grid', $random_class_css->getRandomClass() , $grid);
            
            echo "<style>{$grid}</style>";
        });

        if( $link != '') add_action('wp_head', function() use( $link ) {
            echo "<style>{$link}</style>";
        });

        if( $btn != '') add_action('wp_head', function() use( $btn ) {
            echo "<style>{$btn}</style>";
        });

        if( $table != '') add_action('wp_head', function() use( $table ) {

            $random_class_css = RandomClassGenerator::getInstance();

            $table = str_replace('blz_grid', $random_class_css->getRandomClass() , $table);
            
            echo "<style>{$table}</style>";
        });


        if( $cards  != '') add_action('wp_head', function() use( $cards ) {

            $random_class_css = RandomClassGenerator::getInstance();

            $cards = str_replace('blz_grid', $random_class_css->getRandomClass() , $cards);
            
            echo "<style>{$cards}</style>";
        });

        if( $custom != '') add_action('wp_head', function() use( $custom ) {
            echo "<style>{$custom}</style>";
        });

    }

}


