<?php
/*
 * Plugin Name:		GemBoards
 * Plugin URI:		https://gemboards.com
 * Description:		Project management software
 * Version:			1.2.0
 * Author:			Kodezen Limited
 * Author URI:		https://kodezen.com
 * License:			GPL-3.0+
 * License URI:		http://www.gnu.org/licenses/gpl-3.0.txt
 * Text Domain:		gemboards
 * Domain Path:		/languages/
 */

use GemBoards\API;
use GemBoards\GemboardsCLI;
use GemBoards\hooks\Activity;
use GemBoards\hooks\Board;
use GemBoards\hooks\Card;
use GemBoards\hooks\Dashboard;
use GemBoards\hooks\EmailNotifications;
use GemBoards\hooks\Menu;
use GemBoards\hooks\UserInvitation;
use GemBoards\Installer;
use GemBoards\Utils\Helper;

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

final class GemBoards {

	private function __construct() {
		$this->define_constants();
		$this->set_global_settings();
		$this->load_dependency();
		Installer::init();
		register_activation_hook( __FILE__, [ $this, 'activate' ] );
		register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
		add_action( 'plugins_loaded', [ $this, 'on_plugins_loaded' ] );
		add_action( 'gemboards_loaded', [ $this, 'init_plugin' ] );
	}

	public static function init() {
		static $instance = false;

		if ( ! $instance ) {
			$instance = new self();
		}

		return $instance;
	}

	public function define_constants() {
		/**
		 * Defines CONSTANTS for Whole plugins.
		 */
		define( 'GEMBOARDS_VERSION', '1.2.0' );
		define( 'GEMBOARDS_SETTINGS_NAME', 'gemboards_settings' );
		define( 'GEMBOARDS_PLUGIN_FILE', __FILE__ );
		define( 'GEMBOARDS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
		define( 'GEMBOARDS_PLUGIN_SLUG', 'gemboards' );
		define( 'GEMBOARDS_PLUGIN_ROOT_URI', plugins_url( '/', __FILE__ ) );
		define( 'GEMBOARDS_ROOT_DIR_PATH', plugin_dir_path( __FILE__ ) );
		define( 'GEMBOARDS_INCLUDES_DIR_PATH', GEMBOARDS_ROOT_DIR_PATH . 'includes/' );
		define( 'GEMBOARDS_TEMPLATES_DIR_PATH', GEMBOARDS_ROOT_DIR_PATH . 'templates/' );
		define( 'GEMBOARDS_ASSETS_DIR_PATH', GEMBOARDS_ROOT_DIR_PATH . 'assets/' );
		define( 'GEMBOARDS_ADDONS_DIR_PATH', GEMBOARDS_ROOT_DIR_PATH . 'addons/' );
		define( 'GEMBOARDS_ASSETS_URI', GEMBOARDS_PLUGIN_ROOT_URI . 'assets/' );
		define( 'GEMBOARDS_TEMPLATE_DEBUG_MODE', false );
	}

	/**
	 * When WP has loaded all plugins, trigger the `gemboards_loaded` hook.
	 *
	 * This ensures `gemboards_loaded` is called only after all other plugins
	 * are loaded, to avoid issues caused by plugin directory naming changing
	 *
	 * @since 1.0.0
	 */
	public function on_plugins_loaded() {
		do_action( 'gemboards_loaded' );
	}

	/**
	 * Initialize the plugin
	 *
	 * @return void
	 */
	public function init_plugin() {
		// Init action.
		do_action( 'gemboards_before_init' );

		/**
		 * Gemboards CLI - Only for development / testing purpose.
		 *
		 * You've to run `composer install` for Gemboards generation CLI.
		 * To download composer, visit https://getcomposer.org/download/
		 * For composer.json, visit https://github.com/kodezen/gemboards/blob/main/composer.json
		 *
		 * @see https://github.com/kodezen/gemboards
		 */
		if ( defined( 'WP_CLI' ) && WP_CLI && file_exists( GEMBOARDS_ROOT_DIR_PATH . '/vendor/autoload.php' ) ) {
			try {
				WP_CLI::add_command( 'gemboards', GemboardsCLI::class, [
					'shortdesc' => __( 'Generate cards, boards and projects for development or testing purpose.', 'gemboards' ),
				] );
			} catch ( Exception $e ) {
				Helper::error_log( $e->getMessage() );
			}
		}

		$this->dispatch_hooks();
		// Init action.
		do_action( 'gemboards_init' );
	}

	public function dispatch_hooks() {
		GemBoards\RewriteRules::init();
		GemBoards\Frontend::init();
		GemBoards\Assets::init();
		GemBoards\Ajax::init();
		API::init();
		GemBoards\Database::init();
		GemBoards\Admin::init();

		// @TODO: make Hooks class and move this to it.
		Activity::init();
		Menu::init();
		Dashboard::init();
		Board::init();
		UserInvitation::init();
		Card::init();
		EmailNotifications::init();
	}

	public function set_global_settings() {
		$GLOBALS['gemboards_settings'] = json_decode( get_option( GEMBOARDS_SETTINGS_NAME, '{}' ) );
	}

	public function load_dependency() {
		require_once GEMBOARDS_INCLUDES_DIR_PATH . 'autoload.php';
		require_once GEMBOARDS_INCLUDES_DIR_PATH . 'functions.php';
		require_once GEMBOARDS_INCLUDES_DIR_PATH . 'hooks.php';
	}

	public function activate() {
		GemBoards\Installer::install();
	}

	public function deactivate() {
	}

}

/**
 * Initializes the main plugin
 *
 * @return GemBoards
 */
function gemboards_start() {
	return GemBoards::init();
}

// Plugin Start
gemboards_start();
