<?php
class adminmenuPts extends modulePts
{
  protected $_mainSlug = 'tables-supsystic';
  private $_mainCap = 'manage_options';
  public function init()
  {
    parent::init();
    add_action('admin_menu', [$this, 'initMenu'], 9);
    $plugName = plugin_basename(PTS_DIR . PTS_MAIN_FILE);
    // add_filter('plugin_action_links_' . $plugName, array(
    //    $this,
    //    'addSettingsLinkForPlug'
    // ));
  }
  // public function addSettingsLinkForPlug($links) {
  //    $mainLink = 'http://supsystic.com/';
  //    $twitterStatus = sprintf(__('Cool WordPress plugins from supsystic.com developers. I tried %s - and this was what I need! #supsystic.com', PTS_LANG_CODE) , PTS_WP_PLUGIN_NAME);
  //    array_unshift($links, '<a href="' . $this->getMainLink() . '">' . __('Settings', PTS_LANG_CODE) . '</a>');
  //    array_push($links, '<a title="' . __('More plugins for your WordPress site here!', PTS_LANG_CODE) . '" href="' . $mainLink . '" target="_blank">supsystic.com</a>');
  //    array_push($links, '<a title="' . __('Spread the word!', PTS_LANG_CODE) . '" href="https://www.facebook.com/sharer/sharer.php?u=' . urlencode($mainLink) . '" target="_blank" class="dashicons-before dashicons-facebook-alt"></a>');
  //    array_push($links, '<a title="' . __('Spread the word!', PTS_LANG_CODE) . '" href="https://twitter.com/home?status=' . urlencode($twitterStatus) . '" target="_blank" class="dashicons-before dashicons-twitter"></a>');
  //    array_push($links, '<a title="' . __('Spread the word!', PTS_LANG_CODE) . '" href="https://plus.google.com/share?url=' . urlencode($mainLink) . '" target="_blank" class="dashicons-before dashicons-googleplus"></a>');
  //    return $links;
  // }
  public function initMenu()
  {
    $mainCap = $this->getMainCap();
    $mainSlug = dispatcherPts::applyFilters('adminMenuMainSlug', $this->_mainSlug);
    $mainMenuPageOptions = [
      'page_title' => PTS_WP_PLUGIN_NAME,
      'menu_title' => PTS_WP_PLUGIN_NAME,
      'capability' => $mainCap,
      'menu_slug' => $mainSlug,
      'function' => [framePts::_()->getModule('options'), 'getAdminPage'],
    ];
    $mainMenuPageOptions = dispatcherPts::applyFilters('adminMenuMainOption', $mainMenuPageOptions);
    add_menu_page($mainMenuPageOptions['page_title'], $mainMenuPageOptions['menu_title'], $mainMenuPageOptions['capability'], $mainMenuPageOptions['menu_slug'], $mainMenuPageOptions['function'], 'dashicons-tickets-alt');
    $tabs = framePts::_()->getModule('options')->getTabs();
    $subMenus = [];
    foreach ($tabs as $tKey => $tab) {
      if ((isset($tab['hidden']) && $tab['hidden']) || (isset($tab['hidden_for_main']) && $tab['hidden_for_main']) || (isset($tab['is_main']) && $tab['is_main'])) {
        continue;
      }
      $subMenus[] = [
        'title' => $tab['label'],
        'capability' => $mainCap,
        'menu_slug' => 'admin.php?page=' . $mainSlug . '&tab=' . $tKey,
        'function' => '',
      ];
    }
    $subMenus = dispatcherPts::applyFilters('adminMenuOptions', $subMenus);
    foreach ($subMenus as $opt) {
      add_submenu_page($mainSlug, $opt['title'], $opt['title'], $opt['capability'], $opt['menu_slug'], $opt['function']);
    }
  }
  public function getMainLink()
  {
    return uriPts::_([
      'baseUrl' => admin_url('admin.php'),
      'page' => $this->getMainSlug(),
    ]);
  }
  public function getMainSlug()
  {
    return $this->_mainSlug;
  }
  public function getMainCap()
  {
    return dispatcherPts::applyFilters('adminMenuAccessCap', $this->_mainCap);
  }
}
