<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Source\Provider\GooglePhotos\Type;

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractQueryType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Source\Resolver\CachesResolvedSourceData;
use ZOOlanders\YOOessentials\Source\Resolver\LoadsSourceFromArgs;
use ZOOlanders\YOOessentials\Source\Provider\GooglePhotos\GooglePhotosSource;

class GooglePhotosAlbumMediaQueryType extends AbstractQueryType implements HasSourceInterface
{
    use LoadsSourceFromArgs, CachesResolvedSourceData;

    public function name(): string
    {
        return "googlePhotosAlbumMedia_{$this->source()->id()}_query";
    }

    public function label(): string
    {
        return "{$this->source()->name()} Album Media";
    }

    public static function getCacheKey(): string
    {
        return 'google-photos-album-media';
    }

    public function config(): array
    {
        return [
            'fields' => [
                $this->name() => [
                    'type' => [
                        'listOf' => GooglePhotosMediaType::TYPE_NAME,
                    ],

                    'args' => [
                        'albumId' => [
                            'type' => 'String',
                        ],
                        'offset' => [
                            'type' => 'Int',
                        ],
                        'pageSize' => [
                            'type' => 'Int',
                        ],
                        'media_type' => [
                            'type' => 'String',
                        ],
                        'orderBy' => [
                            'type' => 'String',
                        ],
                        'cache' => [
                            'type' => 'Int',
                        ],
                    ],

                    'metadata' => [
                        'group' => 'Google Photos',
                        'label' => $this->label(),
                        'fields' => [
                            'albumId' => [
                                'label' => 'Album',
                                'type' => 'yooessentials-select-dropdown-async',
                                'description' => 'The album which media to query.',
                                'endpoint' => 'yooessentials/source/google/photos/albums',
                                'meta' => false,
                                'source' => true,
                                'params' => [
                                    'source_id' => $this->source->id(),
                                ],
                            ],
                            '_media_filter' => [
                                'type' => 'grid',
                                'description' => 'Choose the type and the maximum amount of media to fetch.',
                                'width' => '1-2',
                                'fields' => [
                                    'offset' => [
                                        'label' => 'Offset',
                                        'type' => 'yooessentials-number',
                                        'default' => 0,
                                    ],
                                    'pageSize' => [
                                        'label' => 'Limit',
                                        'type' => 'yooessentials-number',
                                        'default' => GooglePhotosSource::PAGE_SIZE_DEFAULT,
                                    ],
                                ],
                            ],
                            'media_type' => [
                                'type' => 'select',
                                'label' => 'Type',
                                'default' => 'all',
                                'options' => [
                                    'All' => 'all',
                                    'Images' => 'images',
                                    'Videos' => 'videos',
                                ],
                            ],
                            'orderBy' => [
                                'label' => 'Order By',
                                'type' => 'select',
                                'default' => 'MediaMetadata.creation_time desc',
                                'options' => [
                                    'Date Desc' => 'MediaMetadata.creation_time desc',
                                    'Date Asc' => 'MediaMetadata.creation_time',
                                ]
                            ],
                            'cache' => $this->cacheField(),
                        ],
                    ],

                    'extensions' => [
                        'call' => [
                            'func' => __CLASS__ . '::resolve',
                            'args' => [
                                'source_id' => $this->source->id(),
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

    public static function resolve($root, array $args, $ctx)
    {
        $source = self::loadSource($args, GooglePhotosSource::class);
        $albumId = $args['albumId'] ?? '';

        if (!$source || empty($albumId)) {
            return [];
        }

        return self::resolveFromCache($source, $args, function () use ($source, $args) {
            return (array) $source->api()->mediaItemsSearch($args);
        });
    }
}
