<?php

/**
 * Plugin Name:       Booktics - Booking Calendar for Appointments and Service Businesses
 * Plugin URI:        https://arraytics.com/booktics/
 * Description:       Schedule, Appointment and Seat Booking plugin.
 * Version:           1.0.20
 * Requires at least: 5.2
 * Requires PHP:      7.4
 * Author:            Arraytics
 * Author URI:        https://arraytics.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       booktics
 * Domain Path:       /languages

 * Booktics is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.

 * Booktics Essential is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Booktics. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package Booktics
 * @category Core
 * @author Arraytics
 * @version 1.0.20
 */

use Booktics\Booktics;
use Booktics\Providers\Global_Service_Provider;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;


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


// Define constant for the Plugin file.
define( 'BOOKTICS_PLUGIN_NAME', "Booktics");
defined( 'BOOKTICS_FILE' ) || define( 'BOOKTICS_FILE', __FILE__ );
defined( 'BOOKTICS_DIR' ) || define( 'BOOKTICS_DIR', __DIR__ );
defined( 'BOOKTICS_VERSION' ) || define( 'BOOKTICS_VERSION', '1.0.20' );

global $booktics_container;

$booktics_container = new \Booktics\Container\Container();

$booktics_container->addServiceProvider( 'global', Global_Service_Provider::class );

/**
 * Booktics container
 *
 * @return  \Booktics\Container\Container
 */
function booktics_container() {
    global $booktics_container;

    return $booktics_container;
}

/**
 * Registers the uninstaller form backend.
 * 
 * @return void
 */
function booktics_register_uninstaller_form() {
    try {
        if ( class_exists( 'UninstallerForm\UninstallerForm' ) && is_callable( [ '\UninstallerForm\UninstallerForm', 'init' ] ) ) {
            $reflection  = new ReflectionMethod( '\UninstallerForm\UninstallerForm', 'init' );
            $totalParams = $reflection->getNumberOfParameters(); // Maximum number of parameters allowed

            if( 6 === $totalParams ) {
                $uninstaller_form = new \UninstallerForm\UninstallerForm();
                $uninstaller_form->init(
                    'Booktics',         // Plugin name
                    'booktics',         // Plugin Slug
                    __FILE__,
                    'booktics',   // Text Domain Name
                    'booktics-feedback-modal',    // plugins-admin-script-handler
                    'https://arraytics.com/?fluentcrm=1&route=contact&hash=09343e4f-30f7-4bda-b8b6-d4b6f90754ad' // web hook for booktics
                );
            } else {
                // For compatibility with older versions of uninstaller form
                $uninstaller_form = new \UninstallerForm\UninstallerForm();
                $uninstaller_form->init(
                    'Booktics',         // Plugin name
                    'booktics',         // Plugin Slug
                    __FILE__,
                    'booktics',   // Text Domain Name
                    'booktics-feedback-modal' 
                );
            }
        }
    } catch ( \Throwable $e ) {
        if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            error_log(
                sprintf(
                    'Booktics: Error in initiating uninstaller form – %s (Line %d, File %s)',
                    $e->getMessage(),
                    $e->getLine(),
                    $e->getFile()
                )
            );
        }
    }
}

/**
 * Add a "Go Pro" action link on the Plugins page.
 * Hidden when the pro plugin is active.
 */
if ( ! function_exists( 'booktics_add_go_pro_action_link' ) ) {
    function booktics_add_go_pro_action_link( $links ) {
        if ( function_exists( 'booktics_pro' ) ) {
            return $links;
        }

        $go_pro = sprintf(
            '<a href="%1$s" target="_blank" rel="noopener noreferrer" style="color:#e8364c;font-weight:600;">%2$s</a>',
            esc_url( 'https://arraytics.com/booktics/pricing/' ),
            esc_html__( 'Go Pro', 'booktics' )
        );

        $links[] = $go_pro;
        return $links;
    }
}
add_filter( 'plugin_action_links_' . plugin_basename( BOOKTICS_FILE ), 'booktics_add_go_pro_action_link' );



/**
 * Load Booktics Plugin when all plugins loaded.
 *
 * @return Booktics The singleton instance of Booktics.
 */
function booktics() {
    add_action( 'admin_init', 'booktics_handle_conflicting_plugins' );
    add_action( 'admin_notices', 'booktics_show_conflict_deactivation_notice' );
    booktics_register_uninstaller_form();
    return Booktics::instance();
}

// Let's go.
booktics();
