<?php

if (!defined('ABSPATH')) {
    exit;
}

class Zbr_Frontend_Includes
{
    private static $scripts = array();

    private static $styles = array();

    public static function init()
    {
        add_action('wp_enqueue_scripts', array(__CLASS__, 'load_scripts'));
    }

    public static function load_scripts()
    {
        self::register_scripts();
        self::register_styles();

        // TODO: find out if we are in a News post, than only enqueue photoswipe
        self::enqueue_script('photoswipe-ui-default');
        self::enqueue_style('photoswipe-default-skin');
        add_action('wp_footer', 'zbr_photoswipe');

        self::enqueue_script( 'zbr-single-news' );
    }

    private static function register_scripts()
    {
        $register_scripts = array(
            'photoswipe' => array(
                'src' => self::get_asset_url('assets/js/photoswipe/photoswipe.js'),
                'deps' => array(),
                'version' => '4.1.1',
            ),
            'photoswipe-ui-default' => array(
                'src' => self::get_asset_url('assets/js/photoswipe/photoswipe-ui-default.js'),
                'deps' => array('photoswipe'),
                'version' => '4.1.1',
            ),
            'zbr-single-news' => array(
                'src' => self::get_asset_url('assets/js/frontend/single-news.js'),
                'deps' => array('jquery'),
                'version' => ZBR_VERSION
            )
        );
        foreach ($register_scripts as $name => $props) {
            self::register_script($name, $props['src'], $props['deps'], $props['version']);
        }
    }

    private static function register_styles()
    {
        $version = ZBR_VERSION;

        $register_styles = array(
            'photoswipe' => array(
                'src' => self::get_asset_url('assets/css/photoswipe/photoswipe.css'),
                'deps' => array(),
                'version' => $version,
                'has_rtl' => false,
            ),
            'photoswipe-default-skin' => array(
                'src' => self::get_asset_url('assets/css/photoswipe/default-skin/default-skin.css'),
                'deps' => array('photoswipe'),
                'version' => $version,
                'has_rtl' => false,
            ),
        );

        foreach ($register_styles as $name => $props) {
            self::register_style($name, $props['src'], $props['deps'], $props['version'], 'all', $props['has_rtl']);
        }
    }

    private static function register_script($handle, $path, $deps = array('jquery'), $version = ZBR_VERSION, $in_footer = true)
    {
        self::$scripts[] = $handle;
        wp_register_script($handle, $path, $deps, $version, $in_footer);
    }

    private static function register_style($handle, $path, $deps = array(), $version = ZBR_VERSION, $media = 'all', $has_rtl = false)
    {
        self::$styles[] = $handle;
        wp_register_style($handle, $path, $deps, $version, $media);

        if ($has_rtl) {
            wp_style_add_data($handle, 'rtl', 'replace');
        }
    }

    private static function get_asset_url($path)
    {
        return apply_filters('zbr_get_asset_url', plugins_url($path, ZBR_PLUGIN), $path);
    }

    private static function enqueue_script($handle, $path = '', $deps = array('jquery'), $version = ZBR_VERSION, $in_footer = true)
    {
        if (!in_array($handle, self::$scripts, true) && $path) {
            self::register_script($handle, $path, $deps, $version, $in_footer);
        }
        wp_enqueue_script($handle);
    }

    private static function enqueue_style($handle, $path = '', $deps = array(), $version = ZBR_VERSION, $media = 'all', $has_rtl = false)
    {
        if (!in_array($handle, self::$styles, true) && $path) {
            self::register_style($handle, $path, $deps, $version, $media, $has_rtl);
        }
        wp_enqueue_style($handle);
    }
}

Zbr_Frontend_Includes::init();
