<?php

namespace Reach\App\Fields\Abstracts;

use Reach\App\Fields\RegisterFieldGroups;
use Reach\App\Interfaces\WordPressHooks;

/**
 * Class AbstractFieldGroup
 *
 * getFields()
 * addLocalFieldGroupToPage()
 * registerLocalFieldsToGroup()
 * verifyGroupPageVisibility()
 *
 * @package Reach\App\Fields\Abstracts
 */
abstract class AbstractFieldGroup implements WordPressHooks {

    /**
     * Parent group field key
     *
     * @var
     */
    protected $group_key;

    /**
     * Field group title
     *
     * @var
     */
    public $group_title;

    /**
     * Field Group slug identifier
     * @var
     */
    public $group_slug;

    /**
     * Field name prefix identifier
     * @var
     */
    public $group_prefix;

    /**
     * Add class hooks.
     */
    public function addHooks() {
        add_filter( 'acf/location/rule_match/' . $this->group_slug, [ $this, 'verifyGroupPageVisibility' ], 11, 3 );
    }

    /**
     * Register fields into our parent group
     *
     * @return array
     *
     * @since 1.0.0
     */
    public function getFields() {
        return [];
    }

    /**
     * Add a local field group with module data
     *
     * @param $key
     * @param $title
     * @param $param
     * @param $order
     *
     * @since 1.0.0
     */
    public function addLocalFieldGroupToPage( $key, $title, $param = 'post_type', $order = 0 ) {
        $group_args = [
            'key'        => $key,
            'title'      => $title,
            'fields'     => [],
            'location'   => [
                [
                    [
                        'param'    => $param,
                        'operator' => '==',
                        'value'    => 'page',
                    ],
                ]
            ],
            'active'     => 1,
            'menu_order' => $order,
        ];

        // get the front page ID and the current page ID that we're viewing
        $current_post  = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT );
        $front_page_id = get_option( 'page_on_front' );

        // hide content if registering the home page
        if ( (int) $front_page_id === $current_post ) {
            $group_args['hide_on_screen'] = [
                'the_content',
                'page_attributes',
                'revisions'
            ];
        }

        // register field group
        acf_add_local_field_group( $group_args );

        // get the fields specific to this parent group
        $fields = $this->getFields();

        // register our fields with the parent group
        $this->registerLocalFieldsToGroup( $fields, $this->group_key );
    }

    /**
     * Loop through fields and add them to our parent group
     *
     * @param $fields
     * @param $group_key
     * @param bool $append
     *
     * @return bool
     *
     * @since 1.0.0
     */
    public function registerLocalFieldsToGroup( $fields, $group_key, $append = true ) {
        // return false if we don't have fields or if the array is empty
        if ( ! $fields || ! is_array( $fields ) ) {
            return false;
        }

        // loop through fields
        foreach ( $fields as $field ) {
            // add parent group key to this field
            $field['parent'] = $group_key;

            // if append is set to true then we want to append the group key string to the end of our field key
            if ( $append ) {
                $field['key'] = $group_key . '_' . $field['key'];
            }

            // register the field into our parent group
            acf_add_local_field( $field );
        }

        return true;
    }

    /**
     * Custom location filter for our page modules
     * Check the site options for the current page and determine module visibility
     *
     * @param $match
     * @param $rule
     * @param $options
     *
     * @return bool
     */
    public function verifyGroupPageVisibility( $match, $rule, $options ) {
        // only check this rule if our post type is a page
        if ( ! array_key_exists( 'post_type', $options ) || 'page' !== $options['post_type'] ) {
            return $match;
        }

        // get the current page ID
        $current_page = $options['post_id'];

        // get the set module options for the current page
        $page_modules = get_option( 'options_page_modules_' . $current_page );

        if ( $page_modules && in_array( $rule['param'], $page_modules ) ) {
            return true;
        }

        return $match;
    }
}