<?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\Instagram;

use ZOOlanders\YOOessentials\Source\Resolver\HasCacheTimes;
use function YOOtheme\app;
use ZOOlanders\YOOessentials\Api\Instagram\InstagramApiInterface;
use ZOOlanders\YOOessentials\Api\Instagram\InstagramPersonalApi;
use ZOOlanders\YOOessentials\Auth\AuthManager;
use ZOOlanders\YOOessentials\Auth\AuthOAuth;
use ZOOlanders\YOOessentials\Source\Type\AbstractSourceType;
use ZOOlanders\YOOessentials\Source\Type\SourceInterface;

class InstagramSource extends AbstractSourceType implements SourceInterface, HasCacheTimes
{
    public const MEDIA_LIMIT_DEFAULT = 20;
    public const MEDIA_CACHE_TIME_DEFAULT = 3600;

    protected ?InstagramApiInterface $api = null;

    public function types(): array
    {
        if (!$this->auth()) {
            return [];
        }

        return [
            new Type\InstagramMediaType(),
            new Type\InstagramAlbumMediaType(),
            new Type\InstagramMediaQueryType($this),
            new Type\InstagramMediaSingleQueryType($this),
        ];
    }

    public function account(): ?string
    {
        return $this->config()['account'] ?? null;
    }

    public function auth(): ?AuthOAuth
    {
        return app(AuthManager::class)->auth($this->account());
    }

    public function api(): InstagramApiInterface
    {
        if ($this->api !== null) {
            return $this->api;
        }

        return $this->api = app(InstagramPersonalApi::class)->forAccount($this->auth());
    }

    public function defaultCacheTime(): int
    {
        return $this->config('cache_default', HasCacheTimes::DEFAULT_CACHE_TIME);
    }

    public function minCacheTime(): int
    {
        if (!($auth = $this->auth())) {
            return HasCacheTimes::MIN_CACHE_TIME;
        }

        if ($auth instanceof AuthOAuth && $auth->custom()) {
            return 0;
        }

        return HasCacheTimes::MIN_CACHE_TIME;
    }
}
