<?php

declare(strict_types=1);

/*
Plugin Name: Kickflip Product Configurators
Plugin URI: http://plugins.svn.wordpress.org/mycustomizer-woocommerce-connector/
description: Build an outstanding product configurator for your ecommerce
Version: 1.0.47
Author: Kickflip
Author URI: https://gokickflip.com/
License: GPL3
 */
// This is executed on install only
require __DIR__ . '/src/Install/MczrInstall.php';
$vendor = __DIR__ . '/vendor';
$install = new \MyCustomizer\WooCommerce\Connector\Install\MczrInstall();

require_once __DIR__ . '/vendor/autoload.php';

use MyCustomizer\WooCommerce\Connector\Api\MczrShopApi;
use MyCustomizer\WooCommerce\Connector\Controller\Admin\MczrOrderController as MczrOrderControllerAdmin;
use MyCustomizer\WooCommerce\Connector\Controller\Admin\MczrSettingController;
use MyCustomizer\WooCommerce\Connector\Controller\MczrAssetController;
use MyCustomizer\WooCommerce\Connector\Controller\MczrCartController;
use MyCustomizer\WooCommerce\Connector\Controller\MczrIframeController;
use MyCustomizer\WooCommerce\Connector\Controller\MczrOrderController;
use MyCustomizer\WooCommerce\Connector\Controller\MczrSocialController;
use MyCustomizer\WooCommerce\Connector\Controller\MczrTemplateController;
use MyCustomizer\WooCommerce\Connector\Libs\MczrFlashMessage;
use MyCustomizer\WooCommerce\Connector\MczrPlugin;
use MyCustomizer\WooCommerce\Connector\Types\WC_Product_Mczr;

if (!defined('ABSPATH')) {
    exit('You cannot access this file directly (ABSPATH is not defined)');
}

/**
 * @throws Exception
 */
function initMczrPlugin(): void
{
    try {
        $plugin = new MczrPlugin();

        \register_activation_hook(__FILE__, [$plugin, 'onActivate']);

        // Could not init : TODO : log
        if (!in_array('woocommerce/woocommerce.php', \apply_filters('active_plugins', \get_option('active_plugins')))) {
            return;
        }
        // GENERAL
        \add_action('plugins_loaded', [$plugin, 'init']);
        \add_action('plugins_loaded', [new MczrCartController(), 'init']);

        // ADMIN
        \add_action('plugins_loaded', [new MczrFlashMessage(), 'init']);
        \add_action('plugins_loaded', [new MczrShopApi(), 'init']);
        \add_action('plugins_loaded', [new MczrAssetController(), 'init']);
        \add_action('plugins_loaded', [new MczrIframeController(), 'init']);
        \add_action('plugins_loaded', [new MczrOrderController(), 'init']);
        \add_action('plugins_loaded', [new MczrTemplateController(), 'init']);
        \add_action('plugins_loaded', [new MczrSocialController(), 'init']);

        // FRONT
        \add_action('plugins_loaded', [new MczrOrderControllerAdmin(), 'init']);
        \add_action('plugins_loaded', [new MczrSettingController(), 'init']);

        // Woocommerce needs to be loaded first, then load WC_Product_Mczr
        \add_action(
            'plugins_loaded',
            function () {
                $load = new WC_Product_Mczr();
                $load->init();
            }
        );
    } catch (Exception $ex) {
        error_log($ex->getMessage());
        echo \esc_attr($ex->getMessage());

        exit;
    }
}

initMczrPlugin();
