<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://sourcecode-agency.it/
 * @since      1.0.0
 *
 * @package    polylai
 * @subpackage polylai/includes
 */
/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    polylai
 * @subpackage polylai/includes
 * @author     Diego Imbriani <diego@sourcecode.team>
 */
class polylai {
    /**
     * The loader that's responsible for maintaining and registering all hooks that power
     * the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      polylai_Loader    $loader    Maintains and registers all hooks for the plugin.
     */
    protected $loader;

    protected $settings;

    /**
     * The unique identifier of this plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $plugin_name    The string used to uniquely identify this plugin.
     */
    protected $plugin_name;

    /**
     * The current version of the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $version    The current version of the plugin.
     */
    protected $version;

    protected $ajax;

    protected $notices;

    /**
     * Define the core functionality of the plugin.
     *
     * Set the plugin name and the plugin version that can be used throughout the plugin.
     * Load the dependencies, define the locale, and set the hooks for the admin area and
     * the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function __construct() {
        if ( defined( 'POLYAI_DEBUG' ) && POLYAI_DEBUG ) {
            $this->version = rand( 1, 10000 );
        } else {
            $this->version = POLYLAI_VERSION;
        }
        $this->plugin_name = 'ai-translator-for-polylang';
        $this->load_dependencies();
        $this->define_admin_hooks();
        // $this->define_public_hooks();
    }

    /**
     * Load the required dependencies for this plugin.
     *
     * Include the following files that make up the plugin:
     *
     * - polylai_Loader. Orchestrates the hooks of the plugin.
     * - polylai_i18n. Defines internationalization functionality.
     * - polylai_Admin. Defines all hooks for the admin area.
     * - polylai_Public. Defines all hooks for the public side of the site.
     *
     * Create an instance of the loader which will be used to register the hooks
     * with WordPress.
     *
     * @since    1.0.0
     * @access   private
     */
    private function load_dependencies() {
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-aiengine.php';
        /**
         * The class responsible for orchestrating the actions and filters of the
         * core plugin.
         */
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-loader.php';
        /**
         * The class responsible for defining internationalization functionality
         * of the plugin.
         */
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-i18n.php';
        /**
         * The class responsible for chatgpt interaction
         */
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-chatgpt.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'plugins/class-polylai-yoast.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'plugins/class-polylai-elementor.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'plugins/class-polylai-acf.php';
        /**
         * The class responsible for defining all actions that occur in the admin area.
         */
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-polylai-translator-admin.php';
        /**
         * The class responsible for defining all actions that occur in the public-facing
         * side of the site.
         */
        // require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-polylai-translator-public.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-settings.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-cron.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-utils.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-translation-request.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dto/class-polylai-translator-cloud-request.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dto/class-polylai-translator-cloud-status.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dto/class-polylai-translator-data.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dto/class-polylai-translator-translation.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-cloud.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-ajax.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-notices.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-translator.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-manager.php';
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-polylai-translator-duplicator.php';
        $this->loader = new polylai_Loader();
        $this->settings = new polylai_Settings();
        $this->ajax = new polylai_Ajax();
        $this->notices = new polylai_Notices();
    }

    /**
     * Define the locale for this plugin for internationalization.
     *
     * Uses the polylai_i18n class in order to set the domain and to register the hook
     * with WordPress.
     *
     * @since    1.0.0
     * @access   private
     */
    public function set_locale() {
        $plugin_i18n = new polylai_i18n();
        $plugin_i18n->load_plugin_textdomain();
    }

    /**
     * Register all of the hooks related to the admin area functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_admin_hooks() {
        $plugin_admin = new polylai_Admin($this->get_plugin_name(), $this->get_version());
        $this->loader->add_action( 'init', $plugin_admin, 'register_post_status' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
        $this->loader->add_action( 'admin_menu', $plugin_admin, 'admin_menu' );
        $this->loader->add_action( 'admin_init', $this->settings, 'register_settings' );
        $this->loader->add_action( 'admin_init', $plugin_admin, 'handle_rating' );
        $this->loader->add_action( 'admin_init', $plugin_admin, 'check_commands' );
        $this->loader->add_action( 'admin_notices', $this->notices, 'check_usage' );
        $this->loader->add_action( 'admin_notices', $this->notices, 'check_bg_messages' );
        $this->loader->add_filter( "manage_trip_posts_columns", $plugin_admin, 'add_column' );
        $this->loader->add_filter(
            'post_row_actions',
            $plugin_admin,
            'row_actions',
            10,
            2
        );
        $this->loader->add_filter(
            'page_row_actions',
            $plugin_admin,
            'row_actions',
            10,
            2
        );
        $post_types = get_post_types( [
            'public'   => true,
            '_builtin' => false,
        ], 'names' );
        foreach ( $post_types as $post_type ) {
            $this->loader->add_filter(
                "{$post_type}_row_actions",
                $plugin_admin,
                'row_actions',
                10,
                2
            );
        }
        $this->loader->add_action( 'wp_ajax_polylai_get_post_tr_status', $this->ajax, 'get_post_tr_status' );
        $this->loader->add_action( 'wp_ajax_polylai_enqueue_translations', $this->ajax, 'enqueue_translations' );
        $this->loader->add_action( 'wp_ajax_polylai_enqueue_bulk', $this->ajax, 'enqueue_bulk' );
        $this->loader->add_action( 'wp_ajax_polylai_logs', $plugin_admin, 'get_logs' );
        $this->loader->add_action( 'wp_ajax_polylai_logs_download', $plugin_admin, 'download_logs' );
        $this->loader->add_action( 'wp_ajax_polylai_save_credits_license', $plugin_admin, 'save_credits_license' );
        $this->loader->add_action( 'wp_ajax_polylai_translation_modal', $plugin_admin, 'show_translation_modal' );
        $this->loader->add_action( 'wp_ajax_polylai_pricing', $plugin_admin, 'show_pricing' );
        $this->loader->add_action( 'wp_ajax_polylai_history', $this->ajax, 'list_history' );
        $this->loader->add_action( 'wp_ajax_polylai_progress', $this->ajax, 'list_pending' );
        $this->loader->add_action( 'wp_ajax_polylai_trigger', $this->ajax, 'trigger' );
        $this->loader->add_action( 'wp_ajax_polylai_add_translation', $this->ajax, 'add_translation' );
        $this->loader->add_action( 'wp_ajax_polylai_stop_all_translations', $this->ajax, 'stop_all_translations' );
        $this->loader->add_action( 'wp_ajax_polylai_count_posts', $this->ajax, 'count_posts' );
        $this->loader->add_action( 'plugin_action_links_polylai-translator/polylai-translator.php', $plugin_admin, 'action_links' );
        $this->loader->add_action( 'rest_api_init', $plugin_admin, 'register_api' );
        $this->loader->add_action( 'init', $plugin_admin, 'create_translation_post_type' );
        $this->loader->add_action( 'init', $this->ajax, 'check_cloud' );
        $this->loader->add_action( 'plugins_loaded', $this, 'set_locale' );
    }

    /**
     * Run the loader to execute all of the hooks with WordPress.
     *
     * @since    1.0.0
     */
    public function run() {
        $this->loader->run();
    }

    /**
     * The name of the plugin used to uniquely identify it within the context of
     * WordPress and to define internationalization functionality.
     *
     * @since     1.0.0
     * @return    string    The name of the plugin.
     */
    public function get_plugin_name() {
        return $this->plugin_name;
    }

    /**
     * The reference to the class that orchestrates the hooks with the plugin.
     *
     * @since     1.0.0
     * @return    polylai_Loader    Orchestrates the hooks of the plugin.
     */
    public function get_loader() {
        return $this->loader;
    }

    /**
     * Retrieve the version number of the plugin.
     *
     * @since     1.0.0
     * @return    string    The version number of the plugin.
     */
    public function get_version() {
        return $this->version;
    }

}
