<?php

namespace Gigneo;

use Carbon_Fields\Container\Container;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Theme_Option
 *
 * Plugin theme option class for Gigneo.
 *
 * @since 0.0.1
 */
class Theme_Option {

	/**
	 * The single instance of the class.
	 *
	 * @var Theme_Option|null
	 * @since 0.0.1
	 */
	private static $instance = null;

	/**
	 * Constructor for the class.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {

		// Register theme options
		add_action( 'carbon_fields_register_fields', array( $this, 'register_theme_options' ) );

		//Reset button
		add_action( 'carbon_fields_container_gigneo-theme-options_before_submit_button', array( $this, 'add_reset_button' ) );

		//Sidebar
		add_action( 'carbon_fields_container_gigneo-theme-options_sidebar_area', array( $this, 'sidebar_content' ) );

		//After Save Options
		add_action( 'carbon_fields_theme_options_container_saved', array( $this, 'theme_option_after_save' ) );
	}

	/**
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @return Theme_Option
	 * @since 0.0.1
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	public function theme_option_after_save() {
		if ( function_exists( 'gigneo_flush_option_cache' ) ) {
			gigneo_flush_option_cache();
		}

		flush_rewrite_rules();
	}

	/**
	 * Register theme options.
	 *
	 * @return void
	 */
	public function register_theme_options() {

		$gigneo_option_list = self::theme_option_list();

		$gigneo_general_label      = $this->render_heading_markup( $gigneo_option_list['general']['title'], $gigneo_option_list['general']['description'], $gigneo_option_list['general']['icon'] );
		$gigneo_auth_label         = $this->render_heading_markup( $gigneo_option_list['auth']['title'], $gigneo_option_list['auth']['description'], $gigneo_option_list['auth']['icon'] );
		$gigneo_freelancer_label   = $this->render_heading_markup( $gigneo_option_list['freelancer']['title'], $gigneo_option_list['freelancer']['description'], $gigneo_option_list['freelancer']['icon'] );
		$gigneo_service_label      = $this->render_heading_markup( $gigneo_option_list['service']['title'], $gigneo_option_list['service']['description'], $gigneo_option_list['service']['icon'] );
		$gigneo_employer_label     = $this->render_heading_markup( $gigneo_option_list['employer']['title'], $gigneo_option_list['employer']['description'], $gigneo_option_list['employer']['icon'] );
		$gigneo_job_label          = $this->render_heading_markup( $gigneo_option_list['job']['title'], $gigneo_option_list['job']['description'], $gigneo_option_list['job']['icon'] );
		$gigneo_template_label     = $this->render_heading_markup( $gigneo_option_list['template']['title'], $gigneo_option_list['template']['description'], $gigneo_option_list['template']['icon'] );
		$gigneo_payment_label      = $this->render_heading_markup( $gigneo_option_list['payment']['title'], $gigneo_option_list['payment']['description'], $gigneo_option_list['payment']['icon'] );
		$gigneo_slug_label         = $this->render_heading_markup( $gigneo_option_list['slug']['title'], $gigneo_option_list['slug']['description'], $gigneo_option_list['slug']['icon'] );
		$gigneo_notification_label = $this->render_heading_markup( $gigneo_option_list['notification']['title'], $gigneo_option_list['notification']['description'], $gigneo_option_list['notification']['icon'] );
		$gigneo_other_label        = $this->render_heading_markup( $gigneo_option_list['other']['title'], $gigneo_option_list['other']['description'], $gigneo_option_list['other']['icon'] );

		$gigneo_general_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/general.php';
		$gigneo_general_fields = apply_filters( 'gigneo_ts_general_tab_fields', $gigneo_general_fields );

		$gigneo_auth_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/auth.php';
		$gigneo_auth_fields = apply_filters( 'gigneo_ts_auth_tab_fields', $gigneo_auth_fields );

		$gigneo_freelancer_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/freelancer.php';
		$gigneo_freelancer_fields = apply_filters( 'gigneo_ts_freelancer_tab_fields', $gigneo_freelancer_fields );

		$gigneo_service_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/service.php';
		$gigneo_service_fields = apply_filters( 'gigneo_ts_service_tab_fields', $gigneo_service_fields );

		$gigneo_employer_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/employer.php';
		$gigneo_employer_fields = apply_filters( 'gigneo_ts_employer_tab_fields', $gigneo_employer_fields );

		$gigneo_job_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/job.php';
		$gigneo_job_fields = apply_filters( 'gigneo_ts_job_tab_fields', $gigneo_job_fields );

		$gigneo_template_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/template.php';
		$gigneo_template_fields = apply_filters( 'gigneo_ts_template_tab_fields', $gigneo_template_fields );

		$gigneo_payment_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/payment.php';
		$gigneo_payment_fields = apply_filters( 'gigneo_ts_payment_tab_fields', $gigneo_payment_fields );

		$gigneo_slug_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/slug.php';
		$gigneo_slug_fields = apply_filters( 'gigneo_ts_slug_tab_fields', $gigneo_slug_fields );

		$gigneo_notification_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/notification.php';
		$email_fields = apply_filters( 'gigneo_ts_notification_tab_fields', $gigneo_notification_fields );

		$gigneo_other_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/fields/other.php';
		$gigneo_other_fields = apply_filters( 'gigneo_ts_other_tab_fields', $gigneo_other_fields );

		$container = Container::make( 'theme_options', 'gigneo-theme-options', esc_html__( 'Gigneo Option', 'gigneo' ) )
								->set_page_parent( 'gigneo' )
								->set_page_file( 'gigneo-options' )
								->set_page_menu_position( 1 )
								->set_layout( 'tabbed-vertical' )
								->set_classes( 'gigneo-theme-option-container' )
								->add_tab( $gigneo_general_label, $gigneo_general_fields )
								->add_tab( $gigneo_auth_label, $gigneo_auth_fields )
								->add_tab( $gigneo_freelancer_label, $gigneo_freelancer_fields )
								->add_tab( $gigneo_service_label, $gigneo_service_fields )
								->add_tab( $gigneo_employer_label, $gigneo_employer_fields )
								->add_tab( $gigneo_job_label, $gigneo_job_fields )
								->add_tab( $gigneo_template_label, $gigneo_template_fields )
								->add_tab( $gigneo_payment_label, $gigneo_payment_fields )
								->add_tab( $gigneo_slug_label, $gigneo_slug_fields )
								->add_tab( $gigneo_notification_label, $gigneo_notification_fields )
								->add_tab( $gigneo_other_label, $gigneo_other_fields );

		apply_filters( 'gigneo_container_theme_option', $container );
	}

	/**
	 * Theme option list.
	 *
	 * @return array
	 * @since 0.0.1
	 */
	public static function theme_option_list() {

		$list = array(
			'general'      => array(
				'title'       => esc_html__( 'General', 'gigneo' ),
				'description' => esc_html__( 'Basic settings', 'gigneo' ),
				'icon'        => 'hi hi-squares-plus',
			),
			'auth'         => array(
				'title'       => esc_html__( 'Auth', 'gigneo' ),
				'description' => esc_html__( 'Authentication settings', 'gigneo' ),
				'icon'        => 'hi hi-lock-closed',
			),
			'freelancer'   => array(
				'title'       => gigneo_freelancer_plural(),
				'description' => sprintf(
					// translators: %s: Freelancer singular label
					esc_html__( '%s options', 'gigneo' ),
					gigneo_freelancer_singular()
				),
				'icon'        => 'hi hi-users',
			),
			'service'      => array(
				'title'       => esc_html__( 'Services', 'gigneo' ),
				'description' => esc_html__( 'Service settings', 'gigneo' ),
				'icon'        => 'hi hi-archive-box',
			),
			'employer'     => array(
				'title'       => gigneo_employer_plural(),
				'description' => sprintf(
					// translators: %s: Employer singular label
					esc_html__( '%s options', 'gigneo' ),
					gigneo_employer_singular()
				),
				'icon'        => 'hi hi-user-group',
			),
			'job'          => array(
				'title'       => esc_html__( 'Jobs', 'gigneo' ),
				'description' => esc_html__( 'Job settings', 'gigneo' ),
				'icon'        => 'hi hi-briefcase',
			),
			'template'     => array(
				'title'       => esc_html__( 'Templates', 'gigneo' ),
				'description' => esc_html__( 'Set up templates', 'gigneo' ),
				'icon'        => 'hi hi-square-3-stack-3d',
			),
			'payment'      => array(
				'title'       => esc_html__( 'Payments', 'gigneo' ),
				'description' => esc_html__( 'Payment options', 'gigneo' ),
				'icon'        => 'hi hi-banknotes',
			),
			'slug'         => array(
				'title'       => esc_html__( 'Slugs', 'gigneo' ),
				'description' => esc_html__( 'Configure slugs', 'gigneo' ),
				'icon'        => 'hi hi-square-2-stack',
			),
			'notification' => array(
				'title'       => esc_html__( 'Notifications', 'gigneo' ),
				'description' => esc_html__( 'Notification settings', 'gigneo' ),
				'icon'        => 'hi hi-chat-bubble-left-ellipsis',
			),
			'other'        => array(
				'title'       => esc_html__( 'Others', 'gigneo' ),
				'description' => esc_html__( 'Miscellaneous options', 'gigneo' ),
				'icon'        => 'hi hi-cog-6-tooth',
			),
		);

		return apply_filters( 'gigneo_theme_option_list', $list );
	}

	/**
	 * Generates HTML markup for a heading section.
	 *
	 * @param string $title The title text for the heading.
	 * @param string $description The description text for the heading. Optional.
	 * @param string $icon The icon class for the heading. Optional.
	 *
	 * @return string The generated HTML markup.
	 * @since 0.0.1
	 */
	public static function render_heading_markup( $title, $description = '', $icon = '' ) {
		ob_start();

		if ( ! empty( $icon ) ) {
			echo '<i class="' . esc_attr( $icon ) . '"></i>';
		}

		if ( ! empty( $title ) || ! empty( $description ) ) {
			echo '<span class="content">';

			if ( ! empty( $title ) ) {
				echo '<span class="title">' . esc_html( $title ) . '</span>';
			}

			if ( ! empty( $description ) ) {
				echo '<span class="description">' . esc_html( $description ) . '</span>';
			}

			echo '</span>';
		}

		return ob_get_clean();
	}

	/**
	 * Add reset button.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function add_reset_button() {
		echo '<a href class="button button-secondary">' . esc_html__( 'Discard changes', 'gigneo' ) . '</a>';
	}

	/**
	 * Sidebar content.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function sidebar_content() {
		include_once GIGNEO_DIR_PATH . 'includes/theme-option/views/sidebar.php';
	}
}

// Instantiate
Theme_Option::instance();
