<?php

namespace Reach\App\Fields\Groups;

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

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

    /**
     * FieldGroupContentArea constructor.
     *
     * @param $group_key
     * @param int $order
     */
    public function __construct( $group_key, $order = 0 ) {
        $this->group_key    = $group_key;
        $this->group_title  = __( 'Contact Info', 'reach' ) . ' - ' . substr( $group_key, - 4 );
        $this->group_slug   = 'contact-info-' . $this->group_key;
        $this->group_prefix = $this->group_key . '_contact_info_';

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

    /**
     * Parse our Contact Info location data and build an array of lat/lng locations for output in the google maps.
     *
     * @param bool $multiple
     * @param array $location
     * @param array $locations
     *
     * @return array
     *
     * @since 1.13.0
     */
    public static function getLocationsArray( $multiple = false, $location = [], $locations = [] ) {
        // Setup our default values
        $data = [];

        // Check if we need multiple locations or not.
        if ( $multiple ) {
            // Loop through our location arrays and get their post meta address value
            foreach ( $locations as $index => $post ) {
                $post_meta               = Util::getPostMeta( $post );
                $data[ $index ]          = ! empty( $post_meta['address'] ) ? $post_meta['address'] : '';
                $data[ $index ]['title'] = get_the_title( $post );
                if ( ! empty( $post_meta['location_content'] ) ) {
                    $data[ $index ]['content'] = $post_meta['location_content'];
                }
            }
        } elseif ( ! empty( $location['lat'] ) ) {
            $data[0] = $location;
        }

        return $data;
    }

    /**
     * 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_2950329921080',
                'label'     => __( 'Content', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'           => 'field_2950329921081',
                'label'         => __( 'Layout', 'reach' ),
                'name'          => $this->group_prefix . 'layout',
                'type'          => 'select',
                'choices'       => [
                    'content-left'  => __( 'Content / Map', 'reach' ),
                    'content-right' => __( 'Map / Content', 'reach' ),
                    'map'           => __( 'Map', 'reach' ),
                ],
                'default_value' => [
                    0 => 'content-left',
                ]
            ],
            [
                'key'               => 'field_295012559486',
                'label'             => __( 'Layout (Mobile)', 'reach' ),
                'name'              => $this->group_prefix . 'layout_mobile',
                'type'              => 'select',
                'choices'           => [
                    'content-top'    => __( 'Content / Map', 'reach' ),
                    'content-bottom' => __( 'Map / Content', 'reach' ),
                    'map'            => __( 'Map', 'reach' ),
                ],
                'default_value'     => [
                    0 => 'content-top',
                ],
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_2950329921081',
                            'operator' => '!=',
                            'value'    => 'map'
                        ]
                    ]
                ]
            ],
            [
                'key'               => 'field_2950329921082',
                'label'             => __( 'Content', 'reach' ),
                'name'              => $this->group_prefix . 'content',
                'type'              => 'wysiwyg',
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_2950329921081',
                            'operator' => '!=',
                            'value'    => 'map'
                        ]
                    ]
                ]
            ],
            [
                'key'          => 'field_2950329921979',
                'label'        => __( 'Location Posts', 'reach' ),
                'instructions' => __( 'Enable/Disable selection of locations posts for this map.', 'reach' ),
                'name'         => $this->group_prefix . 'multiple_locations',
                'type'         => 'true_false',
                'ui'           => true
            ],
            [
                'key'               => 'field_2950329921083',
                'label'             => __( 'Location', 'reach' ),
                'name'              => $this->group_prefix . 'location',
                'type'              => 'google_map',
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_2950329921979',
                            'operator' => '!=',
                            'value'    => '1'
                        ]
                    ]
                ]
            ],
            [
                'key'               => 'field_2950329921978',
                'label'             => __( 'Locations', 'reach' ),
                'instructions'      => __( 'Select which locations you would like to show as markers within this map.', 'reach' ),
                'name'              => $this->group_prefix . 'locations',
                'type'              => 'relationship',
                'conditional_logic' => [
                    [
                        [
                            'field'    => $this->group_key . '_field_2950329921979',
                            'operator' => '==',
                            'value'    => '1'
                        ]
                    ]
                ],
                'post_type'         => [
                    0 => 'locations',
                ],
                'filters'           => [
                    0 => 'search',
                ],
                'return_format'     => 'id'
            ],
            [
                'key'       => 'field_2950329921084',
                'label'     => __( 'Attributes', 'reach' ),
                'type'      => 'tab',
                'placement' => 'left'
            ],
            [
                'key'          => 'field_2943940260591',
                '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_2950329921034',
                '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_2950329921035',
                '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_2950329921036',
                '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_2950329921085',
                '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/contact-info', $fields, $this->group_prefix );
    }
}
