<?php

namespace Fab\Controller;

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

use Fab\Metabox\FABMetaboxSetting;
use Fab\Interfaces\JIT_Interface;

/**
 * Link.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Link_Type extends Link_Group implements JIT_Interface {

    /**
     * Constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->type = 'link';
    }

    /**
     * Get labels type.
     *
     * @return array
     */
    public function get_labels_type() {
        return array(
            'name' => __( 'Link', 'floating-awesome-button' ),
        );
    }

    /**
     * Add link data to FAB item
     *
     * @param object $instance FAB item instance.
     * @return void
     */
    public function add_fab_item_data( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        // Set Link.
        $link = $this->grab_link( $instance, $instance->getID() );
        $instance->setLink( $link );

        // Set Link Behavior.
        $link_behavior = get_post_meta( $instance->getID(), FABMetaboxSetting::$post_metas['link_behavior']['meta_key'], true );
        $link_behavior = \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $link_behavior );
        $instance->setLinkBehavior( $link_behavior );

        // Set Link Href.
        $instance->setLinkHref( true );
    }

    /**
     * Grab Link
     *
     * @param object $item Item.
     * @param int    $fab_id FAB ID.
     * @return string
     */
    public function grab_link( $item, $fab_id ) {
        $link = get_post_meta( $fab_id, FABMetaboxSetting::$post_metas['link']['meta_key'], true );
        $link = ( $link && is_string( $link ) ) ? $link : '';

        // Support whatsapp link.
        $link = $this->convert_whatsapp_link( $link, $item->getRawContent() );

        // Support placeholder for link.
        if ( ! is_admin() ) {
            $link = $item->do_placeholder( $link );
        }

        return $link;
    }

    /**
     * Add support for whatsapp link.
     *
     * @param string $link Link.
     * @param string $content Content.
     * @return string
     */
    public function convert_whatsapp_link( $link, $content ) {
        // If content is empty string, return link as is.
        if ( ! $content || '' === $content ) {
            return $link;
        }

        // Convert whatsapp link to api.whatsapp.com - due to unsupported characters in whatsapp link.
        if ( strpos( $link, 'https://wa.me/' ) !== false ) {
            $link = str_replace( 'https://wa.me/', 'https://api.whatsapp.com/send?phone=', $link );
        }

        // Don't add content parameter if we're in admin editing fab.
        if ( strpos( $link, 'https://api.whatsapp.com' ) !== false && ! is_admin() ) {
            $content = wp_strip_all_tags( $content );
            $content = rawurlencode( $content );
            $link    = add_query_arg( 'text', $content, $link );
        }

        return $link;
    }

    /**
     * Filter template postmeta.
     *
     * @param array  $postmeta The postmeta.
     * @param string $type The type.
     * @param object $data The data.
     * @return array
     */
    public function filter_template_postmeta( $postmeta, $type, $data ) {
        if ( $this->type !== $type ) {
            return $postmeta;
        }

        $postmeta['fab_setting_link']          = isset( $data->settings->link ) ? $data->settings->link : '';
        $postmeta['fab_setting_link_behavior'] = isset( $data->settings->open_in_new_tab ) ? \Fab\Plugin\Helper::getInstance()->transform_yes_no_value( $data->settings->open_in_new_tab ) : 'no';

        return $postmeta;
    }

    /**
     * Add as recommended template.
     *
     * @param array $templates The templates.
     * @param int   $max_templates The max templates.
     * @return array
     */
    public function add_fab_dashboard_recommended_templates( $templates, $max_templates ) {
        // Check if max templates is reached.
        if ( count( $templates ) >= $max_templates ) {
            return $templates;
        }

        // Add as recommended template.
        $templates[] = 'floating-button-logged-in-user';
        $templates[] = 'floating-button-on-home-page';
        $templates[] = 'floating-button-on-404';
        $templates[] = 'floating-button-on-any-single-post-type';
        $templates[] = 'floating-button-on-pages';
        $templates[] = 'floating-button-on-posts';
        $templates[] = 'email';
        $templates[] = 'instagram';
        $templates[] = 'latest-post';
        $templates[] = 'whatsapp';
        return $templates;
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add link group type.
        add_filter( 'fab_types_group_link', array( __CLASS__, 'jit_add_fab_types_group' ), 10, 1 );

        // Adjust item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );

        // Add fab setting types.
        add_filter( 'fab_setting_types', array( __CLASS__, 'jit_add_fab_setting_types' ), 10, 1 );

        // Filter template postmeta.
        add_filter( 'fab_template_postmeta', array( __CLASS__, 'jit_filter_template_postmeta' ), 10, 3 );

        // Add as recommended template.
        add_filter( 'fab_dashboard_recommended_templates', array( __CLASS__, 'jit_add_fab_dashboard_recommended_templates' ), 10, 2 );
    }
}
