<?php
/**
 * EverestForms MailPoet Admin
 *
 * @package EverestForms\MailPoet\Admin\Integration
 * @version 1.0.0
 * @since   1.0.0
 */
namespace EverestForms\Pro\Addons\MailPoet\Admin\Integration;

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'MailPoetIntegration' ) ) :

	/**
	 * MailPoetIntegration class.
	 *
	 * @since 1.0.0
	 */
	class MailPoetIntegration extends \EVF_Integration {

		/**
		 * Account status.
		 *
		 * @var string
		 */
		public $account_status = '';

		/**
		 * Init and hook in the integration.
		 */
		public function __construct() {
			$this->id                 = 'mailpoet';
			$this->icon               = plugins_url( 'src/Addons/MailPoet/assets/img/mailpoet.png', EFP_PLUGIN_FILE );
			$this->method_title       = esc_html__( 'MailPoet', 'everest-forms-pro' );
			$this->method_description = esc_html__( 'MailPoet Integration with Everest Forms', 'everest-forms-pro' );
			$is_cofigured             = $this->is_configured();
			$connection_status        = get_option( 'everest_forms_integrations_' . $this->id, false );
			$this->account_status     = evf_string_to_bool( $connection_status ) && $is_cofigured ? 'connected' : '';
		}

		/**
		 * Facilitates mailpoet integration. Evoked from extensibility.
		 */
		public function output_integration() {

			$status_class = ( 'connected' === $this->account_status ) ? ' Connected' : '';
			$is_checked   = ( 'connected' === $this->account_status ) ? 'checked' : '';
			?>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=evf-settings&tab=integration' ) ); ?>" class="everest-forms-integration-back-button">
			<span><?php echo esc_html__( 'Back', 'everest-forms-pro' ); ?></span>
		</a>
		<div class="everest-forms-integration-content">
			<div class="integration-addon-detail">
				<div class="evf-integration-info-header">
					<figure class="evf-integration-logo">
						<img src="<?php echo esc_attr( $this->icon ); ?>" alt="<?php echo esc_attr( 'test' ); ?>">
					</figure>
					<div class="integration-info">
						<h3><?php echo $this->method_title; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ ?></h3>
						<div class="integration-status <?php echo $status_class; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ ?>">
							<span class="toggle-switch connected"><?php echo $status_class; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ ?></span>
						</div>
					</div>
				</div>
				<p><?php echo $this->method_description; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ ?></p>
			</div>
			<div class="integration-connection-detail">
				<div class="evf-account-connect">
					<h3><?php printf( esc_html__( 'Enable Connection', 'everest-forms-pro' ) ); ?></h3>
					<?php
					if ( $this->is_configured() ) {
						?>
					<form class="evf-mailpoet-connection-form">
						<div class="evf-connection-form-wrapper">
							<div class="evf_integration_checkbox_wrapper">
								<input type="checkbox" name="evf_enable_mailpoet" class="evf_enable_mailpoet" value="1" class="evf_integration_checkbox" <?php echo $is_checked; ?>>
								<label class="evf_integration_checkbox_label"><?php printf( esc_html__( 'Check the fields to connect your MailPoet account', 'everest-forms-pro' ) ); ?></label>
							</div>
						</div>
						<button type="button" class="everest-forms-btn everest-forms-btn-primary everest-forms-integration-connect-mailpoet" data-source="<?php echo esc_attr( $this->id ); ?>"><?php printf( esc_html__( 'Save', 'everest-forms-pro' ) ); ?></button>
					</form>
						<?php
					} else {
						?>
						<div>
							<p class="evf-warning"><?php echo $this->configuration_setup_message(); ?></p>
						</div>
						<?php
					}
					?>
				</div>
			</div>
		</div>
			<?php
		}
		/**
		 * Check the mailpoet configuration.
		 *
		 * @since 1.0.0
		 * @return boolean
		 */

		public static function is_configured() {
			try {
				if ( ! class_exists( \MailPoet\API\API::class ) ) {
					return false;
				}
				$api = \MailPoet\API\API::MP( 'v1' )->isSetupComplete();
				if ( $api ) {
					return $api;
				}
			} catch ( Exception $e ) {

			}

			return false;
		}

		/**
		 * Configuration setup message.
		 *
		 * @since 1.0.0
		 */
		public static function configuration_setup_message() {
			$message = sprintf( "%s <a href='%s' target='_blank'>%s</a>", esc_html__( 'MailPoet is not configured yet! Please configure your MailPoet api first.', 'everest-forms-pro' ), esc_url( 'https://docs.everestforms.net/' ), esc_html__( 'For more', 'everest-form-mailpoet' ) );
			return $message;
		}
		/**
		 * Supported custom field types.
		 *
		 * @param string $field_for The field for.
		 *
		 * @since 1.0.0
		 */
		public static function get_supported_field_types( $field_for ) {
			switch ( $field_for ) {
				case 'email_field':
					$field_types = array( 'email' );
					break;
				case 'first_name_field':
				case 'last_name_field':
					$field_types = array( 'first-name', 'last-name', 'text', 'dat-time', 'url', 'number' );
					break;
				case 'custom_fields':
					$field_types = array( 'first-name', 'last-name', 'text', 'date-time', 'textarea', 'radio', 'select', 'checkbox' );
					break;
				default:
					$field_types = array();
					break;
			}
			return $field_types;
		}
	}
endif;
