<?php

namespace GriffinForms\Admin\Html\Elements;

class ModalItemsList extends \GriffinForms\Admin\Html\Format
{
    protected $items;
    protected $item_type;
    protected $search_value;
    protected $lang;
    protected $name;
    
    protected function setLang($type = null)
    {
        $lang_class = '\GriffinForms\Admin\Language\\' . $this->item_type;
        $this->lang = $lang_class::getInstance();    
    }
    
    public function html()
    {
        echo '';
        
        if (!empty($this->search_value)) {
            
            if (!empty($this->items)) {
                $this->searchResultDetails();
                $this->itemsList();
            } else {
                $this->noResults();
            }
            
        } else {
            
            if (empty($this->items)) {
                $this->noItems();
            } else {
                $this->itemsList();
            }
            
        }
        
    }
    
    protected function searchResultDetails()
    {   
        echo '<div class="row border-bottom">';
        echo '<div class="col-md-12 py-2 text-center small">';

        echo sprintf(
            /* translators: 1: search term, 2: number of results */
            esc_html__('Your search %1$s returned %2$d results.', 'griffinforms-form-builder'),
            '<span class="text-muted">' . esc_html($this->search_value) . '</span>',
            count($this->items)
        );

        echo ' ';
        echo '<span class="griffinforms-modal-selectitem-resetsearch text-primary" id="griffinforms-modal-reset' . esc_attr($this->item_type) . 'search" role="button">';
        echo esc_html($this->lang->getText('reset_search'));
        echo '</span>';
        echo '</div>';
        echo '</div>';
    }
    
    protected function noResults()
    {
        echo '<div class="row justify-content-center align-items-center py-4 mt-4" style="min-height: 200px;">';
        
        echo '<div class="col-md-12 text-center">';
        echo '<i class="dashicons dashicons-search" style="font-size: 36px; color: var(--wp-admin-theme-color, #3858e9);"></i>'; 
        echo '</div>';
        
        echo '<div class="col-md-12 fw-bold text-center mt-3">';
        echo esc_html($this->lang->getText('no_results'));
        echo '</div>';
        
        echo '<div class="col-md-12 text-center text-muted mb-2 fs-6">';
        echo esc_html($this->search_value);
        echo '</div>';       
    
        echo '<div class="col-md-12 text-center mt-2">';
        echo '<span id="griffinforms-modal-reset' . esc_attr($this->item_type) . 'search" role="button" style="cursor: pointer; color: var(--wp-admin-theme-color, #3858e9); text-decoration: underline;">';
        echo esc_html($this->lang->getText('reset_search'));
        echo '</span>';
        echo '</div>';
        
        echo '</div>'; 
    }
    
    protected function noItems()
    {
        echo '<div class="row justify-content-center align-items-center" style="height: 200px;">';
        echo '<div class="col-md-12 text-center text-muted">';
        
        echo '<i class="dashicons dashicons-warning" style="font-size: 36px; color: var(--wp-admin-theme-color, #3858e9);"></i>';
        
        echo '<h6 class="mt-4 mb-2">' . esc_html($this->lang->getText('no_' . $this->item_type . '_exist')) . '</h6>';
        echo '<p class="text-muted">' . esc_html__('Why not create one now?', 'griffinforms-form-builder') . '</p>';
        
        $this->createItemButton();
        
        echo '</div>';
        echo '</div>';
    }
    
    protected function createItemButton()
    {
        $buttons = \GriffinForms\Admin\Html\Elements\Buttons::getInstance();
        $buttons->wpBtn([
            'data' => [
                'data-bs-toggle=modal',
                'data-bs-target=#griffinforms-create' . esc_attr($this->item_type) . '-modal'
            ],
            'label' => sprintf(
                esc_html__('Create New %s', 'griffinforms-form-builder'),
                esc_html(ucfirst($this->item_type))
            ),
        ]);
    }
    
    protected function itemsList()
    {
        foreach ($this->items as $item) {
            $this->listItem($item);
        }
        
        
    }
    
    protected function listItem($item)
    {
        // Prepare plain values for name, heading, and description
        $item_name_value = !empty($item->name) ? $item->name : __('Unnamed', 'griffinforms-form-builder');
        $item_heading_value = !empty($item->heading) ? $item->heading : __('Label not set', 'griffinforms-form-builder');
        $item_description_value = !empty($item->description) ? $item->description : __('No description available.', 'griffinforms-form-builder');

        echo '<div class="row griffinforms-modalitemlist-item" id="griffinforms-modalitemlist-' . esc_attr($this->item_type  . '-' . $item->id) . '"';
        echo ' data-item-id="' . esc_attr($item->id) . '" data-item-heading="' . esc_attr(wp_strip_all_tags($item_heading_value)) . '" data-bs-toggle="modal" data-bs-target="#griffinforms-select' . esc_attr($this->name) . '-modal" role="button">';

        // Name field with icon
        echo '<div class="griffinforms-modalitemlist-itemname col-md-3 py-2 text-truncate">';
        $this->itemIcon($item);
        echo wp_kses_post('<span>' . $item_name_value . '</span>');
        echo '</div>';

        // Heading field (skip if item type is folder)
        if ($this->item_type !== 'folder') {
            echo '<div class="col-md-3 py-2 text-truncate">';
            echo wp_kses_post('<span>' . $item_heading_value . '</span>');
            echo '</div>';
        }

        // Description field
        echo '<div class="col-md-6 py-2 text-muted text-truncate">';
        echo wp_kses_post('<span>' . $item_description_value . '</span>');
        echo '</div>';

        echo '</div>';
    }
    
    protected function itemIcon($item)
    {
        if (isset($item->icon)) {
            $location = $this->config->getRootUrl() . 'admin/images/' . $this->item_type . '/' . $item->icon . '.png';
        } else {
            $location = $this->config->getRootUrl() . 'admin/images/' . $this->item_type . '.png';
        }
        
        echo '<span class="me-3">';
        echo '<img class="griffinforms-modalitemlist-icon" src="' . esc_url($location) . '" height="18">';
        echo '</span>';
        
    }
    
    public function __construct($items, $item_type, $name, $search_value = null)
    {
        $this->items = $items;
        $this->item_type = $item_type;
        $this->name = $name;
        $this->search_value = $search_value;
        $this->setAll();
;    }
}