<?php

namespace Reach\App\Fields\Groups;

use Reach\App\Fields\Abstracts\AbstractFieldGroup;
use Reach\App\Fields\Types\AnimationAttributes;
use Reach\App\Modules\Modules;

/**
 * Class FieldGroupProducts
 *
 * addHooks()
 * getFields()
 *
 * @package Reach\App\Fields\Groups
 */
class FieldGroupProducts extends AbstractFieldGroup {

    /**
     * FieldGroupProducts constructor.
     *
     * @param $group_key
     * @param int $order
     */
    public function __construct( $group_key, $order = 0 ) {
        $this->group_key    = $group_key;
        $this->group_title  = __( 'Featured Products', 'reach' ) . ' - ' . substr($group_key, -4);
        $this->group_slug   = 'products-' . $this->group_key;
        $this->group_prefix = $this->group_key . '_products_';

        // register our parent group for this module
        $this->addLocalFieldGroupToPage( $this->group_key, $this->group_title, $this->group_slug, $order );
    }

    /**
     * Get animation defaults
     *
     * @return array
     *
     * @since 1.0.0
     */
    public static function animationDefaults() {
        return [
            'type'  => 'fade',
            'speed' => '1200'
        ];
    }

    /**
     * Register fields into our parent group
     *
     * @return array
     *
     * @since 1.0.0
     */
    public function getFields() {
        $fields = [
            [
                'key'       => 'field_5984557210206',
                'label'     => __( 'Content', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'   => 'field_5984557210207',
                'label' => __( 'Content', 'reach' ),
                'name'  => $this->group_prefix . 'content',
                'type'  => 'wysiwyg'
            ],
            [
                'key'           => 'field_5984557210208',
                'label'         => __( 'Featured Products', 'reach' ),
                'name'          => $this->group_prefix . 'products',
                'type'          => 'relationship',
                'post_type'     => [ 'products' ],
                'filters'       => '',
                'return_format' => 'object'
            ],
            [
                'key'   => 'field_5984557210209',
                'label' => __( 'Button', 'reach' ),
                'name'  => $this->group_prefix . 'button',
                'type'  => 'smart_button'
            ],
            [
                'key'       => 'field_5984557210239',
                'label'     => __( 'Attributes', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'      => 'field_4997626793575',
                'label'    => __( 'CSS Classes', 'reach' ),
                'name'     => $this->group_prefix . 'css_classes',
                'type'     => 'text',
                'wrapper'  => [
                    'width' => '100%',
                    'class' => ''
                ],
                'instructions'  => __( 'CSS classes entered here will be prefixed with &#34;rl-&#34; to avoid conflicts and added to this module’s wrapping element. ', 'reach' )
            ],
            [
                'key'     => 'field_8591648772113',
                'label'   => __( 'Button Alignment', 'reach' ),
                'name'    => $this->group_prefix . 'button_alignment',
                'type'    => 'select',
                'choices' => Modules::getButtonAlignmentOptions( $this->group_prefix ),
                'required'      => 0,
                'return_format' => 'value',
                'default_value' => [
                    0 => ''
                ]
            ],
            [
                'key'     => 'field_5984557210201',
                'label'   => __( 'Background', 'reach' ),
                'name'    => $this->group_prefix . 'background',
                'type'    => 'background_attributes',
                'wrapper' => [
                    'class' => ( ! current_user_can( 'administrator' ) ? ' acf-field--hidden' : '' )
                ],
                'instructions'  => __( 'Recommended minimum image dimensions 1440x500px. Width cropped at 1440px. Accepted file formats: .png .jpg', 'reach' )
            ],
            AnimationAttributes::registerFields( $this->group_prefix, self::animationDefaults() )
        ];

        return apply_filters( 'reach/fields/group/products', $fields, $this->group_prefix );
    }
}
