<?php

namespace EasyContentManager\Ajax;

use EasyContentManager\Classes\AbstractAjaxHandler;
use EasyContentManager\Utils\Helper;

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

class Addons extends AbstractAjaxHandler {
	public function __construct() {
		$this->actions = [
			'get_all_addons' => [
				'callback' => [ $this, 'get_all_addons' ],
				'fields' => [],
			],
			'saved_addon_status' => [
				'callback' => [ $this, 'saved_addon_status' ],
				'fields' => [
					'addons' => 'array|string',
				],
			],
		];
	}

	public function get_all_addons() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}
		$ecm_addons = json_decode( get_option( EASY_CONTENT_MANAGER_ADDONS_SETTINGS_NAME, '{}' ) );
		wp_send_json_success( $ecm_addons );
	}

	public function saved_addon_status( $payload ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die();
		}

		$addons = $payload['addons'] ?? [];

		foreach ( $addons as $addon ) {
			$addon_name = $addon['addon_name'] ?? '';
			$addon_slug = $addon['addon_slug'] ?? '';
			$status = isset( $addon['status'] ) ? (bool) $addon['status'] : false;

			if ( empty( $addon_slug ) ) {
				wp_send_json_error( __( 'Addon Name missing', 'easy-content-manager' ) );
			}

			if ( $status ) {
				$required_plugin = $addon['required_plugin'] ?? '';
				do_action( 'easy_content_manager/before_active_addon', $addon_slug, $required_plugin );
				if ( $required_plugin && is_array( $required_plugin ) ) {
					foreach ( $required_plugin as $plugin ) {
						if ( ! Helper::is_plugin_active( sanitize_text_field( $plugin['plugin_dir_path'] ) ) ) {
							$error_message = sprintf( '%s Plugin is required to activate %s addon.', sanitize_text_field( $plugin['plugin_name'] ), $addon_name );
							wp_send_json_error( $error_message );
						}
					}
				}
			}

			$saved_addons = (array) json_decode( get_option( EASY_CONTENT_MANAGER_ADDONS_SETTINGS_NAME ), true );
			$saved_addons[ $addon_slug ] = $status;
			update_option( EASY_CONTENT_MANAGER_ADDONS_SETTINGS_NAME, wp_json_encode( $saved_addons ) );
			// Fire Addon Action
			if ( $status ) {
				do_action( "easy-content-manager/addons/activated_{$addon_slug}", $status );
			} else {
				do_action( "easy-content-manager/addons/deactivated_{$addon_slug}", $status );
			}
		}//end foreach

		// response
		wp_send_json_success( $saved_addons );
	}

}
