<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\View;

/**
 * Sharing Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Sharing_Notice extends Notice implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        $this->id = 'fab_sharing_notice';
    }

    /**
     * Show admin sharing notice.
     *
     * @return void
     */
    public function fab_show_admin_notice() {
        // If sharing is not considered, return.
        if ( ! get_option( 'fab_sharing_notice_considered' ) ) {
            return;
        }

        // Check if the notice should be shown.
        if ( ! $this->should_show_notice() ) {
            return;
        }

        // Setup Buttons.
        $plugin_url = 'https://wordpress.org/plugins/floating-awesome-button/';
        $buttons    = array();
        $buttons[]  = array(
            'message' => __( 'Share via Email', 'floating-awesome-button' ),
            'url'     => 'mailto:?subject=' . urlencode( __( 'Check out this awesome WordPress plugin!', 'floating-awesome-button' ) ) . '&body=' . urlencode( $plugin_url ),
            'target'  => '_blank',
            'classes' => 'text-white text-center text-xs px-2.5 py-1.5 bg-primary-600 hover:bg-primary-700 transition-all font-semibold',
        );
        $buttons[]  = array(
            'message' => __( 'Share on Facebook', 'floating-awesome-button' ),
            'url'     => 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $plugin_url ),
            'target'  => '_blank',
            'classes' => 'text-white text-center text-xs px-2.5 py-1.5 bg-blue-600 hover:bg-blue-700 transition-all font-semibold',
        );
        $buttons[]  = array(
            'message' => __( 'Share on X (Twitter)', 'floating-awesome-button' ),
            'url'     => 'https://twitter.com/intent/tweet?url=' . urlencode( $plugin_url ) . '&text=' . urlencode( __( 'Check out this awesome WordPress plugin!', 'floating-awesome-button' ) ),
            'target'  => '_blank',
            'classes' => 'text-white text-center text-xs px-2.5 py-1.5 bg-gray-800 transition-all font-semibold',
        );

        // Prepare the data for the admin notice.
        $data = array_merge(
            $this->get_default_data(),
            array(
                'title'   => __( '🎉 Enjoying FAB? Share it with others!', 'floating-awesome-button' ),
                'message' => __( 'Help us spread the word about FAB! Share it with your friends and fellow WordPress users.', 'floating-awesome-button' ),
                'buttons' => $buttons,
            )
        );

        View::RenderStatic( 'Backend.admin-notice', $data );
    }

    /**
     * Consider showing sharing notice after 14 days of FAB usage.
     *
     * @param int     $post_id The post ID.
     * @param WP_Post $post    The post object.
     * @param bool    $update  Whether the post is being updated.
     * @return void
     */
    public function fab_consider_showing_sharing_notice( $post_id, $post, $update ) {
        // If post is trash, return.
        if ( 'trash' === $post->post_status ) {
            return;
        }

        // Check if post is published and been created more than 14 days ago.
        if ( 'publish' === $post->post_status && $post->post_date_gmt < strtotime( '-14 days' ) ) {
            update_option( 'fab_sharing_notice_considered', true );
        }
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Show admin sharing notice.
        add_action( 'admin_notices', array( __CLASS__, 'jit_fab_show_admin_notice' ) );

        // Consider showing sharing notice after 14 days of FAB usage.
        add_action( 'fab_save_post', array( __CLASS__, 'jit_fab_consider_showing_sharing_notice' ), 10, 3 );
    }
}
