<?php

/**
 * Helpful extra functions for fields or files
 * to get information more easily inside yaml files or php
 */
Kirby::plugin('scd/custom-methods', [
    'collectionMethods' => [],
    'pageMethods' => [
        'zips' => function () {
            return $this->files()->filterBy('template', 'zip');
        },
        'getsyncedfiles' => function () {
            $currentFiles = $this->files()->filterBy('template', 'in', ['image', 'video', 'mp3']);
            $resultAssets = $currentFiles;
            $syncedPages = $this->sync_events()->toPages();

            foreach ($syncedPages as $syncedPage) {
                $resultAssets->add($syncedPage->files()->filterBy('template', 'in', ['image', 'video', 'mp3']));
            }

            $siteAssets = kirby()->site()->files();
            $resultAssets->add($siteAssets);

            return $resultAssets;
        },
        'getmixedimages' => function () {
            $siteAssets = kirby()->site()->files()->filterBy('template', 'image');
            $currentPage = $this;
            $resultAssets;

            if ($currentPage->isModule()) {
                $resultAssets = $currentPage->page()->files()->filterBy('template', 'image');
            } elseif ($currentPage->intendedTemplate() == "pressesub") {
                $resultAssets = $currentPage->getsyncedfiles()->filterBy('template', 'image');
            } else {
                $resultAssets = $currentPage->files()->filterBy('template', 'image');
            }

            return $resultAssets->add($siteAssets);
        },
        'getmixedvideos' => function () {
            $siteAssets = kirby()->site()->files()->filterBy('template', 'video');
            $currentPage = $this;
            $resultAssets;

            if ($currentPage->isModule()) {
                $resultAssets = $currentPage->page()->files()->filterBy('template', 'video');
            } elseif ($currentPage->intendedTemplate() == "pressesub") {
                $resultAssets = $currentPage->getsyncedfiles()->filterBy('template', 'video');
            } else {
                $resultAssets = $currentPage->files()->filterBy('template', 'video');
            }

            return $resultAssets->add($siteAssets);
        },
        'getmixedaudios' => function () {
            $siteAssets = kirby()->site()->files()->filterBy('template', 'mp3');
            $currentPage = $this;
            $resultAssets;

            if ($currentPage->isModule()) {
                $resultAssets = $currentPage->page()->files()->filterBy('template', 'mp3');
            } elseif ($currentPage->intendedTemplate() == "pressesub") {
                $resultAssets = $currentPage->getsyncedfiles()->filterBy('template', 'mp3');
            } else {
                $resultAssets = $currentPage->files()->filterBy('template', 'mp3');
            }

            return $resultAssets->add($siteAssets);
        },

    ],
    'fieldMethods' => [
        'toPagesAndDrafts' => function ($field) {
            $result = new Pages();
            if ($field && $field->isNotEmpty()) {
                $selectedPages = $field->yaml();

                foreach ($selectedPages as $page) {
                    $result->add(kirby()->page($page));
                }
            }
            return $result;
        },

        'toPageOrDraft' => function ($field) {
            if ($field && $field->isNotEmpty()) {
                $selectedPages = $field->yaml();
                foreach ($selectedPages as $page) {
                    $result = kirby()->page($page);
                    if ($result) {
                        return $result;
                    } else {
                        return false;
                    }
                }
            } else {
                return false;
            }
        },

        'dbstructure' => function ($field) {
            $_structure = new Kirby\Cms\Structure();
            $collection = $field->toStructure();

            foreach ($collection as $item) {
                $_structure->add(new Kirby\Cms\StructureObject([
                    'id'        => $item->id(),
                    'content'   => [
                        'desc' => $item->content()->get(kirby()->language()),
                    ],
                ]));
            }

            return $_structure;
        },

        'mapValueToLabel' => function ($field) {
            $context = $field->parent()->parent();
            $options = $context->content()->get($field->key())->toStructure();

            if ($field->value() != '' || $field->value() != null) {
                $value = $options->findBy('id', $field->value());
                if ($value) {
                    $value = $value->content()->get(kirby()->language());
                }
            } else {
                $value = null;
            }

            return $value;
        },
    ],
    'fileMethods' => [
        'responsiveImg' => function ($alt, $imgclass = 'regular-img', $sizes = null) {
            $sizesAttr = "";
            if ($sizes)
                $sizesAttr = $sizes;

            if ($this->focus()->isNotEmpty())
                //    $tag = "<img class='{$imgclass}' style='--objPos:{$this->focus()}' alt='{$alt}' src='{$this->src()}' srcset='{$this->srcset('webp')}' width='{$this->width()}' height='{$this->height()}' draggable='false' onerror=\"this.src='{$this->src()}'\">";
                $tag = "<picture style='--objPos:{$this->focus()}'>
                        <source {$sizesAttr} srcset='{$this->srcset('webp')}' type='image/webp'>
                        <source {$sizesAttr} srcset='{$this->srcset('default')}' type='image/jpeg'>
                        <img class='{$imgclass}' alt='{$alt}' src='{$this->resize(900)->url()}' width='{$this->width()}' height='{$this->height()}' draggable='false'>
                    </picture>";
            else
                //$tag = "<img class='{$imgclass}' alt='{$alt}' srcset='{$this->srcset('webp')}' width='{$this->width()}' height='{$this->height()}' draggable='false' onerror=\"this.src='{$this->src()}'\">";

                $tag = "<picture>
                        <source {$sizesAttr} srcset='{$this->srcset('webp')}' type='image/webp'>
                        <source {$sizesAttr} srcset='{$this->srcset('default')}' type='image/jpeg'>
                        <img class='{$imgclass}' alt='{$alt}' src='{$this->resize(900)->url()}' width='{$this->width()}' height='{$this->height()}' draggable='false'>
                    </picture>";

            return $tag;
        },
    ],
]);
