<?php

namespace GriffinForms\Admin\Html\Pages\Lists;

class FormThemes extends \GriffinForms\Admin\Html\Pages\Lists\Format
{
    protected $meta;
    protected $order_by = 'created';
    protected $order = 'desc';

    protected function setType()
    {
        $this->type = 'formtheme';
    }

    protected function setBulkActions()
    {
        $this->bulk_actions = array(
            'deleteFormThemes'
        );
    }

    protected function deleteFormThemesModal()
    {
        $for = 'deleteFormThemes';
        $class = '';
        $data = array('item_type' => $this->type);
        $this->newModal($for, $class, $data);
    }

    protected function pageModals()
    {
        $this->deleteFormThemesModal();
        $this->deleteListItemModal();
        (new \GriffinForms\Admin\Html\Modals\ColumnManager())->html();
    }

    protected function createFormThemeBtn()
    {
        $create_url = admin_url('admin.php?page=griffinforms-formtheme');
        echo '<a href="' . esc_url($create_url) . '" class="page-title-action">';
        echo esc_html($this->lang->getText('create', $this->type));
        echo '</a>';
    }

    protected function listHeading()
    {
        echo '<h1 class="wp-heading-inline">' . esc_html($this->lang->getText($this->meta, 'list_heading')) . '</h1>';
        $this->createFormThemeBtn();
        echo '<hr class="wp-header-end">';
    }

    protected function setFilterNames()
    {
        $this->filter_names = array(
            'search',
            'date_from',
            'date_to',
        );
    }

    protected function registerColumns(): void
    {
        $r = $this->column_registry;

        $r->register('formtheme', 'checkbox',    ['label' => '',                                                     'locked' => true, 'default_on' => true]);
        $r->register('formtheme', 'name',        ['label' => $this->lang->getText('name', 'list_head'),              'locked' => true, 'default_on' => true, 'sortable' => true]);

        $r->register('formtheme', 'colors',      ['label' => $this->lang->getText('colors', 'list_head'),            'default_on' => true]);
        $r->register('formtheme', 'description', ['label' => $this->lang->getText('description', 'list_head'),       'default_on' => true]);
        $r->register('formtheme', 'category',    ['label' => $this->lang->getText('category', 'list_head'),          'default_on' => true]);
        $r->register('formtheme', 'author',      ['label' => $this->lang->getText('author', 'list_head'),            'default_on' => true]);
        $r->register('formtheme', 'created',     ['label' => $this->lang->getText('created', 'list_head'),           'default_on' => true, 'sortable' => true]);

        $r->register('formtheme', 'id',          ['label' => __('ID', 'griffinforms-form-builder'),                  'default_on' => false]);
        $r->register('formtheme', 'lastedited',  ['label' => __('Last Edited', 'griffinforms-form-builder'),         'default_on' => false]);
        $r->register('formtheme', 'lasteditor', ['label' => __('Last Editor', 'griffinforms-form-builder'),         'default_on' => false,
            'variants' => [
                'default'  => __('Username + full name', 'griffinforms-form-builder'),
                'nicename' => __('Username only', 'griffinforms-form-builder'),
            ],
            'default_variant' => 'default',
        ]);

        parent::registerColumns();
    }

    protected function setRowActions()
    {
        $this->row_actions = array(
            'edit',
            'duplicate',
            'download',
            'trash'
        );
    }

    protected function itemFilters()
    {
        echo '<div class="alignleft actions">';
        $this->dateFilter();
        echo '</div>';
    }

    protected function headingHeadingText($list_column)
    {
        echo esc_html($this->lang->getText('heading', 'table_header'));
    }

    protected function colorsHeadingText($list_column)
    {
        echo esc_html($this->lang->getText('colors', 'list_head'));
    }

    protected function descriptionHeadingText($list_column)
    {
        echo esc_html($this->lang->getText('description', 'table_header'));
    }

    protected function categoryHeadingText($list_column)
    {
        echo esc_html($this->lang->getText('category', 'table_header'));
    }

    protected function colorsCellText($list_column, $item)
    {
        // The $item is already a FormTheme object, load it to get the full data
        $form_theme = new \GriffinForms\Items\FormTheme();
        $form_theme->loadItem($item->id);

        // Get the three colors
        $form_style_bg = esc_attr($form_theme->getPageBackgroundColor());
        $label_color = esc_attr($form_theme->getLabelColor());
        $submit_btn_bg = esc_attr($form_theme->getSubmitButtonBackgroundColor());

        // Render 3 color squares with no gap
        echo '<div class="d-flex">';
        echo '<span class="gf-color-square" style="background-color:' . $form_style_bg . ';" title="Form Style Background"></span>';
        echo '<span class="gf-color-square" style="background-color:' . $label_color . ';" title="Label Color"></span>';
        echo '<span class="gf-color-square" style="background-color:' . $submit_btn_bg . ';" title="Submit Button Background"></span>';
        echo '</div>';
    }

    protected function headingCellText($list_column, $item)
    {
        echo esc_html($item->heading ?? '—');
    }

    protected function descriptionCellText($list_column, $item)
    {
        $description = $item->description ?? '—';
        // Truncate long descriptions
        if (strlen($description) > 60) {
            $description = substr($description, 0, 60) . '...';
        }
        echo esc_html($description);
    }

    protected function categoryCellText($list_column, $item)
    {
        $category = $item->category ?? 'default';
        $category_display = ucfirst(str_replace('_', ' ', $category));
        echo '<span class="badge text-bg-secondary">' . esc_html($category_display) . '</span>';
    }

    protected function rowActionDuplicate($item)
    {
        $nonce = wp_create_nonce('duplicate_item');
        echo '<a class="griffinforms-formtheme-duplicate-btn" data-gf-id="' . absint($item->id) . '" data-gf-type="formtheme" data-gf-nonce="' . esc_attr($nonce) . '" href="#">';
        echo esc_html($this->lang->getText('duplicate', 'row_action'));
        echo '</a>';
    }

    protected function rowActionDownload($item)
    {
        echo '<a class="griffinforms-formtheme-download-btn" data-gf-id="' . absint($item->id) . '" href="#">';
        echo esc_html($this->lang->getText('download', 'row_action'));
        echo '</a>';
    }

    protected function idCellText($list_column, $item)
    {
        echo '<span class="badge text-bg-secondary">' . absint($item->id) . '</span>';
    }

}
