<?php
// Silence may be golden, but sometimes words paint the canvas of understanding.

namespace Plugli\Linksy\SearchAndReplace\Core;

use Exception;
use Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\Utils;
use Plugli\Linksy\SearchAndReplace\Admin\Admin;
use Plugli\Linksy\SearchAndReplace\Helpers\Migrations;
use Linksy\SearchAndReplace\Plugli\Standard\Core\Init as _Init;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Notices;

/**
 * The core plugin class.
 * Defines internationalization, admin-specific hooks, and public-facing site hooks.
 *
 * @since  1.0.0
 * @author Plugli Team
 */
class Init extends _Init
{
    private $plugin_admin = null;
    private $flush_duplicate_keywords = null;

    public function setup()
    {
        $this->plugin_name = LINKSYAI_SAR_PLUGIN_NAME;
        $this->version = LINKSYAI_SAR_PLUGIN_VERSION;
        $this->plugin_basename = LINKSYAI_SAR_PLUGIN_BASENAME;
        $this->plugin_text_domain = LINKSYAI_SAR_PLUGIN_TEXT_DOMAIN;

        $this->plugin_admin = new Admin(
            $this->plugin_name,
            $this->version,
            $this->plugin_text_domain
        );
    }


    /**
     * Register all of the hooks related to the admin area functionality
     * of the plugin.
     *
     * @access private
     */
    protected function define_admin_hooks()
    {
        $this->loader->add_action('admin_enqueue_scripts', $this->plugin_admin, 'enqueue_styles');
        $this->loader->add_action('admin_enqueue_scripts', $this->plugin_admin, 'enqueue_scripts');

        $this->loader->add_action('admin_menu', $this->plugin_admin, 'add_plugin_admin_menu');

        if (Utils::is_plugin_active('linksy/linksy.php')) {
            deactivate_plugins('linksy-search-and-replace/linksy-search-and-replace.php');

            Notices::instance(
                '
                    <strong>Linksy Search and Replace</strong> has been disabled because <strong>Linksy</strong> is active.
                ',
                'info',
                true
            );
        }
    }

    public function handle_plugin_loaded()
    {
        parent::handle_plugin_loaded();

        if ($this->plugin_admin->is_current_page() && Migrations::has_migrations()) {
            try {
                Migrations::create_tables();
            } catch (Exception $e) {
                Notices::instance(
                    '
                        <strong>Linksy Search and Replace</strong> needs an important database upgrade.
                        <a id="linksy-db-migrator" href="javascript:;">Update now</a>.
                    ',
                    'info'
                );
            }
        }
    }
}
