<?php

namespace Webien\Setup\Jet\DynamicTags;

/**
 * Note!
 * Register macros on jet-engine/register-macros action only,
 * as the base macro class \Jet_Engine_Base_Macros is not available before that action;
 * after it - all macros are registered already
 */

class SelectedPosts extends \Jet_Engine_Base_Macros {

    /**
     * Macros tag - this macro will look like %current_user_prop|ID% if typed manually
     */
    public function macros_tag() {
        return 'webien_tags_selected_posts_jet';
    }

    /**
     * Macros name in UI
     */
    public function macros_name() {
        return __( 'Kopplade poster [Haus]', 'webien' );
    }

    /**
     * Macros arguments - see this https://developers.elementor.com/docs/controls/regular-control/ for reference
     * An empty array may be returned if the macro has no arguments
     */
    public function macros_args() {
        return array(
            'source_type'    => array(
                'label' => __( 'Välj källa', 'webien' ),
                'type'    => 'select',
                'options' => array(
                    'current' => __('Aktuell post', 'webien'),
                    'option' => __('Inställningssida', 'webien'),
                ),
                'default' => 'current',
            ),
            'post_field'    => array(
                'label'   => __('Fält', 'webien') . '<br><small style="line-height:1.2;font-weight: 100;display: block;margin-bottom: 10px;">' .  __( 'Sluggen på fältet som ska användas. Om detta lämnas tomt går det automatiskt mot fält som startar med "kopplade_poster"', 'webien' ) . '</small>',
                //'description'   => __( 'Sluggen på fältet som ska användas. Om detta lämnas tomt går det automatiskt mot fält som startar med "kopplade_poster"', 'webien' ),
                'type'    => 'text',
                'default' => 'kopplade_poster',
            ),
        );
    }

    /**
     * Macros callback
     */
    public function macros_callback( $args = array() ) {

        $source_type = ! empty( $args['source_type'] ) ? $args['source_type'] : null;
        //$option_field = ! empty( $args['option_field'] ) ? $args['option_field'] : null;
        $option_field = ! empty( $args['post_field'] ) ? $args['post_field'] : null;
        $post_field = ! empty( $args['post_field'] ) ? $args['post_field'] : null;

        $idArr = [];
        $metaKey = null;

        if($source_type === 'option' && !empty($option_field)){
            $posts = get_option('options_' . $option_field);

            $metaKey = 'options_' . $option_field;

            foreach($posts as $id){
                $idArr[] = $id;
            }

        } else {

            $meta = get_post_meta(get_the_ID());

            // If field is specified
            if($post_field){
                $posts = get_field($post_field, get_the_ID());
                $metaKey = $post_field;

                foreach($posts as $id){
                    $idArr[] = $id;
                }

            } else {

                // Get ACF fields that starts with our prefix and collect those id:s
                foreach ($meta as $key => $field){
                    if(substr( $key, 0, 15 ) === "kopplade_poster") {
                        $metaKey = $key;

                        $arr = unserialize($field[0]);
                        if($arr){
                            foreach($arr as $id){
                                $idArr[] = $id;
                            }
                        }
                    }
                }
            }


        }

        // Make sure to just include post ID:s even if ACF is set to return object
        $finalArr = [];
        foreach ($idArr as $arr) {
            if(is_object($arr)){
                $finalArr[] = $arr->ID;
            } else {
                $finalArr[] = $arr;
            }
        }


        // make sure only uniques
        $finalArr = array_unique($finalArr);

        // If empty return a dummy id so we don't get all posts
        if(empty($finalArr)){
            $finalArr[] = 0;
        }

        $finalArr = apply_filters('webien/plugin/kopplade_poster', $finalArr, $metaKey);

        return implode(',', $finalArr);
    }
}

/**
 * Create an instance of Current_User_Prop to add current_user_prop macro
 * of course you may create a separate file with the class, include it instead of declaring the class right in the action,
 * and then create an instance of it
 */
