<?php

namespace App\Livewire;

use App\Models\Downloads;
use Livewire\Component;

class DownloadButton extends Component
{
    public $downloadUrl;

    public $downloadId;

    public $downloadCount;

    public $episodeNumber;

    public $fillNumbers;

    public $fileSize;

    public $background = 'bg-rose-600';

    public $fileExtension = 'HEVC';

    public $version = '';

    public function mount()
    {
        if (str_contains($this->downloadUrl, 'AV1')) {
            $this->fileExtension = 'AV1';
        }

        if (str_contains($this->downloadUrl, 'v2')) {
            $this->version = 'v2';
        }

        if (str_contains($this->downloadUrl, 'v3')) {
            $this->version = 'v3';
        }
    }

    public function clicked($downloadId)
    {
        $download = Downloads::find($downloadId);
        if (! $download) {
            return;
        }

        $download->count++;
        $download->save();

        $this->downloadCount = $download->count;
        cache()->forget("episode_{$download->episode->id}_download_{$download->type}");
    }

    public function render()
    {
        return view('livewire.download-button');
    }
}
