<?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\GoogleBusinessProfile\Type;

use ZOOlanders\YOOessentials\Source\GraphQL\AbstractObjectType;
use ZOOlanders\YOOessentials\Source\GraphQL\HasSourceInterface;
use ZOOlanders\YOOessentials\Util;

class GoogleBusinessProfileMediaType extends AbstractObjectType implements HasSourceInterface
{
    public const TYPE_NAME = 'GoogleMyBusinessMedia';
    public const TYPE_LABEL = 'Location Media';

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

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

    public function config(): array
    {
        return [
            'metadata' => [
                'type' => true,
                'label' => $this->label(),
            ],
            'fields' => [
                'name' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'ID',
                    ],
                ],
                'description' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Description',
                        'filters' => ['limit'],
                    ],
                ],
                'mediaFormat' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Format',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveMediaFormat',
                    ],
                ],
                'width' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Width (px)',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveWidth',
                    ],
                ],
                'height' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Height (px)',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveHeight',
                    ],
                ],
                'thumbnailUrl' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Thumbnail URL',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveThumbnailUrl',
                    ],
                ],
                'sourceUrl' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Source URL',
                    ],
                ],
                'googleUrl' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Google URL',
                    ],
                ],
                'createTime' => [
                    'type' => 'String',
                    'metadata' => [
                        'label' => 'Created At',
                        'filters' => ['date'],
                    ],
                ],
                'viewCount' => [
                    'type' => 'Int',
                    'metadata' => [
                        'label' => 'Total Views',
                    ],
                    'extensions' => [
                        'call' => __CLASS__ . '::resolveViewCount',
                    ],
                ],
                'attribution' => [
                    'type' => GoogleBusinessProfileMediaAttributionType::TYPE_NAME,
                    'metadata' => [
                        'label' => 'Attribution',
                    ],
                ],
                'locationAssociation' => [
                    'type' => GoogleBusinessProfileMediaLocationAssociationType::TYPE_NAME,
                    'metadata' => [
                        'label' => 'Location Association',
                    ],
                ],
            ],
        ];
    }

    public static function resolveMediaFormat(array $media): ?string
    {
        return $media['mediaFormat'] ?? null;
    }

    public static function resolveWidth(array $media): ?int
    {
        return $media['dimensions']['widthPixels'] ?? null;
    }

    public static function resolveHeight(array $media): ?int
    {
        return $media['dimensions']['heightPixels'] ?? null;
    }

    public static function resolveViewCount(array $media): ?int
    {
        return $media['insights']['viewCount'] ?? null;
    }

    public static function resolveThumbnailUrl(array $media): ?string
    {
        $name = $media['name'] ?? '';
        $url = $media['thumbnailUrl'] ?? '';

        return Util\File::cacheMedia($url, "gbp-media-$name");
    }
}
