<?php
namespace App\Translation;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;
use Illuminate\Support\Facades\Http;

class CustomFileLoader extends FileLoader
{
    protected $url;

    public function __construct(Filesystem $files, $path, $url)
    {
        parent::__construct($files, $path);
        $this->url = rtrim($url, '/');
    }

    public function load($locale, $group, $namespace = null)
    {
        if ($group == '*') {
            return $this->loadJsonTranslations($locale);
        }

        return parent::load($locale, $group, $namespace);
    }

    protected function loadJsonTranslations($locale)
    {
        try {
            $response = Http::get("{$this->url}/{$locale}.json");

            if ($response->ok()) {
                return json_decode($response->body(), true);
            }
        } catch (\Exception $e){

        }

        return [];
    }

    /**
     * @inheritDoc
     */
    public function addNamespace($namespace, $hint)
    {
        // TODO: Implement addNamespace() method.
    }

    /**
     * @inheritDoc
     */
    public function addJsonPath($path)
    {
        // TODO: Implement addJsonPath() method.
    }

    /**
     * @inheritDoc
     */
    public function namespaces()
    {
        // TODO: Implement namespaces() method.
    }
}
