<?php

namespace App\Http\Controllers;

use App\Models\Entry;
use App\Models\EntryFile;
use App\Services\FileServersService;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;

class ToolsController extends Controller
{

    public function patcher()
    {
        return view('tools.patcher');
    }

    public function directPatch( Request $request, int $entryId, EntryFile $file )
    {
        if( $file->entry_id != $entryId || $file->state === 'private' ) {
            abort(404);
        }

        $service = app(FileServersService::class);


        $patches = [
            'file' => $service->getDownloadFileUrl( $file ),
            'name' => $file->entry->title,
            'outputName' => $file->filename
        ];


        SEOTools::setTitle('ROM Patcher');
        return view('tools.patcher', compact('patches'));
    }

    public function play( Request $request, int $entryId, EntryFile $file )
    {
        if( $file->entry_id != $entryId || $file->state === 'private' ) {
            abort(404);
        }

        $service = app(FileServersService::class);

        $emuConfig = [
            'core' => $file->playOnlineSetting?->core,
            'threads' => $file->playOnlineSetting?->threads,
        ];

        if( $file->entry->type === 'homebrew' ){
            $filePath = $service->getDownloadFileUrl( $file );

            return view('tools.play-homebrew', compact('filePath', 'emuConfig'));
        }
        $patches = [
            'file' => $service->getDownloadFileUrl( $file ),
            'name' => $file->entry->title,
            'outputName' => $file->filename
        ];

        SEOTools::setTitle('Play online');
        return view('tools.play', compact('patches', 'emuConfig'));
    }

    public function hasher( Request $request )
    {
        SEOTools::setTitle('Play online');
        return view('tools.hasher');
    }

}
