<?php

namespace Reach\App\Widgets;

use Reach\App\Interfaces\WordPressHooks;
use Reach\App\Fields\SiteOptions;
use Reach\App\Fields\Groups\FieldGroupContactInfo;
use \WP_Widget;

/**
 * Class GoogleMapWidget
 *
 * @package Reach\App
 */
class GoogleMapWidget extends WP_Widget implements WordPressHooks {

    /**
     * GoogleMapWidget constructor.
     */
    public function __construct() {
        $widget_ops = [
            'classname'   => 'google_maps_widget',
            'description' => 'Google Maps Widget built with ACF Pro',
        ];
        parent::__construct( 'google_map_widget', 'Google Map Widget', $widget_ops );
    }

    public function addHooks() {
        add_action( 'widgets_init', [ $this, 'registerWidget' ], 5 );
        add_action( 'widgets_init', [ $this, 'registerFields' ], 10 );
    }

    /**
     * Outputs the content of the widget
     *
     * @param array $args
     * @param array $instance
     *
     * @return bool|void
     */
    public function widget( $args, $instance ) {
        // outputs the content of the widget
        if ( ! empty( $args['before_widget'] ) ) {
            echo $args['before_widget'];
        }

        $options   = SiteOptions::getSiteOptions();
        $api_key   = acf_get_setting( 'google_api_key' );
        $map_style = ! empty( $options['map_style'] ) ? $options['map_style'] : 'standard';
        $map_json  = AD_THEME_DIR_URI . 'map-definitions/' . $map_style . '.json';

        $multiple       = ! empty( $instance['acf']['field_ga900229921979'] ) ? $instance['acf']['field_ga900229921979'] : false;
        $location       = ! empty( $instance['acf']['field_5a0a141ef8eb3'] ) ? $instance['acf']['field_5a0a141ef8eb3'] : [];
        $location_posts = ! empty( $instance['acf']['field_ga900229921978'] ) ? $instance['acf']['field_ga900229921978'] : [];
        $locations      = FieldGroupContactInfo::getLocationsArray( $multiple, $location, $location_posts );

        if ( empty( $locations ) ) {
            return false;
        }
        ?>

        <div class="widget widget__map" data-json="<?php echo $map_json; ?>" data-api-key="<?php echo $api_key; ?>">
            <?php
            // loop through locations and output marker markup for the map.
            foreach ( $locations as $location ) {
                printf(
                    '<div class="marker" data-lat="%1$s" data-lng="%2$s"></div>',
                    ( ! empty( $location['lat'] ) ? esc_attr( $location['lat'] ) : '' ),
                    ( ! empty( $location['lng'] ) ? esc_attr( $location['lng'] ) : '' )
                );
            }
            ?>
        </div>

        <?php
        if ( ! empty( $args['after_widget'] ) ) {
            echo $args['after_widget'];
        }
    }

    /**
     * Outputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form( $instance ) {
        // outputs the options form on admin
    }

    /**
     * Processing widget options on save
     *
     * @param array $new_instance The new options
     * @param array $old_instance The previous options
     *
     * @return array
     */
    public function update( $new_instance, $old_instance ) {
        // processes widget options to be saved
        return $new_instance;
    }

    /**
     * Registers a WP_Widget widget
     */
    function registerWidget() {
        register_widget( $this );
    }

    function registerFields() {

        if ( function_exists( 'acf_add_local_field_group' ) ) {

            acf_add_local_field_group( [
                'key'      => 'group_5a0a140926fb0',
                'title'    => 'Google Map Widget',
                'fields'   =>
                    [
                        [
                            'key'          => 'field_ga900229921979',
                            'label'        => __( 'Location Posts', 'reach' ),
                            'instructions' => __( 'Enable/Disable selection of locations posts for this map.', 'reach' ),
                            'name'         => 'multiple_locations',
                            'type'         => 'true_false',
                            'ui'           => true
                        ],
                        [
                            'key'               => 'field_5a0a141ef8eb3',
                            'label'             => 'Google Map',
                            'instructions'      => __( 'Search for a single location to be displayed on your map.', 'reach' ),
                            'name'              => 'widget_google_map',
                            'type'              => 'google_map',
                            'conditional_logic' => [
                                [
                                    [
                                        'field'    => 'field_ga900229921979',
                                        'operator' => '!=',
                                        'value'    => '1'
                                    ]
                                ]
                            ]
                        ],
                        [
                            'key'               => 'field_ga900229921978',
                            'label'             => __( 'Locations', 'reach' ),
                            'instructions'      => __( 'Select which locations you would like to show as markers within this map.', 'reach' ),
                            'name'              => 'locations',
                            'type'              => 'relationship',
                            'conditional_logic' => [
                                [
                                    [
                                        'field'    => 'field_ga900229921979',
                                        'operator' => '==',
                                        'value'    => '1'
                                    ]
                                ]
                            ],
                            'post_type'         => [
                                0 => 'locations',
                            ],
                            'filters'           => [
                                0 => 'search',
                            ],
                            'return_format'     => 'id'
                        ],
                    ],
                'location' => [
                    [
                        [
                            'param'    => 'widget',
                            'operator' => '==',
                            'value'    => 'google_map_widget',
                        ],
                    ],
                ],
            ] );
        }
    }
}
