<?php

namespace Reach\App\Widgets;

use Reach\App\Interfaces\WordPressHooks;
use \WP_Widget;

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

  function __construct() {
    $widget_ops = array(
       'classname'   => 'custom_text_widget',
       'description' => 'Custom Text Widget built with ACF Pro',
     );
     parent::__construct( 'custom_text_widget', 'Custom Text Widget', $widget_ops );
  }

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

  /**
   * Outputs the content of the widget
   *
   * @param array $args
   * @param array $instance
   */
   public function widget( $args, $instance ) {
      // outputs the content of the widget
      $widget_id = $args["widget_id"];

      if( ! empty($args['before_widget']) ) {
        echo $args['before_widget'];
      }

      include(AD_THEME_PATH . "widgets/widget-custom-text.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;
   }

  function registerWidget() {
  	register_widget( $this );
  }

  function registerFeilds(){

    if( function_exists('acf_add_local_field_group') ) {

      acf_add_local_field_group(
        [
          'key'    => 'group_7383356798658',
          'title'  => 'Custom Text Widget',
          'fields' =>
          [  
            [
              'key'             => 'field_0545112742947',
              'label'           => __( 'Mobile Alignment', 'reach' ),
              'name'            => 'widget_mobile_content_alignment',
              'type'            => 'button_group',
              'instructions'    => __( 'Deselect option to use default alignment.', 'reach' ),          
              'choices'               => [
                'align-left-mobile'   => '<span class="dashicons dashicons-editor-alignleft"></span>',
                'align-center-mobile' => '<span class="dashicons dashicons-editor-aligncenter"></span>',
                'align-right-mobile'  => '<span class="dashicons dashicons-editor-alignright"></span>',
              ],
              'allow_null'        => 'true',
              'layout'            => 'horizontal',
              'return_format'     => 'value',
            ],
            [
              'key'           => 'field_0034764924673',
              'label'         => __( 'Tablet Alignment', 'reach' ),
              'name'          => 'widget_tablet_content_alignment',
              'type'          => 'button_group',
              'instructions'  => __( 'Deselect option to use default alignment.', 'reach' ), 
              'choices'               => [
                'align-left-tablet'   => '<span class="dashicons dashicons-editor-alignleft"></span>',
                'align-center-tablet' => '<span class="dashicons dashicons-editor-aligncenter"></span>',
                'align-right-tablet'  => '<span class="dashicons dashicons-editor-alignright"></span>',
              ],
              'allow_null'        => 'true',
              'layout'            => 'horizontal',
              'return_format'     => 'value',
            ],
            [
              'key'            => 'field_9341407638796',
              'label'          => __( 'Desktop Alignment', 'reach' ),
              'name'           => 'widget_desktop_content_alignment',
              'type'           => 'button_group',
              'instructions'   => __( 'Deselect option to use default alignment.', 'reach' ),              
              'choices'                => [
                'align-left-desktop'   => '<span class="dashicons dashicons-editor-alignleft"></span>',
                'align-center-desktop' => '<span class="dashicons dashicons-editor-aligncenter"></span>',
                'align-right-desktop'  => '<span class="dashicons dashicons-editor-alignright"></span>',
              ],
              'allow_null'        => 'true',              
              'layout'            => 'horizontal',
              'return_format'     => 'value',
            ],
            [
            'key'             => 'field_2409560269914',
            'label'           => __( 'Content', 'reach' ),
            'name'            => 'widget_custom_text',
            'instructions'    => __( 'Alignment options within the text editor will override the responsive options above.', 'reach' ),
            'type'            => 'wysiwyg'
          ]
        ],
        'location' =>
        [
          [
            [
            'param'    => 'widget',
            'operator' => '==',
            'value'    => 'custom_text_widget',
            ],
          ],
        ],
        'menu_order' => 0,
        'position'   => 'normal',
        'style'      => 'default',
        'label_placement'       => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen'        => '',
        'active'                => 1,
        'description'           => '',
      ]);

    }
  }
 }
