<?php

namespace Aweos\Resizer\Tests;

use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase;
use Cache;
use Event;
use Illuminate\Http\UploadedFile;
use Storage;

class MoveTest extends TestCase
{

    public function setUp(): void
    {
        parent::setUp();
        Storage::fake('local');
    }

    public function testItMovesAllVersionsOfAFile() 
    {
        Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->set('resizer.original.pages/alt/test.jpg', 'aa');
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);
        $this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
        UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages/alt', 'test-200x300.jpg', 'local');

        Event::fire('media.file.move', [null, '/pages/alt/test.jpg', '/pages/neu/test.jpg']);

        $this->assertFileCount(0, 'pages/alt');
        $this->assertHasFile('pages/neu/test-200x300.jpg');
        $this->assertNull(Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->get('resizer.original.pages/alt/test.jpg'));
    }

    public function testItMovesFilesOnRename() 
    {
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);
        $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local');

        Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);

        $this->assertFileCount(1, 'pages');
        $this->assertHasFile('pages/testneu-200x300.jpg');
    }

    public function testitDeletesOldAndNewCacheWhenMoving() 
    {
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);
        $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local');
        Cache::tags(['resizer', 'resizer.pages/test.jpg'])->put('resizer.original.pages/test.jpg', 'AAA');
        Cache::tags(['resizer', 'resizer.pages/testneu.jpg'])->put('resizer.original.pages/testneu.jpg', 'AAA');

        Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);

        $this->assertFileCount(1, 'pages');
        $this->assertHasFile('pages/testneu-200x300.jpg');
        $this->assertFalse(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->has('resizer.original.pages/test.jpg'));
        $this->assertFalse(Cache::tags(['resizer', 'resizer.pages/testneu.jpg'])->has('resizer.original.pages/test.jpg'));
    }

    public function testRenamePdfHeaderFile() 
    {
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);
        $this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.pdf.jpg', 'local');

        Event::fire('media.file.rename', [null, '/pages/test.pdf', '/pages/testneu.pdf']);

        $this->assertFileCount(1, 'pages');
        $this->assertHasFile('pages/testneu-200x300.pdf.jpg');
    }

    public function testItDoesntMoveOtherFilesInTheSameDirectory() 
    {
        Cache::tags(['resizer', 'resizer.pages/test.jpg'])->set('resizer.original.pages/test.jpg', 'aa');
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);
        $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
        UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local');
        UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'testother-200x300.jpg', 'local');

        Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);

        $this->assertFileCount(2, 'pages');
        $this->assertHasFile('pages/testother-200x300.jpg');
        $this->assertHasFile('pages/testneu-200x300.jpg');
        $this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg'));
    }

}