<?php

namespace Aldine;

use Roots\Sage\Container;
use Roots\Sage\Assets\JsonManifest;
use Roots\Sage\Template\Blade;
use Roots\Sage\Template\BladeProvider;

/**
 * Theme assets
 */
add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style(
        'aldine/webfonts',
        'https://fonts.googleapis.com/css?family=Karla:400,400i,700|Spectral:400,400i,600',
        false,
        null
    );
    wp_enqueue_style('aldine/main.css', asset_path('styles/main.css'), false, null);
    /* wp_enqueue_style(
        'uio/normalize.css',
        get_theme_file_uri() . '/lib/infusion/src/lib/normalize/css/normalize.css',
        false,
        null
    );
    wp_enqueue_style(
        'uio/fluid.css',
        get_theme_file_uri() . '/lib/infusion/src/framework/core/css/fluid.css',
        false,
        null
    );
    wp_enqueue_style(
        'uio/enactors.css',
        get_theme_file_uri() . '/lib/infusion/src/framework/preferences/css/Enactors.css',
        false,
        null
    );
    wp_enqueue_style(
        'uio/prefseditor.css',
        get_theme_file_uri() . '/lib/infusion/src/framework/preferences/css/PrefsEditor.css',
        false,
        null
    );
    wp_enqueue_style(
        'uio/separatedpanelprefseditor.css',
        get_theme_file_uri() . '/lib/infusion/src/framework/preferences/css/SeparatedPanelPrefsEditor.css',
        false,
        null
    );
    wp_enqueue_script('uio.js', get_theme_file_uri() . '/lib/infusion/infusion-uiOptions.js', ['jquery'], null, true);
    */
    wp_enqueue_script('aldine/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);
    wp_localize_script('aldine/main.js', 'SAGE_DIST_PATH', get_theme_file_uri() . '/dist/');
}, 100);

/**
 * Theme setup
 */
add_action('after_setup_theme', function () {
    /**
     * Enable plugins to manage the document title
     * @link https://developer.wordpress.org/reference/functions/add_theme_support/#title-tag
     */
    add_theme_support('title-tag');

    /**
     * Register navigation menus
     * @link https://developer.wordpress.org/reference/functions/register_nav_menus/
     */
    register_nav_menus([
        'network-footer-menu' => __('Network Footer Menu', 'aldine')
    ]);

    /**
     * Enable post thumbnails
     * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
     */
    add_theme_support('post-thumbnails');

    /**
     * Enable HTML5 markup support
     * @link https://developer.wordpress.org/reference/functions/add_theme_support/#html5
     */
    add_theme_support('html5', ['caption', 'comment-form', 'comment-list', 'gallery', 'search-form']);

    /**
     * Enable selective refresh for widgets in customizer
     * @link https://developer.wordpress.org/themes/advanced-topics/customizer-api/#theme-support-in-sidebars
     */
    add_theme_support('customize-selective-refresh-widgets');

    /**
     * Use main stylesheet for visual editor
     * @see resources/assets/styles/layouts/_tinymce.scss
     */
    add_editor_style(asset_path('styles/main.css'));
}, 20);

/**
 * Register sidebars
 */
add_action('widgets_init', function () {
    $config = [
        'before_widget' => '<section class="widget %1$s %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="tc ttu">',
        'after_title'   => '</h3>'
    ];
    register_sidebar([
        'name'          => __('Front Page Content', 'aldine'),
        'id'            => 'front-page-block',
        'before_widget' => '<section class="widget %1$s %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="tc ttu">',
        'after_title'   => '</h3>'
    ]);
    register_sidebar([
        'name'          => __('Network Footer Block 1', 'aldine'),
        'id'            => 'network-footer-block-1'
    ] + $config);
    register_sidebar([
        'name'          => __('Network Footer Block 2', 'aldine'),
        'id'            => 'network-footer-block-2'
    ] + $config);
});

add_action('widgets_init', function () {
    foreach ([
        'WP_Widget_Pages',
        'WP_Widget_Calendar',
        'WP_Widget_Archives',
        'WP_Widget_Links',
        'WP_Widget_Media_Audio',
        'WP_Widget_Meta',
        'WP_Widget_Search',
        'WP_Widget_Categories',
        'WP_Widget_Recent_Posts',
        'WP_Widget_Recent_Comments',
        'WP_Widget_RSS',
        'WP_Widget_Tag_Cloud'
    ] as $widget) {
        unregister_widget($widget);
    }
});

/**
 * Updates the `$post` variable on each iteration of the loop.
 * Note: updated value is only available for subsequently loaded views, such as partials
 */
add_action('the_post', function ($post) {
    sage('blade')->share('post', $post);
});

/**
 * Setup Sage options
 */
add_action('after_setup_theme', function () {
    /**
     * Add JsonManifest to Sage container
     */
    sage()->singleton('sage.assets', function () {
        return new JsonManifest(config('assets.manifest'), config('assets.uri'));
    });

    /**
     * Add Blade to Sage container
     */
    sage()->singleton('sage.blade', function (Container $app) {
        $cachePath = config('view.compiled');
        if (!file_exists($cachePath)) {
            wp_mkdir_p($cachePath);
        }
        (new BladeProvider($app))->register();
        return new Blade($app['view']);
    });

    /**
     * Create @asset() Blade directive
     */
    sage('blade')->compiler()->directive('asset', function ($asset) {
        return "<?= " . __NAMESPACE__ . "\\asset_path({$asset}); ?>";
    });
});

add_action('wp_head', function () {
    $primary = get_option('pb_network_primary_color');
    $secondary = get_option('pb_network_secondary_color');
    if ($primary || $secondary) { ?>
        <style type="text/css">
            <?php if ($primary) { ?>
                a { color: <?= $primary ?>; }
                .primary { color: <?= $primary ?>; }
                .bg-primary { background-color: <?= $primary ?>; }
                .b--primary { border-color: <?= $primary ?>; }
                .button.button-primary {
                  border-color: <?= $primary ?>;
                  background: <?= $primary ?>;
                }
                .button.button-primary:hover,
                .button.button-primary:focus {
                  color: <?= $primary ?>;
                }
                .button.button-primary.button-outline {
                  color: <?= $primary ?>;
                }
                .button.button-primary:hover,
                .button.button-primary:focus {
                  background: <?= $primary ?>;
                }
                @media (min-width: 960px) {
                  .home .main > .block-2,
                  .home .one-two + .block {
                    border-color: <?= $primary ?>;
                  }
                }
            <?php }
if ($secondary) { ?>
                .secondary { color: <?= $secondary ?>; }
                .bg-secondary { background-color: <?= $secondary ?>; }
                .b--secondary {
                  border-color: <?= $secondary ?>;
                }
                .button.button-secondary {
                  border-color: <?= $secondary ?>;
                  background: <?= $secondary ?>;
                }
                .button.button-secondary:hover,
                .button.button-secondary:focus {
                  color: <?= $secondary ?>;
                }
                .button.button-secondary.button-outline {
                  color: <?= $secondary ?>;
                }
                .button.button-secondary:hover,
                .button.button-secondary:focus {
                  background: <?= $secondary ?>;
                }
                .home .block h3::before {
                  background-color: <?= $secondary ?>;
                }
                .home .one-two .block-2 {
                  background: <?= $secondary ?>;
                }
<?php } ?>
        </style>
    <?php }
});