<?php

namespace GriffinForms\Admin\Html\Elements;

class TemplateCard extends \GriffinForms\Format
{
    protected $config;
    protected $category;
    protected $search_term;
    protected $user_img;
    protected $placeholder_img;
    protected $buttons;
    
    public function __construct($category = '', $search_term = '')
    {
        $this->category = $category;
        $this->search_term = $search_term;
        $this->setAll();
    }

    protected function setUserImg()
    {
        $this->user_img = $this->config->getRootUrl() . 'admin/images/user.svg';
    }

    protected function setPlaceholderImg()
    {
        $this->placeholder_img = $this->config->getRootUrl() . 'admin/images/forms/form_template_placeholder_griffin.jpeg';
    }

    protected function setButtons()
    {
        $this->buttons = Buttons::getInstance();
    }

    public function cardsHtml($templates)
    {
        $html = '';

        if (!empty($templates)) {
            foreach ($templates as $template) {
                $html .= $this->formTemplateCard(template: $template);
            }
        } else {
            $html .= $this->noTemplateFound();
        }

        return $html;
    }

    protected function noTemplateFound()
    {
        if (!empty($this->search_term)) {
            /* translators: %s is either a search keyword or category name depending on context */
            $msg = sprintf(__('No templates found for keyword <strong>%s</strong>', 'griffinforms-form-builder'), $this->search_term);
        } else {
            /* translators: %s is either a search keyword or category name depending on context */
            $msg = sprintf(__('No templates found for category <strong>%s</strong>', 'griffinforms-form-builder'), $this->category);
        }

        $html  = '<div class="gf-notice gf-notice-warning small w-100" role="alert">';
        $html .= '<span class="ms-1">' . wp_kses_post($msg) . '</span>';
        $html .= '</div>';
        return $html;
    }

    public function formTemplateCard($template = '')
    {
        $badge = $template['origin'] === 'user' ? $this->userBadge() : '';

        // Server-side render preview HTML for this template
        $preview_html = '';
        if (!empty($template['form_data'])) {
            try {
                $preview_class = new \GriffinForms\Admin\Html\Elements\Previews\TemplatePreview($template['form_data']);
                $preview_html = $preview_class->renderPreview();
            } catch (\Throwable $e) {
                $preview_html = '<p>' . esc_html__('No preview available.', 'griffinforms-form-builder') . '</p>';
            }
        }

        // Start building the HTML for the form card
        $html = '<div class="col">';
        $html .= '<div class="griffinforms-form-template-card card small p-0 mt-0 position-relative overflow-hidden shadow-none" style="width: auto; height: 100%" data-gf-templateid="' . absint($template['id']) . '">';
        $html .= '<div class="griffinforms-template-preview mb-1 bg-light">';
        $html .= $preview_html; // Insert server-rendered preview
        $html .= '</div>';
        $html .= $badge;
        $html .= '<div class="card-body p-2">';
        $description = $template['description'] ?? __('No description available.', 'griffinforms-form-builder');
        $html .= '<div class="card-title small fw-bold">' . esc_html($template['name'] ?? __('Untitled', 'griffinforms-form-builder')) . '</div>';
        $html .= '<p class="card-text small text-muted" title="' . esc_attr($description) . '">' . esc_html($description) . '</p>';
        
        $html .= '<div class="text-center">';
        $html .= $this->buttons->wpBtn([
            'class' => 'griffinforms-create-form-from-template button button-secondary button-small text-center',
            'data'  => ['data-gf-templateid=' . absint($template['id'])],
            'label' => __('Create Form', 'griffinforms-form-builder'),
            'link'  => '#'
        ], false);
        $html .= '</div>';

        $html .= '</div>'; // Close card-body
        $html .= '</div>'; // Close card
        $html .= '</div>'; // Close col
        
        return $html;
    }

    protected function userBadge()
    {
        $html  = '<img class="griffinforms-template-card-badge position-absolute border-5 border-white rounded-circle" width="24" ';
        $html .=  'src="' . esc_url($this->user_img) . '">';
        return $html;
    }
}
