<?php

namespace Reach\App\Fields\Groups;

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

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

    /**
     * FieldGroupListColumns constructor.
     *
     * @param $group_key
     * @param int $order
     */
    public function __construct( $group_key, $order = 0 ) {
        $this->group_key    = $group_key;
        $this->group_title  = __( 'List Columns', 'reach' ) . ' - ' . substr( $group_key, - 4 );
        $this->group_slug   = 'list-columns-' . $this->group_key;
        $this->group_prefix = $this->group_key . '_list_columns_';

        // 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'
        ];
    }

    /**
     * Get Layout Options
     *
     * @return array
     *
     * @since 1.0.0
     */
    public function getLayoutOptions() {

        $layouts = [
            'default'    => [
                'label' => __( 'Theme Default', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/grey-placeholder.png'
            ],
            'vertical'   => [
                'label' => __( 'Stacked Vertically', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/list-columns-vertical.jpg'
            ],
            'horizontal' => [
                'label' => __( 'Stacked Horizontally', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/list-columns-horizontal.jpg'
            ]
        ];

        if ( isset( $_GET['post'] ) ) {
            $id            = $_GET['post'];
            $page_meta     = Util::getPostMeta( $id );
            $currentLayout = Util::getField( $this->group_key . '_list_columns_layout', $page_meta, '' );

            if ( $currentLayout !== 'default' ) {
                unset( $layouts['default'] );
            }
        }

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


    /**
     * Register fields into our parent group
     *
     * @return mixed
     */
    public function getFields() {
        $fields = [
            [
                'key'       => 'field_8591648772200',
                'label'     => __( 'Content', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'   => 'field_8591648772201',
                'label' => __( 'Content', 'reach' ),
                'name'  => $this->group_prefix . 'content',
                'type'  => 'wysiwyg'
            ],
            [
                'key'           => 'field_8591648772209',
                'label'         => __( 'List Type', 'reach' ),
                'name'          => $this->group_prefix . 'list_type',
                'type'          => 'select',
                'choices'       => [
                    'list-items' => __( 'List Items', 'reach' ),
                    'content'    => __( 'Content', 'reach' ),
                ],
                'default_value' => [
                    0 => 'list-items',
                ]
            ],
            [
                'key'               => 'field_8591648772212',
                'label'             => __( 'Number of Columns', 'reach' ),
                'name'              => $this->group_prefix . 'columns_count',
                'type'              => 'select',
                'choices'           => [
                    '2' => __( '2 Columns', 'reach' ),
                    '3' => __( '3 Columns', 'reach' ),
                ],
                'default_value'     => [
                    0 => '2',
                ],
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_8591648772209',
                            'operator' => '==',
                            'value'    => 'list-items'
                        ]
                    ]
                ]
            ],
            [
                'key'               => 'field_8591648772210',
                'label'             => __( 'List Items', 'reach' ),
                'name'              => $this->group_prefix . 'list_items',
                'type'              => 'repeater',
                'layout'            => 'block',
                'required'          => 0,
                'collapsed'         => 'field_8591648772211',
                'button_label'      => __( 'Add List Item', 'reach' ),
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_8591648772209',
                            'operator' => '==',
                            'value'    => 'list-items'
                        ]
                    ]
                ],
                'sub_fields'        => [
                    [
                        'key'      => 'field_8591648772211',
                        'label'    => __( 'Item', 'reach' ),
                        'name'     => 'item',
                        '_name'    => 'item',
                        'type'     => 'text',
                        'wrapper'  => [
                            'width' => '',
                            'class' => ''
                        ],
                        'required' => 1,
                    ]
                ]
            ],
            [
                'key'               => 'field_8591648772204',
                'label'             => __( 'Columns', 'reach' ),
                'name'              => $this->group_prefix . 'columns',
                'type'              => 'repeater',
                'layout'            => 'block',
                'required'          => 0,
                'collapsed'         => 'field_8591648772206',
                'button_label'      => __( 'Add Column', 'reach' ),
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_8591648772209',
                            'operator' => '==',
                            'value'    => 'content'
                        ]
                    ]
                ],
                'sub_fields'        => [
                    [
                        'key'           => 'field_8591648772205',
                        'label'         => __( 'Image', 'reach' ),
                        'name'          => 'image',
                        '_name'         => 'image',
                        'type'          => 'image',
                        'return_format' => 'array',
                        'required'      => 0,
                    ],
                    [
                        'key'      => 'field_8591648772206',
                        'label'    => __( 'Headline', 'reach' ),
                        'name'     => 'headline',
                        '_name'    => 'headline',
                        'type'     => 'text',
                        'wrapper'  => [
                            'width' => '',
                            'class' => ''
                        ],
                        'required' => 1,
                    ],
                    [
                        'key'       => 'field_8591648772207',
                        'label'     => __( 'Content', 'reach' ),
                        'name'      => 'content',
                        '_name'     => 'content',
                        'type'      => 'textarea',
                        'rows'      => 3,
                        'required'  => 0,
                        'new_lines' => 'wpautop',
                    ]
                ],
            ],
            [
                'key'   => 'field_8591648772208',
                'label' => __( 'Button', 'reach' ),
                'name'  => $this->group_prefix . 'button',
                'type'  => 'smart_button'
            ],
            [
                'key'       => 'field_8591648772202',
                'label'     => __( 'Attributes', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'           => 'field_8973h3489324h789',
                'label'         => __( 'List Columns Layout', 'reach' ),
                'name'          => $this->group_prefix . 'layout',
                'type'          => 'radio_image',
                'choices'       => $this->getLayoutOptions(),
                'layout'        => 'horizontal',
                'default_value' => 'default'
            ],
            [
                'key'          => 'field_8538840079225',
                '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_8591648772981',
                'label'         => __( 'Padding', 'reach' ),
                'name'          => $this->group_prefix . 'enable_padding',
                'type'          => 'true_false',
                'wrapper'       => [
                    'width' => '20',
                ],
                'default_value' => 0,
                'ui'            => 1,
                'ui_on_text'    => __( 'On', 'reach' ),
                'ui_off_text'   => __( 'Off', 'reach' ),
            ],
            [
                'key'     => 'field_8591648772982',
                'label'   => __( 'Padding Top', 'reach' ),
                'name'    => $this->group_prefix . 'padding_top',
                'type'    => 'range',
                'wrapper' => [
                    'width' => '40'
                ],
                'min'     => 0,
                'max'     => 300,
                'step'    => 5,
                'append'  => 'px',
            ],
            [
                'key'     => 'field_8591648772983',
                'label'   => __( 'Padding Bottom', 'reach' ),
                'name'    => $this->group_prefix . 'padding_bottom',
                'type'    => 'range',
                'wrapper' => [
                    'width' => '40'
                ],
                'min'     => 0,
                'max'     => 300,
                'step'    => 5,
                'append'  => 'px',
            ],
            [
                'key'           => 'field_8591648772911',
                '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_8591648772203',
                '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/list-columns', $fields, $this->group_prefix );
    }
}
