<?php

namespace Fab\Controller;

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

use Fab\Interfaces\Model_Interface;

/**
 * WPForms Lite integration
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WPForms_Lite extends Base implements Model_Interface {

    /**
     * Autoload integration
     *
     * @return void
     */
    public function autoload_integration() {
        \FAB()->Helper->autoload_integration( FAB_WPFORMS_LITE_PLUGIN_FILE );
    }

    /**
     * Add integration to FAB
     *
     * @param array $integrations The integrations.
     * @return array The integrations.
     */
    public function add_integration( $integrations ) {
        $integrations['wpforms-lite'] = array(
            'name'        => __( 'WPForms Lite', 'floating-awesome-button' ),
            'description' => __( 'WPForms Lite is a powerful and flexible contact form plugin for WordPress.', 'floating-awesome-button' ),
            'url'         => 'https://wordpress.org/plugins/wpforms-lite/',
            'icon_url'    => 'https://ps.w.org/wpforms-lite/assets/icon.svg',
            'plugin_file' => FAB_WPFORMS_LITE_PLUGIN_FILE,
            'status'      => \Fab\Plugin\Helper::getInstance()->check_plugin_integration( FAB_WPFORMS_LITE_PLUGIN_FILE ),
        );
        return $integrations;
    }

    /**
     * Filter template title.
     *
     * @param string $title The title.
     * @param string $type The type of FAB.
     * @param object $data The data.
     */
    public function filter_template_title( $title, $type, $data ) {
        // Prevent if wpforms lite plugin is not active.
        if ( ! FAB_WPFORMS_LITE_PLUGIN_ACTIVE ||
            ! isset( $data->requires ) ||
            ! in_array( FAB_WPFORMS_LITE_PLUGIN_FILE, $data->requires, true ) ||
            ! isset( $data->wpformsid )
        ) {
            return $title;
        }

        // Get form title.
        $form = get_post( $data->wpformsid );
        if ( $form ) {
            // translators: %s will be replaced form title.
            $title = sprintf( __( 'WPForms: %s', 'floating-awesome-button' ), $form->post_title );
        }

        return $title;
    }

    /**
     * Filter the FAB template content.
     *
     * @param string $content The content.
     * @param string $type The type of FAB.
     * @param object $data The data.
     */
    public function filter_template_content( $content, $type, $data ) {
        // Prevent error if wpforms lite plugin is not active.
        if ( ! FAB_WPFORMS_LITE_PLUGIN_ACTIVE ||
            ! isset( $data->requires ) ||
            ! in_array( FAB_WPFORMS_LITE_PLUGIN_FILE, $data->requires, true ) ||
            ! isset( $data->wpformsid )
        ) {
            return $content;
        }

        // Check if form id is set.
        if ( isset( $data->wpformsid ) ) {
            $form = get_post( $data->wpformsid );
        } else {
            // Get latest wpform.
            $forms = get_posts(
                array(
                    'post_type'   => 'wpforms',
                    'numberposts' => 1,
                )
            );

            // Check if any forms exist.
            if ( ! empty( $forms ) ) {
                // Get form.
                $form = $forms[0];
            }
        }

        // Check if form is valid.
        if ( $form && 'wpforms' === $form->post_type ) {
            // Change content to wpforms shortcode.
            $content = sprintf( '[wpforms id="%d"]', $form->ID );
        }

        return $content;
    }

    /**
     * Enqueue scripts
     *
     * @return void
     */
    public function enqueue_scripts() {
        try {
            $screen = get_current_screen();

            // Check if screen is WPForms edit page and integration is enabled.
            $screens = array(
                'toplevel_page_wpforms-overview', // Overview/Index page.
                'wpforms_page_wpforms-builder', // Edit page.
            );

            // Check if screen is in the array.
            if ( in_array( $screen->id, $screens, true ) && \Fab\Plugin\Helper::getInstance()->check_plugin_integration( FAB_WPFORMS_LITE_PLUGIN_FILE ) === 'enabled' ) {
                \FAB()->Helper->wp_enqueue_script_typescript( 'fab-integration-wpforms-lite', 'assets/ts/integration/wpforms-lite.ts', array(), FAB_VERSION, true );
                wp_localize_script(
                    'fab-integration-wpforms-lite',
                    'FAB_TEMPLATE',
                    array(
                        'id'    => 'wpforms-popup',
                        'label' => array(
                            'button' => __( 'Turn into Popup', 'floating-awesome-button' ),
                        ),
                    )
                );
            }
        } catch ( \Exception $e ) {
            // phpcs:ignore
            error_log( $e->getMessage() );
        }
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * Execute Class.
     *
     * @return void
     */
    public function run() {
        // Add integration to FAB.
        add_filter( 'fab_plugin_integrations', array( $this, 'add_integration' ) );

        // Prevent error if plugin is not active.
        if ( ! FAB_WPFORMS_LITE_PLUGIN_ACTIVE ) {
            return;
        }

        // Autoload integration.
        add_action( 'admin_init', array( $this, 'autoload_integration' ) );

        // Filter template title.
        add_filter( 'fab_template_title', array( $this, 'filter_template_title' ), 10, 3 );

        // Filter template content.
        add_filter( 'fab_template_content', array( $this, 'filter_template_content' ), 10, 3 );

        // Add custom script to WPForms edit page.
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    }
}
