<?php

namespace Blazemedia\Affiliation\Rendering;

/** 
 */
class Disclaimer {

    private static $instance;

    private $status = false;

    private string $defaultDisclaimer;
    
    private array $disclaimers = [];

    private $post;

    private function __construct() {

        $options = get_option('affiliation_settings_option_name');

        $this->post = get_post();

        $this->defaultDisclaimer = $options !== false ? $options['disclaimer'] : '';

        add_filter( 'the_content', [ $this, 'printDisclaimer' ], 900 , 1 );

    }


    public function get() : array {

        return $this->disclaimers;
    }

    public function add( string $disclaimer ) : void {

        if( trim($disclaimer) == '' || in_array( $disclaimer, $this->disclaimers ) ) return;

        $this->disclaimers[] = $disclaimer;
    }

    public function getStatus() : bool {
        return $this->status;
    }

    public function setStatus( bool $status ) : void {
        $this->status = $status;
    }

    public function printDisclaimer( $content ) {

        if( !$this->status ) return $content;

        if( $this->hasNoDisclaimerTag() || $this->isNoDisclaimerFlagOn() ) return $content;

        return empty( $this->disclaimers ) ? $this->printDefaultDisclaimer( $content ) : $this->printCustomDisclaimers( $content);
    }

    
    private function isNoDisclaimerFlagOn() {

        $has_disclaimer = get_post_meta( $this->post->ID, 'has_disclaimer', true );

        return !empty($has_disclaimer) && $has_disclaimer == "no";
    }


    private function hasNoDisclaimerTag(){

        return has_tag('no-disclaimer', $this->post );
    }

    private function printDefaultDisclaimer( $content ) {

        $errorStyle = <<<HTML
        <style>.bmaff_error { display: none !important;}</style>
        HTML;

        $disclaimerMarkup = <<<HTML
            <div class="blz_affiliation_disclaimer">{{ disclaimer }}</div>
        HTML;

        if (!is_user_logged_in()) {
            $disclaimerMarkup .= $errorStyle;
        };
        return $content . str_replace('{{ disclaimer }}', html_entity_decode($this->defaultDisclaimer), $disclaimerMarkup );
    }

    private function printCustomDisclaimers( $content ) {

        $errorStyle = <<<HTML
        <style>.bmaff_error { display: none !important;}</style>
        HTML;
   
        $disclaimerContainer = <<<HTML
            <div class="blz_custom_disclaimer">{{ disclaimer_default }}{{ disclaimers }}</div> 
        HTML;

        $disclaimerMarkup = <<<HTML
            {{ disclaimer }}
        HTML;

        if (!is_user_logged_in()) {
            $disclaimerContainer .= $errorStyle;
        };

        $disclaimerContainer = str_replace('{{ disclaimer_default }}', html_entity_decode($this->defaultDisclaimer) ."<br><br>", $disclaimerContainer );

        $disclaimers .= array_reduce( $this->disclaimers, function( $markup, $disclaimer) use ( $disclaimerMarkup ) {

            return $markup . str_replace('{{ disclaimer }}', html_entity_decode($disclaimer)."<br><br>", $disclaimerMarkup );

        }, '');

        return $content . str_replace('{{ disclaimers }}', $disclaimers, $disclaimerContainer );

    }


    /**
     * Metodo pubblico per l'accesso all'istanza unica di classe.
     * @return object|
     */
    public static function getInstance() {

        if ( !isset( self::$instance ) ) {
            
            self::$instance = new Disclaimer();
        }
        
        return self::$instance;
    }

}