<?php

namespace Animekyun\Images\Screenshots;

use Animekyun\Repositories\EpisodeRepository;
use Illuminate\Support\Collection;

class ScreenshotLocator
{

    protected $size = 'src';
    protected $episode;
    protected $collection;
    protected $placeholder = true;

    public function __construct(EpisodeRepository $episodeRepository, Collection $collection)
    {
        $this->episodeRepository = $episodeRepository;
        $this->collection = $collection;
    }

    /**
     * Bind the episode model to the class
     * @param $episode \Episode|int the episode model or the id of the episode
     * @return $this
     */
    public function episode($episode)
    {
        if (get_class($episode) == 'Episode') {
            $this->episode = $episode;
        } else {
            $this->episode = $this->episodeRepository->getById($episode);
        }
        return $this;
    }

    /**
     * @param bool $bool set to true if placeholder should be returned
     * @return $this
     */
    public function placeholder($bool = true)
    {
        $this->placeholder = $bool;
        return $this;
    }

    /**
     * Set the size of the to be located screenshot
     * @param string $size set to src, xlarge, large, medium, or small
     * @return $this
     */
    public function size($size = 'src')
    {
        $this->size = $size;
        return $this;
    }

    /**
     * @param bool $local true if full path should be returned. False returns uri
     * @param int $index the index of the image to be returned
     * @return array
     */
    public function get($local, $index = null)
    {
        if (!$index) {
            $pathArr = array_map(function ($item) use ($local) {
                return $this->getFilepath($item, $local);
            }, (array)$this->getCollection($index));
            return $pathArr;
        }
        $collection = $this->getCollection($index);
        return $this->getFilepath($collection, $local);
    }

    /**
     * Generates the filepath to the screenshot or placeholder. It gets either the full path or the uri
     * @param string $uri
     * @param bool $local set to true if the full path should be returned
     * @return bool|string
     */
    private function getFilepath($uri, $local)
    {
        if (!\File::isFile(public_path() . $uri) && $this->placeholder) {
            if ($local) {
                return \Config::get('images.screenshots_placeholder_local');
            }
            return \Config::get('images.screenshots_placeholder_public');
        } elseif (!\File::isFile(public_path() . $uri) && !$this->placeholder) {
            return false;
        }

        if ($local) {
            return public_path() . $uri;
        }
        return $uri;
    }

    /**
     * Get the collection of screenshots
     * @param int $index the key of the screenshot to grab
     * @return array|string
     */
    protected function getCollection($index)
    {
        try {
            $files = \File::allFiles($this->getThumbnailsFolder());
        } catch (\Exception $e) {
            return false; // folder does not exist or there are no screenshots.
        }

        $fileCollection = $this->collection->make(array_map(function ($file) {
            return str_replace(public_path(), '', $file->getRealPath()); // chop off system path
        }, $files));

        if ($index == 'random') {
            return $fileCollection->random();
        } elseif (is_int($index)) {
            return $fileCollection->get($index);
        }
        return $fileCollection->all();
    }

    public function getThumbnailsFolder()
    {
        if ($this->size == 'src') {
            return $this->getSourceFolder();
        }
        return \Config::get('screenshots.thumbs_local') . '/'
        . $this->episode->show->slug . '/'
        . $this->episode->episode_number . '/'
        . $this->size;
    }

    public function getSourceFolder()
    {
        return \Config::get('screenshots.src_local') . '/'
        . $this->episode->show->slug . '/'
        . $this->episode->episode_number;
    }

}