<?php namespace Webien\Setup;

class General
{
    static function init()
    {
        add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_scripts'], 9999);

        // Styles/Scripts for admin/elementor editor
        add_action('elementor/editor/before_enqueue_scripts', [__CLASS__, 'enqueue_elementor_admin_scripts']);
        add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_elementor_admin_scripts']);

        add_action( 'wp_print_styles', [__CLASS__, 'deregister_styles'], 99999 );

        add_action( 'init', [__CLASS__, 'add_elementor_widget_cat'], 99999 );
    }

    static function on_activation()
    {

    }


    // Check for local development, will affect for example s3 upload and therefore image optimization plugin.
    public static function isDev(){
        if(defined('WP_ENV') && WP_ENV === 'development') return true;
        if($_SERVER['REMOTE_ADDR'] === '127.0.0.1') return true;
        if(strpos($_SERVER['SERVER_NAME'], '.test') !== false) return true;
        if(strpos($_SERVER['SERVER_NAME'], '.dev') !== false) return true;
        return false;
    }


    static function enqueue_scripts()
    {
        $ver = WEBIEN_PLUGIN_VERSION;

        wp_enqueue_style('webien-setup', WEBIEN_PLUGIN_URL . 'dist/style.css', [], $ver, 'all');
        wp_enqueue_script('webien-waypoints',  'https://cdnjs.cloudflare.com/ajax/libs/waypoints/3.1.1/noframework.waypoints.min.js', ['jquery'], $ver, true);
        wp_enqueue_script('webien-setup',  WEBIEN_PLUGIN_URL . 'dist/script.js', ['jquery'], $ver, true);
    }

    static function enqueue_elementor_admin_scripts()
    {
        wp_enqueue_script('webien-setup-admin',  WEBIEN_PLUGIN_URL . 'dist/admin.js', [], false, true);
        wp_enqueue_style('webien-setup', WEBIEN_PLUGIN_URL . 'dist/admin.css', [], time());
    }


    static function deregister_styles(){
        if(!is_user_logged_in()){
            wp_deregister_style( 'dashicons' );
        }

    }


    public static function add_elementor_widget_cat(){
        if (!defined('WP_INSTALLING') && did_action('elementor/loaded') && class_exists('acf')) {
            \Elementor\Plugin::instance()
                ->elements_manager
                ->add_category('webien', ['title'=>'Webien']);
        }
    }


}
