<?php

namespace GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormElements;

class RowColumn extends \GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormElements\Format
{
    protected $bs_rowcolumn_container_classes = '';

    public function getHtml()
    {
        if ($this->item === false) {
            return;
        }

        $is_theme_set = $this->isThemeSet();
        $css_class = 'col-md-' . $this->item->getProp('width');
        $theme_class = !$is_theme_set ? ' gf-no-theme' : '';
        $additional_classes = !$is_theme_set && !empty($this->bs_rowcolumn_container_classes) ? ' ' . esc_attr($this->bs_rowcolumn_container_classes) : '';

        echo '<div id="' . esc_attr($this->getHtmlId($this->item_id)) . '" class="griffinforms-app-formlayout-rowcolumn ' . esc_attr($css_class) . ' p-2 flex-column text-center' . $theme_class . $additional_classes . '" ';
        $this->dataAttrs();
        echo ' style="min-height:75px;">';

        $this->renderActionCluster($this->getContextActions(), array(
            'id'    => 'gf-column-icons-wrap',
            'class' => 'gf-column-action-cluster',
            'label' => __('Column actions', 'griffinforms-form-builder'),
            'meta_container' => true,
        ));
        $this->formFields();

        // $this->columnResizeHandle();

        echo '</div>';
    }

    private function columnName()
    {
        echo '<div id="' . esc_attr($this->getHtmlId('name') . '-' . $this->item_id) . '">';
        $this->textUpdateField(
            'name',
            array('placeholder' => __('Name cannot be empty', 'griffinforms-form-builder'), 'required' => 'required'),
            array('font-size' => '12px', 'font-family' => 'monospace')
        );
        echo '</div>';
    }

    private function columnHeading()
    {
        echo '<div id="' . esc_attr($this->getHtmlId('heading-' . $this->item_id)) . '">';
        $this->textUpdateField(
            'heading',
            array('placeholder' => __('Heading of your column (optional)', 'griffinforms-form-builder')),
            array('font-size' => '13px')
        );
        echo '</div>';
    }

    private function columnDescription()
    {
        echo '<div id="' . esc_attr($this->getHtmlId('description-' . $this->item_id)) . '">';
        $this->textAreaUpdateField(
            'description',
            array('placeholder' => __('Description of your column (optional)', 'griffinforms-form-builder'))
        );
        echo '</div>';
    }

    private function formFields()
    {
        $field_ids = $this->item->getProp('fields');
        
        if (empty($field_ids) or !is_array($field_ids)) {
            return;
        }

        foreach ($field_ids as $field_id) {
            $field = new Field($field_id, $this->item_id, $this, $this->form_theme);
            $field->getHtml();
        }
    }

    private function getContextActions()
    {
        return array_merge(array(
            array(
                'id'           => 'drag',
                'label'        => __('Drag Column', 'griffinforms-form-builder'),
                'icon'         => 'drag_indicator',
                'icon_svg_file' => 'admin/images/toolbar-icons/builder/drag.svg',
                'class'        => 'gf-fe-drag-btn griffinforms-app-formlayout-rowcolumn-drag-handle',
                'action'       => 'drag',
                'context_type' => 'column',
            ),
            $this->getDeleteMenuAction(
                'column',
                sprintf(__('Delete %s', 'griffinforms-form-builder'), $this->config->getFriendlyName('rowcolumn'))
            )
        ), $this->getMoveActions());
    }

    private function getMoveActions()
    {
        return array(
            array(
                'id'       => 'move-horizontal',
                'label'    => __('Move column', 'griffinforms-form-builder'),
                'class'    => 'gf-fe-action-composite--horizontal',
                'children' => array(
                    array(
                        'id'           => 'move-left',
                        'label'        => __('Move column left', 'griffinforms-form-builder'),
                        'icon'         => 'chevron_left',
                        'icon_svg_file' => 'admin/images/toolbar-icons/builder/left.svg',
                        'class'        => 'gf-col-move-btn gf-col-move-left',
                        'action'       => 'move-column-left',
                        'context_type' => 'column',
                    ),
                    array(
                        'id'           => 'move-right',
                        'label'        => __('Move column right', 'griffinforms-form-builder'),
                        'icon'         => 'chevron_right',
                        'icon_svg_file' => 'admin/images/toolbar-icons/builder/right.svg',
                        'class'        => 'gf-col-move-btn gf-col-move-right',
                        'action'       => 'move-column-right',
                        'context_type' => 'column',
                    ),
                ),
            )
        );
    }

    private function columnResizeHandle()
    {
        echo '<div class="rowcolumn-handel">';
        $this->mIcon('width');
        echo '</div>';
    }
}
