<?php

namespace Blazemedia\Affiliation\Rendering;

use Blazemedia\Affiliation\API\CachedApi;
use Blazemedia\Affiliation\PostSignature\PostSignatureTable;
use Blazemedia\Affiliation\Rendering\Utils\LinksDataInPage;

/** 
 */
class PageLinksData {

    private string $author    = '';
    private string $alias     = '';
    private string $domain    = '';
    private string $tipologia = '';
    private string $custom    = '';
    private string $po        = '';
    private string $path      = '';
    private string $postid    = '';

    private $post;

    private $pageData;    
    private $cachedApi;

    public function __construct() {

        if( !is_singular() ) return;

        $this->post = get_post();
    
        add_filter( 'the_content', [ $this, 'printPageLinkAttributes' ], 900 , 1 );
        
    }

    public function get() {

        $this->cachedApi = CachedApi::getInstance();

        $pageDataObj = new PageData( $this->post );

        $this->pageData = $pageDataObj->getParams();

        $gtPageData = $this->getGTPageData();

        $this->domain    = $this->pageData['domain'];
        $this->author    = $this->post == null ? '' : $gtPageData->author->name;
        $this->alias     = $this->post == null ? '' : $gtPageData->author->alias_name;
        $this->tipologia = $this->post == null ? '' : $gtPageData->rules->type;
        $this->custom    = $this->post == null ? '' : $gtPageData->rules->custom;
        $this->po        = $this->post == null || !isset($gtPageData->rules->po) ?'' : $gtPageData->rules->po;
        $this->path      = $this->post == null ? '' : $this->getPagePath();
        $this->postid    = $this->post == null ? '' : $this->post->ID;
    }


    function getGTPageData() {

        $signatureTable = PostSignatureTable::getInstance();

        $domain     =  $this->pageData['domain'];
        $post_type  =  $this->pageData['post_type'];
        $post_id    =  $this->post != null ? $this->post->ID : '';
        $taxonomies =  $this->pageData['taxonomies'];
        $author_id  =  $this->pageData['author_id'];
        
        $apiCall = BLZ_API_RENDERING_MANAGER . "page/$domain/$post_type/$post_id/$taxonomies/$author_id";   

        $transient = $signatureTable->getSignatureId( $apiCall );

        $result = json_decode( $this->cachedApi->get( $apiCall, 0, $transient ) );



        /// questo controllo dovrebbe andare nel rendering manager
        if( !isset( $result->rules->type ) ) {
            $result->rules->type   = '';
            $result->rules->custom = '';
            $result->rules->po     = '';
        }

        return $result;
    }


    protected function getPagePath() {

        return get_permalink( $this->post );
    }



    /**
     * ritorna un array chiave-valore con i campi dell'oggetto
     *
     * @return array
     */
    public function toArray() : array {

        return [
            'domain'    => $this->domain,
            'author'    => $this->author,
            'alias'     => $this->alias,
            'tipologia' => $this->tipologia,
            'custom'    => $this->custom,
            'po'        => $this->po,
            'path'      => $this->path,
            'postid'    => $this->postid
        ];
    }

    public function toJSON() : string {

        return \json_encode( $this->toArray() );
    }


    /**
     * ritorna la stringa con i campi chiave-valore dell'oggetto 
     * sotto forma di data-attributes
     *
     * @return string
     */
    public function toDataAttributes( array $data ) : string {

        return array_reduce( array_keys( $data ), function( $attributes, $key) use ( $data ){

            return "{$attributes} data-{$key}=\"{$data[$key]}\"";

        },'');
    }



    public function getPageData( $params ) {

        $this->domain    = empty( $params['domain']    ) ? $this->domain    : $params['domain'];
        $this->author    = empty( $params['author']    ) ? $this->author    : $params['author'];
        $this->tipologia = empty( $params['tipologia'] ) ? $this->tipologia : $params['tipologia'];
        $this->custom    = empty( $params['custom']    ) ? $this->custom    : $params['custom'];
        $this->po        = empty( $params['po'    ]    ) ? $this->po        : $params['po'];
    }

    

    /**
     * Inserisce i dati raccolti dai link prima del body
     *
     * @return string 
     */
    public function printPageLinkAttributes( $content ) {

        $this->get();

        $data = $this->toArray();

        $linksData = LinksDataInPage::getInstance();

        $data['programs'] = implode( ',', $linksData->getPrograms() );
        $data['subjects'] = implode( ',', $linksData->getSubjects() );
        $data['themes'  ] = implode( ',', $linksData->getThemes()   );

        $dataAttributes = $this->toDataAttributes( $data );

        $data_element = '<span class="bm_post" style="display:none"'.$dataAttributes.'></span>';
        
        return $content . $data_element;
    }


}