<?php

namespace Reach\App\Fields\Groups;

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

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

    /**
     * FieldGroupGallery constructor.
     *
     * @param $group_key
     * @param int $order
     * @param bool $register
     */
    public function __construct( $group_key, $order = 0, $register = true ) {
        $this->group_key    = $group_key;
        $this->group_title  = __( 'Gallery', 'reach' ) . ' - ' . substr( $group_key, - 4 );
        $this->group_slug   = 'gallery-' . $this->group_key;
        $this->group_prefix = $this->group_key . '_gallery_';

        // if false, don't continue to register within the page.
        if ( ! $register ) {
            return false;
        }

        // 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_591ca1f42e819',
                'label'     => __( 'Content', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'   => 'field_591ca1f42e821',
                'label' => __( 'Content', 'reach' ),
                'name'  => $this->group_prefix . 'content',
                'type'  => 'wysiwyg'
            ],
            [
                'key'          => 'field_591ca1f42e820',
                'label'        => __( 'Images', 'reach' ),
                'name'         => $this->group_prefix . 'images',
                'type'         => 'gallery',
                'instructions' => __( 'Thumbnail images are cropped to 480x480px, Detail images width cropped at 1440px. Accepted file formats: .png .jpg', 'reach' )
            ],
            [
                'key'   => 'field_35a591ca6da88',
                'label' => __( 'Button', 'reach' ),
                'name'  => $this->group_prefix . 'button',
                'type'  => 'smart_button'
            ],
            [
                'key'       => 'field_591ca1f42e822',
                'label'     => __( 'Attributes', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'           => 'field_8973h3489324h324',
                'label'         => __( 'Gallery Layout', 'reach' ),
                'name'          => $this->group_prefix . 'layout',
                'type'          => 'radio_image',
                'choices'       => $this->getGalleryLayoutOptions(),
                'layout'        => 'horizontal',
                'default_value' => 'default'
            ],
            [
                'key'           => 'field_71810a8701e99',
                'label'         => __( 'Carousel Slides', 'reach' ),
                'name'          => $this->group_prefix . 'slides_to_show',
                'type'          => 'select',
                'instructions'  => __( 'Applies to the carousel layout.', 'reach' ),
                'choices'       => $this->getCarouselOptions(),
                'default_value' => [
                    0 => '',
                ],
            ],
            [
                'key'           => 'field_8973h3489324a002',
                'label'         => __( 'Gallery Width', 'reach' ),
                'name'          => $this->group_prefix . 'width',
                'type'          => 'select',
                'choices'       => [
                    'sm' => __( 'Small', 'reach' ),
                    'md' => __( 'Medium', 'reach' ),
                    'lg' => __( 'Large', 'reach' ),
                ],
                'default_value' => [
                    0 => 'md',
                ],
                'instructions'  => __( 'Select the overall width of the gallery container.', 'reach' )
            ],
            [
                'key'          => 'field_8987622106215',
                '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_h29be82vf92b9ad3',
                'label'         => __( 'Captions', 'reach' ),
                'name'          => $this->group_prefix . 'enable_captions',
                'type'          => 'true_false',
                'instructions'  => __( 'Show image captions for gallery overlay and filmstrip.', 'reach' ),
                'wrapper'       => [
                    'width' => '100',
                    'class' => ( ! current_user_can( 'administrator' ) ? ' acf-field--hidden' : '' )
                ],
                'default_value' => 0,
                'ui'            => 1,
                'ui_on_text'    => __( 'On', 'reach' ),
                'ui_off_text'   => __( 'Off', 'reach' ),
            ],
            [
                'key'           => 'field_591ca1f42e82901',
                'label'         => __( 'Padding', 'reach' ),
                'name'          => $this->group_prefix . 'enable_padding',
                'type'          => 'true_false',
                'wrapper'       => [
                    'width' => '20',
                    'class' => ( ! current_user_can( 'administrator' ) ? ' acf-field--hidden' : '' )
                ],
                'default_value' => 0,
                'ui'            => 1,
                'ui_on_text'    => __( 'On', 'reach' ),
                'ui_off_text'   => __( 'Off', 'reach' ),
            ],
            [
                'key'     => 'field_591ca1f42e82902',
                'label'   => __( 'Padding Top', 'reach' ),
                'name'    => $this->group_prefix . 'padding_top',
                'type'    => 'range',
                'wrapper' => [
                    'width' => '40',
                    'class' => ( ! current_user_can( 'administrator' ) ? ' acf-field--hidden' : '' )
                ],
                'min'     => 0,
                'max'     => 300,
                'step'    => 5,
                'append'  => 'px',
            ],
            [
                'key'     => 'field_591ca1f42e82903',
                'label'   => __( 'Padding Bottom', 'reach' ),
                'name'    => $this->group_prefix . 'padding_bottom',
                'type'    => 'range',
                'wrapper' => [
                    'width' => '40',
                    'class' => ( ! current_user_can( 'administrator' ) ? ' acf-field--hidden' : '' )
                ],
                'min'     => 0,
                'max'     => 300,
                'step'    => 5,
                'append'  => 'px',
            ],
            [
                'key'     => 'field_8591648772866',
                '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_591ca1f42e829',
                '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/gallery', $fields, $this->group_prefix );
    }

    /**
     * Get gallery layout options for the gallery module
     *
     * @return array
     */
    public function getGalleryLayoutOptions() {

        $galleryLayouts = [
            'default'      => [
                'label' => __( 'Carousel', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/gallery-default.jpg'
            ],
            'bleeding'     => [
                'label' => __( 'Grid Bleeding Edges', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/gallery-bleeding.jpg'
            ],
            'non-bleeding' => [
                'label' => __( 'Grid Non-bleeding Edges', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/gallery-non-bleeding.jpg'
            ],
            'masonry'      => [
                'label' => __( 'Masonry', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/gallery-masonry.jpg'
            ],
            'filmstrip'    => [
                'label' => __( 'Filmstrip', 'reach' ),
                'image' => AD_THEME_DIR_URI . 'assets/images/fields/gallery-filmstrip.jpg'
            ]
        ];

        return $galleryLayouts;
    }

    /**
     * Get carousel options for the gallery module
     *
     * @return array
     */
    public function getCarouselOptions() {
        
        $slidesToShow = [

            ''  => __( 'Disabled (depreciated)', 'reach' ),
            '2' => __( '2 Slides', 'reach' ),
            '3' => __( '3 Slides', 'reach' ),
            '4' => __( '4 Slides', 'reach' ),
        ];

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

            if ( ! empty(  $currentSlidesToShow ) ) {
                unset(  $slidesToShow[''] );
            }
        }

        return $slidesToShow;
    }
}
