<?php

use App\Events\SaveDataEvent;
use Illuminate\Database\Seeder;
use App\Fortuneteller;

class FixThumb extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $filePath = public_path('uploads/files/');
        $key = "87252hahagawbj";

        $listTeller = Fortuneteller::where('is_deleted', false)->get();
        foreach ($listTeller as $item) {
            $avatar = $item->avatar;
            if (!file_exists($filePath . $avatar)) continue;

            try {
                $webpFile = str_replace(".jpg" . $key, "", $avatar . $key);
                $webpFile = str_replace(".png" . $key, "", $webpFile);
                $webpFile = str_replace(".jpeg" . $key, "", $webpFile);
                $webpFile = str_replace(".PNG" . $key, "", $webpFile);
                $webpFile = str_replace(".JPG" . $key, "", $webpFile);
                $webpFile = str_replace(".JPEG" . $key, "", $webpFile);
                $webpFile .= ".webp";

                if (!file_exists($filePath . $webpFile)) {
                    ImageCross::make($filePath . $avatar)->encode('webp', 90)->save($filePath . $webpFile);
                    print "[Thumbnail] Webp: " . $webpFile . "\r\n";
                }

                $this->genThumb($avatar, 60, 60);
                $this->genThumb($avatar, 205, 205);
                $this->genThumb($avatar, 260, 356);
                $this->genThumb($avatar, 100, 60);
                $this->genThumb($avatar, 'auto', 260);
            } catch (Exception $exception) {
                print $exception->getMessage();
            }
        }

        event(new SaveDataEvent());
    }

    private function fileExtension($s)
    {
        $n = strrpos($s, ".");
        return ($n === false) ? "" : substr($s, $n + 1);
    }

    private function genThumb($fileName, $width, $height) {
        $filePath = public_path('uploads/files/');
        $key = "87252hahagawbj";

        $extension = "." . $this->fileExtension($fileName);
        $baseName = basename($fileName);
        $baseName = str_replace($extension . $key, "", $baseName . $key);
        $nameFile = $baseName . "_" . $width . "x" . $height . $extension;

        if (!file_exists($filePath . $nameFile)) {
            $imgCross = ImageCross::make($filePath . $fileName)->orientate();
            if ($width == "auto" && is_numeric($height)) {
                $imgCross->fit(null, $height, function ($constraint) {
                    $constraint->upsize();
                })->save($filePath . $nameFile);
            } else if ($height == "auto" && is_numeric($width)) {
                $imgCross->fit($width, null, function ($constraint) {
                    $constraint->upsize();
                })->save($filePath . $nameFile);
            } else {
                $imgCross->fit($width, $height, function ($constraint) {
                    $constraint->upsize();
                })->save($filePath . $nameFile);
            }
            print "[Thumbnail] " . $nameFile . "\r\n";
        }

        $webpFile = str_replace(".jpg" . $key, "", $nameFile . $key);
        $webpFile = str_replace(".png" . $key, "", $webpFile);
        $webpFile = str_replace(".jpeg" . $key, "", $webpFile);
        $webpFile = str_replace(".PNG" . $key, "", $webpFile);
        $webpFile = str_replace(".JPG" . $key, "", $webpFile);
        $webpFile = str_replace(".JPEG" . $key, "", $webpFile);
        $webpFile .= ".webp";

        if (!file_exists($filePath . $webpFile)) {
            ImageCross::make($filePath . $nameFile)->encode('webp', 90)->save($filePath . $webpFile);
            print "[Thumbnail] Webp: " . $webpFile . "\r\n";
        }
    }
}
