<?php

namespace EDPW\Pro\Controllers;

use EDPW\Core\Helpers\EmailTemplate;
use EDPW\Core\Helpers\Plugin;

defined( 'ABSPATH' ) || exit;

class Common {
	
	/**
	 * Enable pro.
	 *
	 * @return true
	 */
	public static function isPro() {
		return true;
	}

	/**
	 * Determine if a given template group is a pro template.
	 *
	 * @param string $template_group The template group to check.
	 *
	 * @return bool Returns true if the template group is a pro template, otherwise false.
	 */
	protected static function isProTemplate( $template_group ) {
		return ! empty( $template_group ) && in_array( $template_group, [ 'caramel', 'peanut', 'biscuit', 'mint' ] );
	}

	/**
	 * Pro in-build addon list.
	 *
	 * @param array $add_on_list Add-on list.
	 *
	 * @return array
	 */
	public static function addProInBuildAddon( $add_on_list ) {
		if ( ! is_array( $add_on_list ) ) {
			return $add_on_list;
		}
		$add_on_list['Booking']      = EDPW_PLUGIN_PATH . 'Pro/Addons/Booking/ebct-booking-compatibility.php';
		$add_on_list['WPLoyalty']    = EDPW_PLUGIN_PATH . 'Pro/Addons/WPLoyalty/wlct-loyalty-compatibility.php';
		$add_on_list['Subscription'] = EDPW_PLUGIN_PATH . 'Pro/Addons/Subscription/esct-subscription-compatibility.php';

		return $add_on_list;
	}

	/**
	 * Is valid template.
	 *
	 * @param string $template_group Template group.
	 * @param string $template Template id.
	 *
	 * @return bool
	 */
	public static function isValidTemplate( $template_group, $template ) {
		if ( ! self::isProTemplate( $template_group ) ) {
			return false;
		}
		$templates = EmailTemplate::getTemplateList();

		return isset( $templates[ $template_group ] ) && array_key_exists( $template, $templates[ $template_group ] );
	}

	/**
	 * Update a template path.
	 *
	 * @param string $file_path Original file path.
	 * @param array $args Arguments array.
	 *   - string $args['template_group'] Template group.
	 *   - string $args['template_name'] Template name.
	 *
	 * @return string Updated file path if the template exists, otherwise the original file path.
	 */
	public static function updateTemplatePath( $file_path, $args ) {
		if ( ! is_array( $args ) || empty( $args['template_group'] ) || empty( $args['template_name'] ) || ! self::isValidTemplate( $args['template_group'], $args['template_name'] ) ) {
			return $file_path;
		}
		$new_file_path = EDPW_PLUGIN_PATH . 'Pro/Views/Templates/' . $args['template_name'] . '/' . $args['template_group'] . '.html';
		if ( file_exists( $new_file_path ) ) {
			return $new_file_path;
		}

		return $file_path;
	}

	/**
	 * Enable Pro Templates.
	 *
	 * Enable the Pro templates for the given template groups.
	 *
	 * @param array $template_groups An array of template groups.
	 * @param string $lang The language code.
	 * @param string $type The template type.
	 *
	 * @return array
	 */
	public static function enableProTemplates( $template_groups, $lang, $type ) {
		if ( ! is_array( $template_groups ) ) {
			return $template_groups;
		}
		foreach ( $template_groups as $group => $group_data ) {
			if ( self::isProTemplate( $group ) ) {
				$template_groups[ $group ]['is_allow'] = true;
			}
		}

		return $template_groups;
	}

	/**
	 * Toggle the activation status of a Pro Addon.
	 *
	 * @param array $response The response data.
	 * @param string $action The action to perform (activate or deactivate).
	 * @param string $slug The slug of the addon.
	 * @param array $addons The list of addons.
	 *
	 * @return array The updated response data with success status and message.
	 */
	public static function toggleProAddonActivate( $response, $action, $slug, $addons ) {
		if ( $action == 'activate' ) {
			if ( empty( $addons[ $slug ]['plugin_file'] ) || ! current_user_can( 'activate_plugin', $addons[ $slug ]['plugin_file'] ) ) {
				return [
					'success' => false,
					'data'    => [ 'message' => esc_html__( 'You are not allowed to activate this add-on.', 'email-editor-plus' ) ]
				];
			}
			if ( is_multisite() && ! is_network_admin() && is_network_only_plugin( $addons[ $slug ]['plugin_file'] ) ) {
				return [
					'success' => false,
					'data'    => [ 'message' => esc_html__( 'You are not allowed to activate this add-on.', 'email-editor-plus' ) ]
				];
			}
			if ( ! empty( $addons[ $slug ]['dependencies'] ) ) {
				$need_add_ons = [];
				foreach ( $addons[ $slug ]['dependencies'] as $dependency ) {
					if ( ! empty( $dependency['file'] ) ) {
						$is_need_to_add = false;
						if ( is_array( $dependency['file'] ) ) {
							// PRO or Free Required check
							$is_need_to_add = true;
							foreach ( $dependency['file'] as $plugin_file ) {
								if ( Plugin::isActive( $plugin_file ) ) {
									$is_need_to_add = false;
									break;
								}
							}
						} elseif ( ! Plugin::isActive( $dependency['file'] ) ) {
							$is_need_to_add = true;
						}
						if ( $is_need_to_add ) {
							$need_add_ons[] = $dependency['name'];
						}
					}
				}
				if ( ! empty( $need_add_ons ) ) {
					// translators: 1. %s will replace a require plugin list
					wp_send_json_error( [ 'message' => sprintf( esc_html__( 'Sorry, you need %s plugin', 'email-editor-plus' ), implode( ',', $need_add_ons ) ) ] );
				}
			}

			$activated = activate_plugin( $addons[ $slug ]['plugin_file'], '', is_network_admin() );
			if ( is_wp_error( $activated ) ) {
				return [
					'success' => false,
					'data'    => [ 'message' => esc_html__( 'Add-on activation has failed', 'email-editor-plus' ) ]
				];
			}

			return [
				'success' => true,
				'data'    => [ 'message' => esc_html__( 'Add-on has been activated', 'email-editor-plus' ) ]
			];
		} elseif ( $action == 'deactivate' ) {
			if ( empty( $addons[ $slug ]['plugin_file'] ) || ! current_user_can( 'deactivate_plugin', $addons[ $slug ]['plugin_file'] ) ) {
				return [
					'success' => true,
					'data'    => [ 'message' => esc_html__( 'You are not allowed to deactivate this add-on.', 'email-editor-plus' ) ]
				];
			}
			if ( ! is_network_admin() && is_plugin_active_for_network( $addons[ $slug ]['plugin_file'] ) ) {
				return [
					'success' => true,
					'data'    => [ 'message' => esc_html__( 'You are not allowed to deactivate this add-on.', 'email-editor-plus' ) ]
				];
			}
			deactivate_plugins( $addons[ $slug ]['plugin_file'], false, is_network_admin() );
			if ( ! is_network_admin() ) {
				update_option( 'recently_activated', [ $addons[ $slug ]['plugin_file'] => time() ] + (array) get_option( 'recently_activated' ) );
			} else {
				update_site_option( 'recently_activated', [ $addons[ $slug ]['plugin_file'] => time() ] + (array) get_site_option( 'recently_activated' ) );
			}

			return [
				'success' => true,
				'data'    => [ 'message' => esc_html__( 'Add-on deactivated', 'email-editor-plus' ) ]
			];
		}

		return $response;
	}
}