<?php
/**
 * Developer: Iaax Page (twitter.com/iaaxpage)
 * Date: 9/9/15, 8:42 AM
 * Generator: PhpStorm
 */

namespace OctopusSignatureClient\Plugin;


use OctopusSignatureClient\WordPress\CustomAPI;
use OctopusSignatureClient\Plugin\UI;


class Setup
{

    public function __construct()
    {
        // Calls a static method to register filters before functions.php
        add_filter('init', array( &$this, 'initFilterActions' ));
        $this->registerTextDomain();
    }

    public function initFilterActions()
    {
        add_filter('wp_footer', array( __CLASS__, 'renderSignatureHTML' ));

        $this->registerStyles();

        // Setup API Endpoints
        new CustomAPI();
    }

    public function registerTextDomain()
    {
        add_action('after_setup_theme', array( $this, 'registerPluginTextDomain' ));
        return $this;
    }

    public function registerPluginTextDomain()
    {
        if( UI::getResourceDirectory('', 'languages') ) {
            load_plugin_textdomain('octopus-signature-client', false, plugin_basename( dirname( __FILE__ ) ) . '/../../resources/languages');
        }
    }

    private function registerStyles()
    {
        if( $styleURL = get_site_option('signature_style_url') ) {
            // Enqueue the style baby!
            wp_enqueue_style('octopus-signature-css', $styleURL);
        }

        return $this;
    }

    // Static methods

    private static function getArraySubset( $completeSet, $key, $value )
    {
        $subset = array();

        foreach( $completeSet as $index => $item ) {

            if( !is_array($item) ) {
                $item = (array)$item;
            }

            if( in_array($key, array_keys($item)) && $item[$key] == $value ) {
                $subset[] = $item;
            }
        }

        return $subset;
    }


    public static function renderSignatureHTML()
    {
        $targetURL       = $anchorText = '';
        $linkRel         = '';
        $currentURLLevel = 0;
        if( $linkList = get_site_option('signature_pages_list') ) {

            $levels = explode('/', untrailingslashit($_SERVER['REQUEST_URI']));
            if( $levels[0] == '' ) {
                unset( $levels[0] );
            }
            $currentURLLevel = ( count($levels) > 1 ? count($levels) - 1 : 0 );

            if( $currentURLLevel > count($linkList) ) {
                $currentURLLevel = count($linkList) - 1;
            }

            $links = self::getArraySubset((array)$linkList, 'pageDepth', $currentURLLevel);

            if( count($links) < 1 ) {
                if( $currentURLLevel > 0 ) {
                    // Default to the first position of the list if there are no appropriate links
                    $links = self::getArraySubset((array)$linkList, 'pageDepth', 0);
                    if( count($links) < 1 ) {
                        // If there's nothing. A plus tard;
                        return false;
                    }
                }
            }

            $rand = 0;
            if( count($links) > 1 ) {
                $rand = rand(0, ( count($links) - 1 ));
            }
            $targetURL  = $links[$rand]['targetURL'];
            $anchorText = $links[$rand]['anchorText'];
            $linkRel    = ( empty( $links[$rand]['linkREL'] ) ? '' : " rel='{$links[$rand]['linkREL']}' " );
        } else {
            $targetURL  = 'http://octopus.mx/';
            $anchorText = __('Agencia de Marketing Digital', 'octopus-signature-client');
        }
        ?>

        <div class="octopusSignatureWrapper">
            <div id="octopus_advanced_signature" class="octopus advancedSignature">
                <p>
                    <?php _e('Diseñado por Octopus', 'octopus-signature-client'); ?>
                    <a href="<?php echo $targetURL; ?>" <?php echo $linkRel; ?>
                       target="_blank"><?php echo $anchorText; ?></a>
                    <?php _e('en Cancún, México', 'octopus-signature-client'); ?>
                </p>
            </div>
        </div>

        <?php
        // Remove the filter for safety
        remove_filter('wp_footer', array( __CLASS__, 'renderSignatureHTML' ));

        return true;
    }

}