<?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 YOOtheme\GraphQL\Type\Definition\ResolveInfo;
use ZOOlanders\YOOessentials\Source\GraphQL\TypeInterface;

class GooglePhotosMediaMetadataType implements TypeInterface
{
    public const TYPE_NAME = 'GooglePhotosMediaMetadata';
    public const TYPE_LABEL = 'Metadata';

    public function type(): string
    {
        return TypeInterface::TYPE_OBJECT;
    }

    public function name(): string
    {
        return self::TYPE_NAME;
    }

    public function label(): string
    {
        return self::TYPE_LABEL;
    }

    public function config(): array
    {
        return [
            'fields' => [
                'width' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Width',
                    ],
                ],
                'height' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Height',
                    ],
                ],
                'creationTime' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Creation Time',
                        'filters' => ['date'],
                    ],
                ],
                'cameraMake' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Camera Brand',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
                'cameraModel' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Camera Model',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
                'isoEquivalent' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Camera ISO',
                        'group' => 'Photo',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
                'focalLength' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Focal Length',
                        'group' => 'Photo',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
                'apertureFNumber' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Aperture F',
                        'group' => 'Photo',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
                'exposureTime' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Exposure Time (s)',
                        'group' => 'Photo',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
                'fps' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Frame Rate',
                        'group' => 'Video',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveField',
                    ],
                ],
            ],
            'metadata' => [
                'type' => true,
                'label' => $this->label(),
            ],
        ];
    }

    public static function resolveField(
        array $media,
        $args = [],
        $context = '',
        ResolveInfo $info = null
    ): string {
        $data = $metadata['photo'] ?? $metadata['video'] ?? [];

        return $data[$info->fieldName] ?? '';
    }
}
