<?php

namespace Reach\App\Plugins;

use Reach\App\Interfaces\WordPressHooks;
use Reach\App\Fields\SiteOptions;

/**
 * Class UberMenu
 *
 * @package Reach\App\Plugins
 */
class UberMenu implements WordPressHooks {

  /**
   * Add class hooks.
   */
  public function addHooks() {
    add_action( 'activate_plugin', [ $this, 'suppressActivation' ], 10, 2 );
    add_action( 'admin_notices', [ $this, 'activationNotice' ] );
    add_filter( 'ubermenu_instance_settings' , [ $this, 'addCustomInstanceSettingsDefaults' ] , 50 , 2 );
    add_filter( 'ubermenu_settings_panel_fields' , [ $this, 'addCustomSettingsPanelDefaults' ] , 50 , 1 );
  }

  /**
   * Don't allow the UberMenu plugin to be activated via the plugins page
   * (It must be activated via the Mega Menu Toggle under Options)
   * 
   * @param string $plugin
   * @param boolean $network_wide
   * 
   * @return void
   */
  public function suppressActivation( $plugin, $network_wide ) {
    global $pagenow;
    $options = SiteOptions::getSiteOptions();

    if ( empty($options['mega_menu']) && 'ubermenu/ubermenu.php' == $plugin && $pagenow == 'plugins.php' ) {
      $redirect = self_admin_url( 'plugins.php?megamenu=1' );
      wp_redirect( $redirect );
      exit;
    }
  }

  /**
   * Admin notification for UberMenu activation
   * 
   * @return void
   */
  public function activationNotice() {
    if ( ! is_plugin_active( 'ubermenu/ubermenu.php' ) && isset( $_GET['megamenu'] ) ) {
      echo '<div class="error"><p>UberMenu can only be activated through the Mega Menu Toggle under <a href="' . get_admin_url() . 'admin.php?page=theme-general-options">Options > Header</a>.</p></div>';
    }
  }

  /**
   * Add custom defaults to Main UberMenu Configuration
   * 
   * @param array $settings
   * @param string $config_id
   * 
   * @return array
   */
  public function addCustomInstanceSettingsDefaults($settings , $config_id) {

    // Top Level Responsive Columns (Tablet)
    $settings[415]['default'] = 1;
    // Submenu Responsive Columns (Tablet)
    $settings[416]['default'] = 1;
  
    return $settings;
   
  }

  /**
   * Add custom defaults to General Settings
   * 
   * @param array $all_fields
   * 
   * @return array
   */
  public function addCustomSettingsPanelDefaults( $all_fields ) {

    // Responsive Breakpoint
    $all_fields['ubermenu_general'][120]['default'] = '992';
  
    return $all_fields;
   
  }

}
