<?php

namespace Blazemedia\Affiliation\Rendering;

use Blazemedia\Affiliation\API\CachedApi;
use Blazemedia\Affiliation\Rendering\Utils\RandomClassGenerator;

class Table extends RenderingTemplate {

    public function __construct( string $shortcode = 'affiliate_table', string $format = 'table', string $format_id = 'table_id' ) {

        parent::__construct( $shortcode, $format, $format_id );
    }

    /**
     * Stampa il formato impostato da shortcode
     */
    public function manageLink( $atts, $content, $tag ) {

        $params = $this->pageData->getParams([
            
            'format'     => $this->format,
            'format_id'  => $atts[ $this->format_id ],
            'link_id'    => 0,
            'extra'      => urlencode(json_encode( [] ))
        ]);

        
        /// richiede il markup del link
        $this->getFormatData( $params );

        $this->randomizeClassForAdBlockers();

        return $this->markup;
    }


    function randomizeClassForAdBlockers() { 

        // randomize class for adblockers
        $random_class_markup = RandomClassGenerator::getInstance();

        $this->markup = str_replace('blz_grid', $random_class_markup->getRandomClass() , $this->markup . $this->getCSS() );
    }

    function getCSS(){
        $css = CachedApi::getInstance();

        $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 ,"gt_gridcss");
        $table  = $css->get( "{$stylesBaseUrl}/blz_tables.css",  24 ,"gt_tablecss");

        $random_class_css = RandomClassGenerator::getInstance();

        $table = str_replace('blz_grid', $random_class_css->getRandomClass() , $table);
        $grid  = str_replace('blz_grid', $random_class_css->getRandomClass() , $grid);

        return "<style>{$table}\n{$grid}</style>";
    }
}


