<?php

namespace Fab\Plugin;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Interfaces\Model_Interface;
use Fab\Interfaces\Initiable_Interface;

/**
 * Constants
 *
 * @package    Fab
 * @subpackage Fab\Includes
 */
class Constants implements Model_Interface, Initiable_Interface {

    // Load traits.
    use \Fab\Plugin\Helper\Directory;
    use \Fab\Plugin\Helper\Singleton;

    /**
     * Add constants on rest api
     *
     * @return void
     */
    public function rest_api_constants() {
        // We add this check because, on backend templates page rest_api_init hook is not called.
        // this could be coming from WP error, but for now this check is require to initiate API endpoint.
        if ( ! defined( 'FAB_REST_API_USER_IS_ADMIN' ) ) {
            define( 'FAB_REST_API_USER_IS_ADMIN', current_user_can( 'manage_options' ) );
        }
    }

    /**
     * Initiate constants
     * - This function contains all static constants used in the plugin.
     *
     * @return void
     */
    public function initialize() {
        define( 'ARTISTUDIO_URL', 'https://artistudio.xyz/' );
        define( 'TEMPLATEHUB_URL', 'https://templatehub.artistudio.xyz/' );

        /**
         * Third Party Integrations
         *
         * NOTE:
         * - We define these constants here because we need to check if the plugin is active before using it.
         * - We can't use $controller::getInstance()->get_plugin_file() because it'll cause racing encapsulation.
         */
        define( 'FAB_CONTACT_FORM_7_PLUGIN_FILE', 'contact-form-7/wp-contact-form-7.php' );
        define( 'FAB_WPFORMS_LITE_PLUGIN_FILE', 'wpforms-lite/wpforms.php' );
        define( 'FAB_WOOCOMMERCE_PLUGIN_FILE', 'woocommerce/woocommerce.php' );
        define( 'FAB_DATAAGENT_PLUGIN_FILE', 'dataagent/dataagent.php' );

        /**
         * Check if the plugin is active
         * - We use this to check if the plugin is active before using it.
         * - This will be act as cache to avoid calling is_plugin_active() on every plugin load.
         */
        define( 'FAB_CONTACT_FORM_7_PLUGIN_ACTIVE', is_plugin_active( FAB_CONTACT_FORM_7_PLUGIN_FILE ) );
        define( 'FAB_WPFORMS_LITE_PLUGIN_ACTIVE', is_plugin_active( FAB_WPFORMS_LITE_PLUGIN_FILE ) );
        define( 'FAB_WOOCOMMERCE_PLUGIN_ACTIVE', is_plugin_active( FAB_WOOCOMMERCE_PLUGIN_FILE ) );
        define( 'FAB_DATAAGENT_PLUGIN_ACTIVE', is_plugin_active( FAB_DATAAGENT_PLUGIN_FILE ) );
    }

    /**
     * Initiate constants
     * - This function contains all dynamic constants used in the plugin.
     * - These constants are defined on init hook.
     *
     * @return void
     */
    public function initiate_constants() {
        // Conditional Constants.
        try {
            $screen = wp_json_encode( \FAB()->Helper->getScreen() );
        } catch ( \Exception $e ) {
            $screen = wp_json_encode( array() );
        }
        define( 'FAB_SCREEN', $screen );
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * Run functions
     *
     * @return void
     */
    public function run() {
        // Add constants on rest api.
        add_action( 'rest_api_init', array( $this, 'rest_api_constants' ) );

        // Add constants on init.
        add_action( 'init', array( $this, 'initiate_constants' ) );
    }
}
