<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    inline-comments-for-post
 * @subpackage inline-comments-for-post/admin
 * @author     Multidots <info@multidots.com>
 */

declare( strict_types = 1 );

namespace INLINE_COMMENTS_FOR_POST\Includes;

use INLINE_COMMENTS_FOR_POST\Includes\Traits\Singleton;

/**
 * Main class file.
 */
class Admin {

	use Singleton;

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * The comment settings of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      array    $icfp_comment_settings    The comment settings of this plugin.
	 */
	private $icfp_comment_settings;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		if ( defined( 'INLINE_COMMENTS_FOR_POST_VERSION' ) ) {
			$this->version = INLINE_COMMENTS_FOR_POST_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->setup_admin_hooks();
	}
	/**
	 * Function is used to define admin hooks.
	 *
	 * @return void
	 * @since   1.0.0
	 */
	public function setup_admin_hooks(): void {
		add_action( 'admin_enqueue_scripts', array( $this, 'icfp_enqueue_styles' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'icfp_enqueue_scripts' ) );
		add_action( 'admin_menu', array( $this, 'icfp_add_plugin_page' ) );
		add_action( 'admin_init', array( $this, 'icfp_register_settings' ) );
		// add_action( 'init', array( $this, 'icfp_turn_comments_off' ) );

		//add_filter( 'wp_insert_post_data', array( $this, 'icfp_default_comments_off' ) );

		$this->icfp_comment_settings = get_option( 'icfp_comment_settings' ) ?? array();
	}

	/**
	 * Filters the post data before it is inserted or updated.
	*
	* This function sets the comment status to 'open' for pages, overriding the default comment status.
	*
	* @param array $data The post data to be inserted or updated.
	* @return array The filtered post data.
	*/
 	public function icfp_default_comments_off( $data ) {
		if( $data['post_type'] == 'page' ) {
			$data['comment_status'] = 'open';
		}
	
		return $data;
	}


	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @return void
	 * @since    1.0.0
	 */
	public function icfp_enqueue_styles(): void {
		wp_enqueue_style( 'inline-comments-for-post', INLINE_COMMENTS_FOR_POST_URL . 'assets/build/admin.css', array(), $this->version, 'all' );

		wp_enqueue_style( 'wp-color-picker' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @return void
	 * @since    1.0.0
	 */
	public function icfp_enqueue_scripts(): void {
		wp_enqueue_script( 'inline-comments-for-post', INLINE_COMMENTS_FOR_POST_URL . 'assets/build/admin.js', array( 'jquery', 'wp-color-picker' ), $this->version, false );

		wp_localize_script(
			'inline-comments-for-post',
			'siteConfig',
			array(
				'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
				'ajax_nonce' => wp_create_nonce( 'loadmore_post_nonce' ),
			)
		);

		wp_add_inline_script(
			'wp-color-picker',
			'jQuery(document).ready(function($){
				$(".icfp-color-picker").wpColorPicker();
			});'
		);
	}

	/**
	 * Add the plugin page.
	 *
	 * @return void
	 * @since    1.0.0
	 */
	public function icfp_add_plugin_page(): void {
		add_menu_page(
			__( 'Better Commenting', 'inline-comments-for-post' ),
			__( 'Better Commenting', 'inline-comments-for-post' ),
			'manage_options',
			'better-commenting-admin',
			array( $this, 'icfp_create_admin_page_callback' ),
			'dashicons-testimonial',
			9999
		);
	}

	/**
	 * Create the admin page callback function.
	 *
	 * @return void
	 * @since    1.0.0
	 */
	public function icfp_create_admin_page_callback() {
		// Check if the nonce is set and valid before saving the settings.
		if ( isset( $_POST['icfp_tabs_nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['icfp_tabs_nonce'] ) ), 'icfp_tabs_action' ) ) {
			// Nonce is invalid.
			wp_die( 'Security check failed' );
		}

		$active_tab = isset( $_REQUEST['tab'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ) : 'dashboard';
		?>
		<div class="cf-dashboard-main-layout">
			<div class="cf-dashboard-layout">
				<div class="cf-dashboard-layout__header">
					<a href="#" target="_blank" class="main-logo">
						<img class="cf-plugin-logo" src="<?php echo esc_url( INLINE_COMMENTS_FOR_POST_URL . 'assets/images/plugin-logo.svg' ); ?>" class="md-ccm-logo" alt="md logo">
					</a>
				</div>
				<div class="cf-dashboard-layout__outer">
					<div class="cf-dashboard-layout__tabs-list nav-tab-wrapper">
						<a href="?page=better-commenting-admin&tab=dashboard" class="nav-tab <?php echo 'dashboard' === $active_tab ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Dashboard', 'inline-comments-for-post' ); ?></a>
						<a href="?page=better-commenting-admin&tab=comment-settings" class="nav-tab <?php echo 'comment-settings' === $active_tab ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Comment Settings', 'inline-comments-for-post' ); ?></a>
					</div>
					<div class="cf-dashboard-layout__inner">
						<?php settings_errors(); ?>
						<form class="cf-dashboard-layout__form cf-admin-page__setting-form" method="post" action="options.php">
							<?php
							wp_nonce_field( 'icfp_tabs_action', 'icfp_tabs_nonce' ); ?>
								<div class="cf-dashboard-layout__form-inner">
									<div class="cf-dashboard-layout__intro">
										<?php if ( 'dashboard' === $active_tab ) {
										?>
											
												<h1><?php esc_html_e( 'Welcome to Better Commenting', 'inline-comments-for-post' ); ?></h1>
												<p><?php esc_html_e( 'Better Commenting is a plugin that allows you to add inline comments to your posts and pages.', 'inline-comments-for-post' ); ?></p>
												<p><?php esc_html_e( 'You can enable or disable comments for each post or page.', 'inline-comments-for-post' ); ?></p>
												<p><?php esc_html_e( 'You can also customize the comment form and comment settings.', 'inline-comments-for-post' ); ?></p>
												<iframe width="760" height="415" src="https://www.youtube.com/embed/yydfYwOXn2I?si=kV5b_Awn0Zg7oG0x" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
										<?php
										}

										if ( 'comment-settings' === $active_tab ) {
											settings_fields( 'icfp_comment_settings_group' );
											do_settings_sections( 'icfp-comment-settings' );
											submit_button();
										}
									?>
									</div>
									<div class="cf-dashboard-details-rightbar"> 
										<div class="cf-dashboard-details-rightbar-inner"> 
											<div class="cf-dashboard-rightbar-block cf-dashboard-sidetop-block"> 
												<div class="cf-dashboard-rightbar-free-trial"> 
													<h5 class="wp-block-heading" id="h-collaborate-like-a-pro-in-wordpress">Collaborate Like a PRO in WordPress!&nbsp;</h5>
													<p>Bring the power of Google Docs collaboration to your WordPress site.</p>
													<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
														<div class="wp-block-button cf-dashboard-rightbar-free-trial-btn md_sidebar-optionbtn">
															<a class="wp-block-button__link wp-element-button" href="https://www.multicollab.com/pricing/?is_trial_period=yes&amp;utm_source=Website&amp;utm_medium=Blog+Side+Banner+%5BOption+1%5D&amp;utm_campaign=Website+Side+Banner+14+Days+&amp;utm_id=Website+Blog+Side+Banner" target="_blank"><strong>Start 14-Day FREE Trial Now!</strong></a>
														</div
														>
													</div> 
												</div>
											</div> 
											<div class="cf-dashboard-rightbar-block cf-dashboard-sidebottom-block"> 
												<div class="cf-dashboard-rightbar-free-trial"> 
													<h5 class="wp-block-heading" id="h-collaborate-like-a-pro-in-wordpress">Can We Help With Your Website?</h5>
													<p>From small tweaks to big projects, we’re your go-to team for all things WordPress.</p>
													<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
														<div class="wp-block-button cf-dashboard-rightbar-free-trial-btn md_sidebar-optionbtn">
															<a class="wp-block-button__link wp-element-button setexternallink" href="https://www.multidots.com/?utm_source=Multicollab+Blog+Side+Banner&amp;utm_medium=Multicollab+Blog+Side+Banner&amp;utm_campaign=Multicollab&amp;utm_id=Multicollab+" target="_blank">
																<strong>Book a Call</strong>
															</a>
														</div>
													</div>
												</div> 
											</div> 
										</div> 
									</div>
								</div>	
						</form>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Register the settings.
	 *
	 * @return void
	 * @since    1.0.0
	 */
	public function icfp_register_settings(): void {
		register_setting( 'icfp_comment_settings_group', 'icfp_comment_settings', array( $this, 'icfp_sanitize_settings' ) );

		add_settings_section(
			'icfp_comment_settings_section',
			__( 'Comment Settings', 'inline-comments-for-post' ),
			array( $this, 'icfp_comment_settings_section_callback' ),
			'icfp-comment-settings'
		);

		add_settings_field(
			'icfp_show_user_avatar',
			__( 'User avatar', 'inline-comments-for-post' ),
			array( $this, 'icfp_show_user_avatar_callback' ),
			'icfp-comment-settings',
			'icfp_comment_settings_section',
			array(
				'label_for' => 'icfp_show_user_avatar',
			),
		);

		add_settings_field(
			'icfp_show_comment_time',
			__( 'Comment time', 'inline-comments-for-post' ),
			array( $this, 'icfp_show_comment_time_callback' ),
			'icfp-comment-settings',
			'icfp_comment_settings_section',
			array(
				'label_for' => 'icfp_show_comment_time',
			),
		);

		add_settings_field(
			'icfp_enable_reply_to_comment',
			__( 'Reply to comment', 'inline-comments-for-post' ),
			array( $this, 'icfp_enable_reply_to_comment_callback' ),
			'icfp-comment-settings',
			'icfp_comment_settings_section',
			array(
				'label_for' => 'icfp_enable_reply_to_comment',
			),
		);

		add_settings_field(
			'icfp_comment_depth',
			__( 'Comment Depth', 'inline-comments-for-post' ),
			array( $this, 'icfp_comment_depth_callback' ),
			'icfp-comment-settings',
			'icfp_comment_settings_section',
			array(
				'label_for' => 'icfp_comment_depth',
			),
		);

		// Add the field to select the color.
		add_settings_field(
			'icfp_comment_color',
			__( 'Global Comment Color', 'inline-comments-for-post' ),
			array( $this, 'icfp_comment_color_callback' ),
			'icfp-comment-settings',
			'icfp_comment_settings_section',
			array(
				'label_for' => 'icfp_comment_color',
			),
		);
	}

	/**
	 * Sanitize the settings.
	 *
	 * @param array $input The input data.
	 * @return array The sanitized data.
	 * @since    1.0.0
	 */
	public function icfp_comment_settings_section_callback( $input ) {
		$sanitary_values = array();
		if ( isset( $input['icfp_show_user_avatar'] ) ) {
			$sanitary_values['icfp_show_user_avatar'] = sanitize_text_field( $input['icfp_show_user_avatar'] );
		}

		if ( isset( $input['icfp_show_comment_time'] ) ) {
			$sanitary_values['icfp_show_comment_time'] = sanitize_text_field( $input['icfp_show_comment_time'] );
		}

		if ( isset( $input['icfp_enable_reply_to_comment'] ) ) {
			$sanitary_values['icfp_enable_reply_to_comment'] = sanitize_text_field( $input['icfp_enable_reply_to_comment'] );
		}

		if ( isset( $input['icfp_comment_depth'] ) ) {
			$sanitary_values['icfp_comment_depth'] = sanitize_text_field( $input['icfp_comment_depth'] );
		}

		if ( isset( $input['icfp_comment_color'] ) ) {
			$sanitary_values['icfp_comment_color'] = sanitize_text_field( $input['icfp_comment_color'] );
		}

		return $sanitary_values;
	}

	/**
	 * Settings field callback function.
	 */
	public function icfp_show_user_avatar_callback() {
		$checked = ( isset( $this->icfp_comment_settings['icfp_show_user_avatar'] ) && $this->icfp_comment_settings['icfp_show_user_avatar'] == 1 ) ? 'checked="checked"' : '';
		printf(
			"<label for='icfp_show_user_avatar'><input class='checkbox' type='checkbox' name='icfp_comment_settings[icfp_show_user_avatar]' id='icfp_show_user_avatar' value='1' %s /> %s</label>",
			$checked,
			__( 'Do you want to display User\'s Avatar?', 'inline-comments-for-post' )
		);
	}
	

	/**
	 * Settings field callback function.
	 */
	public function icfp_show_comment_time_callback() {
		// Check if the 'icfp_show_comment_time' setting is set and its value is '1' (checked)
		$checked = ( isset( $this->icfp_comment_settings['icfp_show_comment_time'] ) && $this->icfp_comment_settings['icfp_show_comment_time'] == 1 ) ? 'checked="checked"' : '';
	
		printf(
			'<label for="icfp_show_comment_time">
				<input class="checkbox" type="checkbox" name="icfp_comment_settings[icfp_show_comment_time]" id="icfp_show_comment_time" value="1" %s /> %s
			</label>',
			$checked,
			__( 'Do you want to display comment time?', 'inline-comments-for-post' )
		);
	}	

	/**
	 * Settings field callback function.
	 */
	public function icfp_enable_reply_to_comment_callback() {
		// Check if the 'icfp_enable_reply_to_comment' setting is set and its value is '1' (checked)
		$checked = ( isset( $this->icfp_comment_settings['icfp_enable_reply_to_comment'] ) && $this->icfp_comment_settings['icfp_enable_reply_to_comment'] == 1 ) ? 'checked="checked"' : '';
	
		printf(
			'<label for="icfp_enable_reply_to_comment">
				<input class="checkbox" type="checkbox" name="icfp_comment_settings[icfp_enable_reply_to_comment]" id="icfp_enable_reply_to_comment" value="1" %s /> %s
			</label>',
			$checked,
			__( 'Do you want to enable reply to comment?', 'inline-comments-for-post' )
		);
	}	

	/**
	 * Settings field callback function.
	 */
	public function icfp_comment_depth_callback() {
		$comment_depth = isset( $this->icfp_comment_settings['icfp_comment_depth'] ) ? $this->icfp_comment_settings['icfp_comment_depth'] : 5;
		printf(
			'<input type="number" name="icfp_comment_settings[icfp_comment_depth]" id="icfp_comment_depth" value="%s" /><p><i>%s</i></p>',
			esc_attr( $comment_depth ),
			esc_html__( 'It represents the nesting level of a comment in a threaded comment system. It is especially useful for displaying nested comments with a hierarchical structure.', 'inline-comments-for-post' )
		);
	}

	/**
	 * Settings field callback function.
	 */
	public function icfp_comment_color_callback() {
		printf(
			'<input type="text" class="icfp-color-picker" name="icfp_comment_settings[icfp_comment_color]" value="%s" />',
			isset( $this->icfp_comment_settings['icfp_comment_color'] ) ? esc_attr( $this->icfp_comment_settings['icfp_comment_color'] ) : '#4b1bce'
		);
	}

	/**
	 * Turn off comments for all pages.
	 *
	 * @return void
	 * @since    1.0.0
	 */
	public function icfp_turn_comments_off(): void {
		// Ensure the function exists
		if ( ! function_exists( 'is_plugin_active' ) ) {
			include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
		}

		// Check if Elementor plugin is active
		if ( is_plugin_active( 'elementor/elementor.php' ) ) {
			// Disable support for comments and trackbacks in post types.
			foreach ( get_post_types() as $post_type ) {
				remove_post_type_support( $post_type, 'comments' );
				remove_post_type_support( $post_type, 'trackbacks' );
			}
		}
	}
}
