<?php
namespace DOZENT_LMS_CERTIFICATE;


final class Dozent_LMS_Certificate_Base {

	protected static $_instance;

	private $WPDashboard;
	private $post_type;
	private $metabox;
	private $download;
	private $template;


	public static function instance() {
		if ( version_compare( phpversion(), '7.1', '<' ) ) {
			add_action( 'admin_notices', [ __CLASS__, 'php_upgrade_notice' ] );
			return;
		}

		if ( ! extension_loaded( 'gd' ) ) {
			add_action( 'admin_notices', [ __CLASS__, 'gd_load_notice' ] );
			return;
		}


		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}

		return self::$_instance;
	}

	public static function php_upgrade_notice(){
		$message = sprintf( __( '%s PHP Version Upgrade Notice:  %s In order to use %s, your server PHP version requires at least %s or greater', 'dozent-lms-certificate' ),  '<strong>', '</strong>', '<strong> Dozent LMS Certificate </strong>', '<strong>7.1.0</strong>' );

		$messagetwo = sprintf( __( 'Currently your server running PHP version %s which out of date, upgrade your PHP version', 'dozent-lms-certificate' ), '<strong> ' . phpversion() . ' </strong>' );

		printf( '<div class="notice notice-error"><p>%1$s</p> <p>%2$s</p> </div>', $message, $messagetwo );
	}

	/**
	 * Check if GD Extension install
	 *
	 */

	public static function gd_load_notice(){
		$message = sprintf( __( 'In order to use %s Plugin, your server must have installed GD extension', 'dozent-lms-certificate' ), '<strong> Dozent LMS Certificate </strong>' );

		printf( '<div class="notice notice-error"><p>%1$s</p> </div>', $message );
	}

	public function __construct() {
		add_action( 'init', [ $this, 'init' ], 0 );
		register_activation_hook( DLC_FILE, [ $this, 'install' ] );
	}

	public function init() {
		if ( ! class_exists( '\Dozent\Application' ) ) {
			add_action( 'admin_notices', [ $this, 'require_dozent_core_plugin' ] );

			return;
		}

		$this->load_plugin_textdomain();

		//Include files
		$this->includes();

		//Load Modules
		$this->load_modules();

		$this->init_settings();
	}

	public function require_dozent_core_plugin(){
		$screen = get_current_screen();
		if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) {
			return;
		}

		$base_name = 'dozent/dozent.php';
		$installed_plugins = get_plugins();

		if ( isset( $installed_plugins[ $base_name ] ) ) {
			if ( ! current_user_can( 'activate_plugins' ) ) {
				return;
			}

			$activation_url = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $base_name . '&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_' . $base_name );

			$message = '<p>' . sprintf( __( 'in order to work %s properly, you must need to activate the Dozent LMS Core plugin.', 'dozent-lms-certificate' ), '<strong> Dozent LMS Certificate </strong>' ) . '</p>';
			$message .= '<p>' . sprintf( '<a href="%s" class="button-primary">%s</a>', $activation_url, __( 'Activate Dozent LMS Now', 'dozent-lms-certificate' ) ) . '</p>';
		} else {

			if ( ! current_user_can( 'install_plugins' ) ) {
				return;
			}

			$install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=dozent' ), 'install-plugin_dozent' );

			$message = '<p>' . sprintf( __( 'in order to work %s properly, you must need to install the Dozent LMS Core plugin.', 'dozent-lms-certificate' ), '<strong> Dozent LMS Certificate </strong>' ) . '</p>';
			$message .= '<p>' . sprintf( '<a href="%s" class="button-primary">%s</a>', $install_url, __( 'Install Dozent LMS Now', 'dozent-lms-certificate' ) ) . '</p>';
		}

		echo '<div class="error"><p>' . $message . '</p></div>';
	}

	public function load_plugin_textdomain() {
		load_plugin_textdomain( 'dozent-lms-certificate', false, basename( dirname( DLC_FILE ) ) . '/languages' );
	}

	public function includes() {
		include DLC_ABSPATH . 'core/functions.php';
	}

	public function load_modules() {
		$this->WPDashboard = new WPDashboard();
		$this->post_type   = new Certificate_Post_Type();
		$this->metabox     = new Certificate_Metabox();
		$this->download    = new Certificate_Download();
		$this->template    = new Certificate_Template();
	}

	public function init_settings(){
		add_filter( 'dozent/options/attr', [ $this, 'generate_settings' ] );
	}

	public function generate_settings( $attrs ){

		$attrs[ 'certificate' ] = [

			'title'        => __( 'Certificate', 'dozent-lms-certificate' ),
			'icon'         => 'dicon-certificate',
			'field_groups' => [
				'general' => [
					'title'     => __( 'Certificate', 'dozent-lms-certificate' ),
					'desc'      => __( 'Set up certificate settings to control the Dozent LMS certificate plugin', 'dozent-lms-certificate' ),
					'fields' => [
						'enable_course_specific_certificate'      => [
							'type'        => 'checkbox',
							'title'       => __( 'Course Specific Certificate', 'dozent-lms-certificate' ),
							'label_title' => __( 'Enable', 'dozent-lms-certificate' ),
							'desc'        => __( 'Course-specific certificates allow instructors to choose certificate templates for each course.', 'dozent-lms-certificate' ),
						],
					],
				],

			],
		];

		return $attrs;
	}

	public function install() {
		update_option( 'DLC_VERSION', DLC_VERSION );
	}

}