<?php
/**
 * Init Configuration
 *
 * @author Jegstudio
 * @package sociatic
 */

namespace Sociatic;

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

/**
 * Init Class
 *
 * @package sociatic
 */
class Init {

	/**
	 * Instance variable
	 *
	 * @var $instance
	 */
	private static $instance;

	/**
	 * Class instance.
	 *
	 * @return Init
	 */
	public static function instance() {
		if ( null === static::$instance ) {
			static::$instance = new static();
		}

		return static::$instance;
	}

	/**
	 * Class constructor.
	 */
	private function __construct() {
		$this->load_hooks();
		$this->init_instance();
	}

	/**
	 * Load initial hooks.
	 */
	private function load_hooks() {
		add_action( 'init', array( $this, 'register_block_patterns' ), 9 );
		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'dashboard_scripts' ) );

		add_filter( 'gutenverse_template_path', array( $this, 'template_path' ), null, 3 );
		add_filter( 'gutenverse_themes_template', array( $this, 'add_template' ), 10, 2 );
		add_filter( 'gutenverse_block_config', array( $this, 'default_font' ), 10 );
		add_filter( 'gutenverse_font_header', array( $this, 'default_header_font' ) );
		add_filter( 'gutenverse_global_css', array( $this, 'global_header_style' ) );
	}

	public function init_instance() {
		new Asset_Enqueue();
	}

	/**
	 * Generate Global Font
	 *
	 * @param string $value  Value of the option.
	 *
	 * @return string
	 */
	public function global_header_style( $value ) {
		$theme_name      = get_stylesheet();
		$global_variable = get_option( 'gutenverse-global-variable-font-' . $theme_name );

		if ( empty( $global_variable ) && function_exists( 'gutenverse_global_font_style_generator' ) ) {
			$font_variable = $this->default_font_variable();
			$value        .= \gutenverse_global_font_style_generator( $font_variable );
		}

		return $value;
	}

	/**
	 * Header Font.
	 *
	 * @param mixed $value  Value of the option.
	 *
	 * @return mixed Value of the option.
	 */
	public function default_header_font( $value ) {
		if ( ! $value ) {
			$value = array(
				array(
					'value'  => 'Alfa Slab One',
					'type'   => 'google',
					'weight' => 'bold',
				),
			);
		}

		return $value;
	}

	/**
	 * Alter Default Font.
	 *
	 * @param array $config Array of Config.
	 *
	 * @return array
	 */
	public function default_font( $config ) {
		if ( empty( $config['globalVariable']['fonts'] ) ) {
			$config['globalVariable']['fonts'] = $this->default_font_variable();

			return $config;
		}

		if ( ! empty( $config['globalVariable']['fonts'] ) ) {
			// Handle existing fonts.
			$theme_name   = get_stylesheet();
			$initial_font = get_option( 'gutenverse-font-init-' . $theme_name );

			if ( ! $initial_font ) {
				$config['globalVariable']['fonts'] = array_merge( $config['globalVariable']['fonts'], $this->default_font_variable() );
				update_option( 'gutenverse-font-init-' . $theme_name, true );
			}
		}

		return $config;
	}

	/**
	 * Default Font Variable.
	 *
	 * @return array
	 */
	public function default_font_variable() {
		return array(
			array(
				'id'   => 'a0sRlN',
				'name' => 'Text Body',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'weight'     => '400',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.5',
						),
					),
				),
			),
			array(
				'id'   => 'uiccB6',
				'name' => 'H1',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '66',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '74',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '36',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.1',
						),
					),
					'weight'     => '800',
				),
			),
			array(
				'id'   => '1Isv3n',
				'name' => 'H1 Hero Page',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '56',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '36',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '44',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.1',
						),
					),
					'weight'     => '800',
				),
			),
			array(
				'id'   => 'oTRAFP',
				'name' => 'H2',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '46',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '32',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '44',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.1',
						),
					),
					'weight'     => '800',
				),
			),
			array(
				'id'   => 'LzdbXD',
				'name' => 'H3',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '34',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '28',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '28',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.2',
						),
					),
				),
			),
			array(
				'id'   => 'PaPuy7',
				'name' => 'H4',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '26',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '24',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '24',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.2',
						),
					),
				),
			),
			array(
				'id'   => 'rhlJXQ',
				'name' => 'H5',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '24',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.1',
						),
					),
					'weight'     => '800',
				),
			),
			array(
				'id'   => 'hdpsvl',
				'name' => 'H6',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Mobile'  =>
						array(
							'point' => '20',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '22',
							'unit'  => 'px',
						),
						'Desktop' =>
						array(
							'point' => '22',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.3',
						),
					),
				),
			),
			array(
				'id'   => 'CfBlR9',
				'name' => 'Text Hero',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.5',
						),
					),
					'weight'     => '400',
				),
			),
			array(
				'id'   => 'HyL09M',
				'name' => '185',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '30',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '24',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '24',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.5',
						),
					),
				),
			),
			array(
				'id'   => 'BBe7T5',
				'name' => 'Pricing Number',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '50',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '48',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '48',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1',
						),
					),
				),
			),
			array(
				'id'   => 'vRlVnG',
				'name' => 'Focus Title',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'weight'     => '400',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.2',
						),
					),
				),
			),
			array(
				'id'   => 'pnXC9z',
				'name' => 'Button',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1',
						),
					),
					'weight'     => '500',
				),
			),
			array(
				'id'   => 'C1iLU3',
				'name' => 'Category',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '14',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '14',
							'unit'  => 'px',
						),
					),
					'weight'     => '500',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1',
						),
					),
				),
			),
			array(
				'id'   => 'VpadVK',
				'name' => 'Author',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Tablet'  =>
						array(
							'point' => '12',
							'unit'  => 'px',
						),
						'Desktop' =>
						array(
							'point' => '12',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '12',
							'unit'  => 'px',
						),
					),
					'weight'     => '400',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'point' => '1',
							'unit'  => 'px',
						),
					),
				),
			),
			array(
				'id'   => 'FPIdVf',
				'name' => 'Nav Menu',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.2',
						),
					),
					'weight'     => '400',
				),
			),
			array(
				'id'   => 'kxo4Qg',
				'name' => 'Likes',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'weight'     => '400',
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '20',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '20',
							'unit'  => 'px',
						),
					),
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1',
						),
					),
				),
			),
			array(
				'id'   => 'i52CMo',
				'name' => 'Fun Fact',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '36',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '34',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '32',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.6',
						),
					),
				),
			),
			array(
				'id'   => '1Tn02D',
				'name' => 'Post Meta',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '14',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '14',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '14',
							'unit'  => 'px',
						),
					),
					'weight'     => '400',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1',
						),
					),
				),
			),
			array(
				'id'   => 'MeJ5ER',
				'name' => '404',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Mobile'  =>
						array(
							'point' => '92',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '116',
							'unit'  => 'px',
						),
						'Desktop' =>
						array(
							'point' => '136',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.1',
						),
					),
				),
			),
			array(
				'id'   => 'YAPBT8',
				'name' => 'Text Icon Hero',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Hind Siliguri',
						'value' => 'Hind Siliguri',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'weight'     => '500',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1',
						),
					),
				),
			),
			array(
				'id'   => 'ToWh5m',
				'name' => 'Teams Name',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '28',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '22',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '24',
							'unit'  => 'px',
						),
					),
					'weight'     => '800',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.2',
						),
					),
				),
			),
			array(
				'id'   => 'K0QhlR',
				'name' => 'Title Post Blog',
				'font' =>
				array(
					'font'       =>
					array(
						'label' => 'Muli',
						'value' => 'Muli',
						'type'  => 'google',
					),
					'size'       =>
					array(
						'Desktop' =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Tablet'  =>
						array(
							'point' => '18',
							'unit'  => 'px',
						),
						'Mobile'  =>
						array(
							'point' => '16',
							'unit'  => 'px',
						),
					),
					'weight'     => '700',
					'lineHeight' =>
					array(
						'Desktop' =>
						array(
							'unit'  => 'em',
							'point' => '1.7',
						),
					),
				),
			),
		);
	}



	/**
	 * Add Template to Editor.
	 *
	 * @param array $template_files Path to Template File.
	 * @param array $template_type Template Type.
	 *
	 * @return array
	 */
	public function add_template( $template_files, $template_type ) {
		$directory = get_template_directory();

		if ( 'wp_template' === $template_type ) {
			$new_templates = array(
				'home',
				'about',
				'service',
				'blog',
				'contact',
				'index',
				'page',
				'single',
				'archive',
				'search',
				'404',
				'basic-template',
				'blank-canvas',
			);

			foreach ( $new_templates as $template ) {
				$template_files[] = array(
					'slug'  => $template,
					'path'  => $directory . "/gutenverse-templates/templates/{$template}.html",
					'theme' => get_template(),
					'type'  => 'wp_template',
				);
			}
		}

		return $template_files;
	}

	/**
	 * Use gutenverse template file instead.
	 *
	 * @param string $template_file Path to Template File.
	 * @param string $theme_slug Theme Slug.
	 * @param string $template_slug Template Slug.
	 *
	 * @return string
	 */
	public function template_path( $template_file, $theme_slug, $template_slug ) {
		$directory = get_template_directory();

		switch ( $template_slug ) {
			case 'home':
				return $directory . '/gutenverse-templates/templates/home.html';
			case 'about':
				return $directory . '/gutenverse-templates/templates/about.html';
			case 'service':
				return $directory . '/gutenverse-templates/templates/service.html';
			case 'blog':
				return $directory . '/gutenverse-templates/templates/blog.html';
			case 'contact':
				return $directory . '/gutenverse-templates/templates/contact.html';
			case 'index':
				return $directory . '/gutenverse-templates/templates/index.html';
			case 'page':
				return $directory . '/gutenverse-templates/templates/page.html';
			case 'single':
				return $directory . '/gutenverse-templates/templates/single.html';
			case 'archive':
				return $directory . '/gutenverse-templates/templates/archive.html';
			case 'search':
				return $directory . '/gutenverse-templates/templates/search.html';
			case '404':
				return $directory . '/gutenverse-templates/templates/404.html';
			case 'header':
				return $directory . '/gutenverse-templates/parts/header.html';
			case 'footer':
				return $directory . '/gutenverse-templates/parts/footer.html';
			case 'basic-template':
				return $directory . '/gutenverse-templates/templates/basic-template.html';
			case 'blank-canvas':
				return $directory . '/gutenverse-templates/templates/blank-canvas.html';
		}

		return $template_file;
	}

	/**
	 * Register Block Pattern.
	 */
	public function register_block_patterns() {
		new Block_Patterns();
	}

	/**
	 * Enqueue scripts and styles.
	 */
	public function dashboard_scripts() {
		$screen = get_current_screen();

		if ( $screen->id === 'appearance_page_sociatic-dashboard' ) {
			// enqueue css.
			wp_enqueue_style(
				'sociatic-dashboard',
				SOCIATIC_URI . '/assets/css/dashboard.css',
				array(),
				SOCIATIC_VERSION
			);

			// enqueue js.
			wp_enqueue_script(
				'sociatic-dashboard',
				SOCIATIC_URI . '/assets/js/dashboard.js',
				array( 'wp-api-fetch' ),
				SOCIATIC_VERSION,
				true
			);

			wp_localize_script( 'sociatic-dashboard', 'GutenverseThemeConfig', $this->theme_config() );
		}
	}

	/**
	 * Register static data to be used in theme's js file
	 */
	public function theme_config() {
		return array(
			'demo'    => esc_url( 'https:/gutenverse.com/demo?name=sociatic' ),
			'pages'   => array(
				'page-0' => SOCIATIC_URI . 'assets/img/Home-1-scaled.jpg',
				'page-1' => SOCIATIC_URI . 'assets/img/About-scaled.jpg',
				'page-2' => SOCIATIC_URI . 'assets/img/Service-scaled.jpg',
				'page-3' => SOCIATIC_URI . 'assets/img/Single-Post-scaled.jpg',
				'page-4' => SOCIATIC_URI . 'assets/img/Blog.jpg',
			),
			'domain'  => 'sociatic',
			'plugins' => array(
				array(
					'slug' => 'gutenverse',
					'name' => 'Gutenverse',
				),
			),
		);
	}

	/**
	 * Add Menu
	 */
	public function admin_menu() {
		add_theme_page(
			'Theme Dashboard',
			'Theme Dashboard',
			'manage_options',
			'sociatic-dashboard',
			array( $this, 'load_dashboard' ),
			1
		);
	}

	/**
	 * Template page
	 */
	public function load_dashboard() {
		?>
			<div id="gutenverse-theme-dashboard">
			</div>
		<?php
	}
}
