<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;

use App\Traits\PhotosTrait;
use App\Traits\ContentElementTrait;
use App\Traits\AppendAttributesTrait;
use App\Utilities\PageLink;

use App\Contracts\SearchResultContract;
use App\Utilities\SearchResult;

use App\Models\FileUpload;

use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use FFMpeg\Format\Video\X264;
use App\Jobs\CreateVideoSizes;

use App\Models\VideoText;

class EmbedVideo extends Model
{
    use HasFactory;
    use ContentElementTrait;
    use PhotosTrait;
    use AppendAttributesTrait;

    protected $with = ['photos', 'fileUpload', 'videoTexts'];
    protected $appends = ['banner', 'url', 'url_retina', 'url_large', 'url_medium', 'url_small', 'url_hls'];

    protected $cast = [
        'fill' => 'boolean',
        'show_text' => 'boolean',
    ];

    public function saveContent(array $input, $id = null)
    {
        if ($id >= 1) {
            $embed_video = EmbedVideo::findOrFail($id);
            $duplicate_photos = false;
        } else {
            $embed_video = new EmbedVideo();
            $duplicate_photos = true;
        }

        $file_upload = FileUpload::find(Arr::get($input, 'file_upload_id'));

        $embed_video->file_upload_id = $file_upload?->id;
        $embed_video->header = Arr::get($input, 'header');
        $embed_video->body = Arr::get($input, 'body');
        $embed_video->layout = Arr::get($input, 'layout');
        $embed_video->fill = Arr::get($input, 'fill');
        $embed_video->show_text = Arr::get($input, 'show_text');
        $embed_video->text_style = Arr::get($input, 'text_style');
        $embed_video->number = Arr::get($input, 'number');

        $embed_video->save();

        $embed_video->saveSinglePhoto($input, $duplicate_photos);
        $embed_video->saveVideoTexts($input, $id ? false : true);

        cache()->tags([cache_name($embed_video)])->flush();
        return $embed_video;
    }

    public function saveVideoTexts($input, $new_version = false)
    {
        if (Arr::get($input, 'video_texts')) {
            foreach (Arr::get($input, 'video_texts') as $video_text_input) {
                $video_text = (new VideoText())->saveVideoText($video_text_input, $this, $new_version ? null : Arr::get($video_text_input, 'id'));
            }
        }
    }

    public function fileUpload()
    {
        return $this->belongsTo(FileUpload::class);
    }

    public function getBannerAttribute()
    {
        return $this->photos->first();
    }

    public function getBodyAttribute($value)
    {
        return $this->convertAttributeLinks($value, 'body');
    }

    public function getSearchFieldsAttribute()
    {
        return [
            'header',
            'body',
        ];
    }

    public function getSearchResultTitle($default = null)
    {
        if (mb_strlen($this->header) > 0) {
            return $this->header;
        }
        return $default;
    }

    public function getSearchResultPreview($terms)
    {
        return SearchResult::truncatePreview(strip_tags($this->body) ? $this->body : $this->header, $terms);
    }

    public function getSearchResultRank($terms)
    {
        $rank = 0;
        foreach ($terms as $term) {
            $rank += mb_substr_count($this->header, $term) * $this->headerRankMultiplier;
            $rank += mb_substr_count($this->body, $term) * $this->bodyRankMultiplier;
        }
        return $rank;
    }

    public function createSizes($size)
    {
        if (Storage::missing($this->fileUpload->storage_filename)) {
            //Log::info('MISSING FILEUPLOAD: '.$this->fileUpload->storage_filename);
            return null; // TODO we got a problem, the original upload is gone
        }

        $sizes = collect([
            'small' => [ 'width' => 576, 'height' => 244, 'bitrate' => 500],
            'medium' => [ 'width' => 768, 'height' => 326, 'bitrate' => 800],
            'large' => [ 'width' => 1152, 'height' => 490, 'bitrate' => 1500],
            'retina' => [ 'width' => 1920, 'height' => 816, 'bitrate' => 3000],
            'high' => [ 'width' => 1920, 'height' => 816, 'bitrate' => 5000],
        ]);

        if ($size === 'hls') {
            $file_name = $this->getFilename().'.m3u8';

            if (Storage::missing($file_name)) {
                $hls = FFMpeg::open($this->fileUpload->storage_filename)
                    ->exportForHLS()
                    ->toDisk('public')
                    ->setSegmentLength(2) // optional
                    ->setKeyFrameInterval(60); // optional

                foreach ($sizes as $name => $data) {
                    $hls->addFormat((new X264())->setKiloBitrate($data['bitrate']), function ($media) use ($data) {
                        $media->scale($data['width'], $data['height']);
                    });
                }

                //Log::info('PROCESSING VIDEO '.$this->id.'::'.$file_name);

                $hls->save($file_name);
            } else {
                //Log::info('NOT MISSING  '.$file_name);
            }
        } else {
            $data = $sizes[$size];

            if ($data) {
                $file_name = $this->getFilename().'_'.$size.'.mp4';

                if (Storage::missing($file_name)) {
                    $bitrate = (new X264())->setKiloBitrate($data['bitrate']);

                    //Log::info('PROCESSING VIDEO '.$this->id.'::'.$file_name);

                    FFMpeg::open($this->fileUpload->storage_filename)
                        ->resize($data['width'], $data['height'], 'fit', true)
                        ->export()
                        ->toDisk('public')
                        ->inFormat($bitrate)
                        ->save($file_name);
                } else {
                    //Log::info('NOT MISSING  '.$file_name);
                }
            }
        }
    }

    protected function getUrl($size = null)
    {
        if (!$this->fileUpload) {
            return null;
        }

        if (Storage::missing($this->fileUpload->storage_filename)) {
            return null; // TODO Send a notifaction to admin about missing file
        }

        if ($size === 'hls') {
            $url = $this->getFilename().'.m3u8';
        } else {
            $url = $this->getFilename().($size ? '_'.$size : '').'.mp4';
        }

        if (Storage::disk('public')->missing($url)) {
            if ($size) {
                //Log::info('CREATE VIDEO '.$this->id.': '.$size);
                CreateVideoSizes::dispatch($this, $size);
                return null;
            } else {
                Storage::disk('public')->put($url, Storage::get($this->fileUpload->storage_filename));
                return $url;
            }
        } else {
            //Log::info('FILE EXISTS: '.$url);
        }
        return $url;
    }

    protected function getFilename()
    {
        return Str::replace(' ', '-', 'videos/'.$this->fileUpload->id.'/'.$this->fileUpload->filename);
    }

    public function getUrlAttribute()
    {
        return $this->getUrl();
    }

    public function getUrlHlsAttribute()
    {
        return $this->getUrl('hls');
    }

    public function getUrlRetinaAttribute()
    {
        return $this->getUrl('retina');
    }

    public function getUrlLargeAttribute()
    {
        return $this->getUrl('large');
    }

    public function getUrlMediumAttribute()
    {
        return $this->getUrl('medium');
    }

    public function getUrlSmallAttribute()
    {
        return $this->getUrl('small');
    }

    public function videoTexts()
    {
        return $this->hasMany(VideoText::class);
    }

    public function regenerate($clear = false)
    {
        $sizes = collect([
            'hls',
            'retina',
            'large',
            'medium',
            'small',
        ]);

        if ($clear) {
            $directory = 'videos/'.$this->fileUpload->id;
            if (Storage::disk('public')->exists($directory)) {
                Storage::disk('public')->deleteDirectory($directory);
            }
        }

        $this->refresh();
        cache()->tags([cache_name($this)])->flush();

        $this->getUrl();
        foreach ($sizes as $size) {
            $this->getUrl($size);
        }
    }
}
