<?php
/**
 * Booktics Main Class
 *
 * @package Booktics/Booktics
 */

namespace Booktics;

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

use Booktics\Container\Container;

/**
 * Class Booktics
 */
class Booktics {

    /**
     * Plugin version.
     *
     * @var string
     */
    public $version;

    /**
     * Plugin file.
     *
     * @var string
     */
    public $plugin_file;

    /**
     * Plugin directory.
     *
     * @var string
     */
    public $plugin_directory;

    /**
     * @var string
     */
    public $build_url;

    /**
     * Plugin base name.
     *
     * @var string
     */
    public $basename;

    /**
     * Plugin text directory path.
     *
     * @var string
     */
    public $text_domain_directory;

    /**
     * Plugin text directory path.
     *
     * @var string
     */
    public $template_directory;

    /**
     * Plugin assets directory path.
     *
     * @var string
     */
    public $assets_url;

    /**
     * Plugin url.
     *
     * @var string
     */
    public $plugin_url;

    /**
     * Container that holds all the services.
     *
     * @var Container
     */
    public $container;

    /**
     * Booktics Constructor.
     */
    public function __construct() {
        $this->init();
        add_action( 'init', array( $this, 'init_classes' ) );
    }

    /**
     * Initialize the plugin.
     *
     * @return void
     */
    protected function init(): void {
        $this->version               = BOOKTICS_VERSION;
        $this->plugin_file           = BOOKTICS_FILE;
        $this->plugin_directory      = BOOKTICS_DIR;
        $this->basename              = plugin_basename( $this->plugin_file );
        $this->text_domain_directory = $this->plugin_directory . '/languages';
        $this->template_directory    = $this->plugin_directory . '/templates';
        $this->plugin_url            = plugins_url( '', $this->plugin_file );
        $this->assets_url            = $this->plugin_url . '/assets';
        $this->build_url             = $this->plugin_url . '/build';
        register_activation_hook(
            $this->plugin_file, array(
                $this,
                'activate',
            )
        );
        register_deactivation_hook(
            $this->plugin_file, array(
                $this,
                'deactivate',
            )
        );
    }

    /**
     * Init classes
     *
     * @return  void
     */
    public function init_classes() {
        if ( ! session_id() ) {
            session_start();
        }
        self::register_custom_post_type();

        ( new \Booktics\Base\Services\Post_Status_Service() )->register();

        $service_provider = $this->get_container()->get( 'global' );

        // Initilize Plugin Banner Notice.
        Plugin_Notice::run();

        self::version_check();
    }

    /**
     * Get container
     *
     * @return  Container
     */
    public function get_container() {
        return booktics_container();
    }

    /**
     * Initializes the Booktics class.
     *
     * Checks for an existing WeMeal instance
     * and if it doesn't find one, creates it.
     *
     * @return Booktics
     */
    public static function instance(): Booktics {
        static $instance = false;

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

        return $instance;
    }

    /**
     * Trigger plugin activation class
     *
     * @return void
     */
    public function activate() {
        Activate::handle();
    }

    /**
     * Trigger plugin deactivation
     *
     * @return void
     */
    public function deactivate() {
        Deactivate::handle();
    }

    /**
     * returns assets url
     *
     * @return string
     */
    public static function assets_url(): string {
        return trailingslashit( plugins_url( '', BOOKTICS_FILE ) . '/assets' );
    }

    /**
     * Check if the plugin version is up to date
     *
     * @return void
     */
    public static function version_check() {
        $latest_version = BOOKTICS_VERSION;
        if ( is_multisite() ) {
            $sites = get_sites();

            foreach ( $sites as $site ) {
                switch_to_blog( $site->blog_id );

                $installed_version = booktics_get_option( 'booktics_version' );
                if ( $installed_version < $latest_version ) {
                    self::run_migration();
                    booktics_update_option( 'booktics_version', booktics()->version );
                }

                restore_current_blog();
            }
        } else {
            $installed_version = booktics_get_option( 'booktics_version' );
            if ( $installed_version < $latest_version ) {
                self::run_migration();
                booktics_update_option( 'booktics_version', booktics()->version );
            }
        }
    }

    /**
     * Run migration
     *
     * @return void
     */
    public static function run_migration() {
        Installer::create_tables();
    }

    /**
     * Register post type
     *
     * @return void
     */
    public static function register_custom_post_type() {
        register_post_type(
            'booktics-service', array(
				'labels'       => array(
					'name'          => __( 'Services', 'booktics' ),
					'singular_name' => __( 'Service', 'booktics' ),
				),
				'public'       => true,
				'has_archive'  => true,
				'has_single'   => true,
				'rewrite'      => true,
				'supports'     => array( 'title', 'editor', 'thumbnail' ),
				'show_in_rest' => true,
				'show_in_menu' => false,
            )
        );
    }
}
