<?php

namespace WPIS;

class EstateDetailsListShortcode {

    private static $shortcode = 'wpis-details-list';

    private static $groups = [
        'general' => [
            'finance_price',
            'reference',
            'address_city',
            'category_label',
            'status_label',
            'building_state',
            'building_constructionYear',
            'building_orientation',
            'finance_cadastre'
        ],
        'configuration' => [
            'configuration_bedrooms',
            'configuration_floors',
            'configuration_bathrooms',
            'configuration_garden',
            'configuration_garage',
            'configuration_parking',
            'configuration_cave',
            'areas_total',
            'areas_ground',
            'areas_garden',
            'areas_living',
            'areas_diningroom',
            'areas_kitchen',
            'areas_washroom',
            'areas_dressing',
            'areas_bedroom1',
            'areas_bedroom2',
            'areas_bedroom3',
            'areas_bedroom4',
            'areas_bedroom5'
        ],
        'energy' => [
            'energy_epcLabel',
            'energy_epcCode',
            'energy_epcValue',
            'energy_epcTotal',
            'energy_gazEmit',
            'energy_heatingType',
            'energy_electicityConformity'
        ]
    ];

    static public function init() {

        $class = new self();

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

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

        global $post;

        $properties = isset($atts['properties'])
            ? $atts['properties']
            : (isset($atts['metas']) ? $atts['metas'] : false);

        if( ! $properties  ){
            if( !isset($atts['collection']) ){
                return $this->generateError('No collection provided ! Supported values are : general, configuration, energy and custom');
            }

            if( ! in_array($atts['collection'], ['general', 'configuration', 'energy']) ){
                return $this->generateError('Collection not valid ! Supported values are : general, configuration, energy and custom');
            }
        }
        else {
            $collection = explode(',', $properties);
        }

        $excludedReferences = [];
        if( isset($atts['exclude']) ){
            $refrences = explode(',', $atts['exclude']);
            foreach($refrences as $reference){
                $excludedReferences []= trim($reference);
            }
        }

        $language = substr( get_bloginfo ( 'language' ), 0, 2 );
        $collection = $collection ?? self::$groups[$atts['collection']];

        $buffer = '<ul class="wpis-detail-list collection-'. ($atts['collection'] ?? 'custom') . (isset($atts['class']) ?? ' ' . $atts['class']) .'">';

        foreach( $collection as $reference ){

            if( in_array($reference, $excludedReferences) ){
                continue;
            }

            $key = 'wpis_' . trim($reference);
            $value = EstateProperties::getFormattedValue($post->ID, $key, $language);
            $label = EstateProperties::getPropertyLabel($key);

            if( $key === 'wpis_finance_price' && EstateProperties::getValue($post->ID, 'wpis_finance_hidden') ){
                $value = false;
            }

            if( $value ) {
                $buffer .= '<li>';
                $buffer .= ' <span class="label label-' . $reference . '">' . $label . '</span>';
                $buffer .= ' <span class="value value-' . $reference . '">' . $value . '</span>';
                $buffer .= '</li>';
            };
        }

        $buffer .= '</ul>';

        if( ! preg_match('/<li>/', $buffer) ){
            return '';
        }

        return $buffer;
    }

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