<?php

namespace CleantalkSP\SpbctWP\AdminBannersModule\AdminBanners;

use CleantalkSP\SpbctWP\AdminBannersModule\AdminBannersHandler;
use CleantalkSP\SpbctWP\UsersPassCheckModule\UsersPassCheckHandler;

class AdminBannerPassLeak extends AdminBannerAbstract
{
    /**
     * Hiding time in days
     */
    const HIDING_TIME = 365;

    /**
     * Simple Banner Name, most be unique
     */
    const NAME = 'passleak';

    /**
     * Data for template
     *
     * @var array $template_data
     */
    private $template_data;

    /**
     * @var AdminBannersHandler
     */
    private $banners_handler;

    /**
     * @param AdminBannersHandler $banners_handler
     * @psalm-suppress PossiblyUnusedMethod
     */
    public function __construct(AdminBannersHandler $banners_handler)
    {
        $this->banners_handler = $banners_handler;
        $this->banner_id       = $this->prefix . $this::NAME . '_' . $this->banners_handler->getUserId();

        $user_id          = $this->banners_handler->getUserId();
        $user             = get_userdata($user_id);
        $user_login       = $user ? $user->user_login : '';
        $user_email       = $user ? $user->user_email : '';
        $user_profile_link = get_edit_user_link($user_id) . '#password';
        $account_label    = esc_html($user_login) . ' (' . esc_html($user_email) . ')';

        $subtitle_parts = array(
            esc_html__('The CleanTalk security module has detected that your password was found in a data leak database.', 'security-malware-firewall'),
            sprintf(
                esc_html__('We recommend changing your password to a more secure one. Please change your password in your %s.', 'security-malware-firewall'),
                '<a href="' . esc_url($user_profile_link) . '" style="text-decoration: none;">' . esc_html__('profile', 'security-malware-firewall') . '</a>'
            ),
            sprintf(
                esc_html__('You can learn more about this feature in the %s.', 'security-malware-firewall'),
                '<a href="' . esc_url('https://blog.cleantalk.org/wordpress-password-leak-protection/') . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none;">' . esc_html__('article', 'security-malware-firewall') . '</a>'
            ),
        );

        $this->template_data = array(
            'title'   => sprintf(
                esc_html__('Security by Cleantalk: Important security recommendation for your account %s!', 'security-malware-firewall'),
                $account_label
            ),
            'content' => implode(' ', $subtitle_parts),
        );
    }

    /**
     * do I need to show a banner?
     *
     * @return bool
     */
    protected function needToShow()
    {
        if (
            !$this->isDismissed() &&
            UsersPassCheckHandler::isUserPassLeaked($this->banners_handler->getUserId())
        ) {
            return true;
        }

        return false;
    }

    /**
     * Print HTML of banner
     */
    protected function display()
    {
        $data = $this->template_data;
        ?>
        <div class="spbc-notice error um-admin-notice notice is-dismissible" id="<?php
        echo $this->banner_id; ?>" style="position: relative;">
            <h3 style="margin-bottom: 0;">
                <?php echo $data['title']; ?>
            </h3>
            <p style="color: gray;">
                <?php echo $data['content']; ?>
            </p>
        </div>
        <?php
    }
}
