<?php declare(strict_types=1);

/**
 * You MUST use this namespace even if you are in a child template or else loading the class file will not work.
 */
namespace Template\easytemplate360\src\CustomWidgets;

/**
 * CustomWidgetExample class.
 *
 * Your custom widget php class MUST begin with CustomWidget
 */
class CustomWidgetExample extends BaseCustomWidget {

    protected function prepareSmarty(): void {
        /*
         * Assign your data to $this->smartyConfig[...] here - use this->resolve* to help resolve complex configuration values.
         *
         * The backend configuration in its raw form is contained in $this->config.
         *
         * Therefore, the simplest line of code that could work here is just:
         *
         * $this->smartyConfig = $this->config;
         *
         * But this would NOT properly resolve language values, images, links, etc.
         *
         * Note: Display settings, like time control, customer groups are evaluated before rendering the widget, so they do not need to be evaluated here, again.
         * Note: Display settings, like widget spacing above/below the widget are resolved automatically.
         * Note: 'size' does not need to be resolved, as it is resolved in every widget automatically.
         */
        $this->smartyConfig['headline'] = $this->resolveTextField($this->config['headline']);
        $this->smartyConfig['technicalString'] = $this->resolveTranslatedValue($this->config['technicalString']);
        $this->smartyConfig['color'] = $this->resolveColorValue($this->config['color']);
        $this->smartyConfig['icon'] = $this->resolveIcon($this->config['icon']);
        $this->smartyConfig['image'] = $this->resolveImage($this->config['image']);
        $this->smartyConfig['link'] = $this->resolveLink($this->config['link']);
        // Note: Shop items, like products, categories etc. are only stored by ID, so you have to load them (or the data you need relating to them) here by yourself.
        $this->smartyConfig['productId'] = (int) $this->config['productId'];
        // Note: Multiselects result in arrays of IDs.
        $this->smartyConfig['categoryIds'] = $this->config['categoryIds'];
        // Note: This results in an assoc array with a value for those breakpoints ("xs", "sm", "md", "lg", "xl") where a value is set explicitly. It does NOT contain the postFix from the configuration JSON.
        $this->smartyConfig['numberPerBreakpoint'] = $this->resolveBreakpointValue('numberPerBreakpoint', $this->config['numberPerBreakpoint']);
        $this->smartyConfig['textPerBreakpoint'] = $this->resolveBreakpointValue('textPerBreakpoint', $this->config['textPerBreakpoint']);
        // Note: For complex breakpoint values, like images, you should first resolve the breakpoint value, then the content:
        $breakpointImages = $this->resolveBreakpointValue('imagesPerBreakpoint', $this->config['imagesPerBreakpoint']);
        if(\is_array($breakpointImages)) {
            $this->smartyConfig['imagesPerBreakpoint'] = \array_map(function ($breakpointImage) {
                return $this->resolveImage($breakpointImage);
            }, $breakpointImages);
        }
        $this->smartyConfig['textArea'] = $this->resolveTranslatedValue($this->config['textArea']);
        // Note: Resolving editor data works like this:
        $this->smartyConfig['wysiwyg'] = $this->resolveTextArea($this->config['wysiwyg']);
        // Note: Checkboxes are booleans.
        $this->smartyConfig['checkbox'] = (bool) $this->config['checkbox'];
        // Note: Not everything must be resolved:
        $this->smartyConfig['rawText'] = $this->config['rawText'];
        $this->smartyConfig['rawTextArea'] = $this->config['rawTextArea'];
        $this->smartyConfig['rawNumber'] = $this->config['rawNumber'];
        $this->smartyConfig['defaultSelect'] = $this->config['defaultSelect'];
        $this->smartyConfig['defaultRadio'] = $this->config['defaultRadio'];
        // Note: Grids are just arrays of configs
        $this->smartyConfig['itemsInAGrid'] = [];
        foreach($this->config['aGrid'] as $anItem) {
            $this->smartyConfig['itemsInAGrid'][] = [
                'gridElementHeadline' => $this->resolveTextField($anItem['gridElementHeadline']),
                'gridElementImage' => $this->resolveImage($anItem['gridElementImage']),
                'gridElementLink' => $this->resolveLink($anItem['gridElementLink']),
            ];
        }

        // Note: You can use this to skip rendering of the widget, it will be evaluated by the base class after this method was called
        // $this->setSkip(true);
    }

    // Note: You can also use this method to determine if the widget should be rendered, at all.
    // This method is executed *before* prepareSmarty is being called!
    protected function shouldRender(): bool {
        return parent::shouldRender();
    }
}