<?php

namespace WPIS;

class EstateMetaGroupShortcode {

    private static $shortcode = 'wpis-group';

    static public function init() {

        $class = new self();

        add_shortcode( self::$shortcode, [$class, 'generateShortcode'] );
    }

    public function generateShortcode($atts = false, $content = null) {

        global $post;

        $separator = ' ';
        $applyFilter = true;
        $showUnit = true;
        $skipZero = false;

        $language = substr( get_bloginfo ( 'language' ), 0, 2 );

        if( !isset($atts['metas']) ){
            return $this->generateError('No metas provided');
        }
        if( isset($atts['wrap']) && !strstr($atts['wrap'], '%s') ){
            return $this->generateError('No string replacement key found, you must add %s in the wrapper');
        };
        if( isset($atts['wrap']) && $content ){
            return $this->generateError('wrap option is not supported if content is used');
        };
        if( $content && !strstr($content, '%s')  ){
            return $this->generateError('No string replacement key found, you must add %s in the wrapper');
        };

        if( isset($atts['format']) && $atts['format'] === 'false' ){
            $applyFilter = false;
        }

        if( isset($atts['unit']) && $atts['unit'] === 'false' ){
            $showUnit = false;
        }

        if( isset($atts['skip-zero']) && $atts['skip-zero'] === 'true' ){
            $skipZero = true;
        }

        if( isset($atts['separator']) ){
            $separator = $atts['separator'];
        }

        $metas = [];
        if( isset($atts['metas']) ){
            $references = explode(',', $atts['metas']);
            foreach($references as $reference){
                $metas []= trim($reference);
            }
        }

        $metaGroup = '';
        $counter = 0;

        foreach( $metas as $meta ){

            if( !$applyFilter && !$showUnit ){
                $metaGroup .= EstateProperties::getValue($post->ID, 'wpis_' . $meta, $language);
            }
            else {
                $value = EstateProperties::getFormattedValue($post->ID, 'wpis_' . $meta, $language, $applyFilter, $showUnit);
                if( $value === '0' && $skipZero){
                    $metaGroup = substr($metaGroup, 0, -strlen($separator));
                    continue;
                }
                $metaGroup .= EstateProperties::getFormattedValue($post->ID, 'wpis_' . $meta, $language, $applyFilter, $showUnit);
            }

            if($counter >= count($metas) - 1 ){
                continue;
            }

            $metaGroup .= $separator;
            $counter++;
        }

        if( isset($atts['wrap']) ){
            $buffer = sprintf($atts['wrap'], $metaGroup);
        }
        elseif( $content ){
            $buffer = sprintf($content, $metaGroup);
        }
        else {
            $buffer = $metaGroup;
        }

        return $buffer;
    }

    public function generateError( $message ){
        return sprintf('[ %s ]', self::$shortcode) . ' : ' .  $message;
    }
}