<?php

use Illuminate\Database\Seeder;
use App\Fortunemethod;
use App\Area;

class DrawThumb extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $key = "87252hahagawbj";
        $filePath = public_path('uploads/files/');
        $files = ["draw.png", "map3.png", "logo-qole.png", "top_bg.jpg", "loading.gif"];
        foreach ($files as $it) {
            ImageCross::make(public_path('uploads/') . $it)->orientate()->save($filePath . $it);
        }

        try {
            $files[] = $this->genThumb("draw.png", 225, 110);
            $files[] = $this->genThumb("draw.png", 195, 95);
            $files[] = $this->genThumb("map3.png", 48, 48);
            $files[] = $this->genThumb("loading.gif", 50, 50);
        } catch (Exception $exception) {
            print $exception->getMessage();
        }

        //Method Image
        $allMethods = Fortunemethod::all();
        $methodPath = public_path('uploads/image/method/');
        foreach ($allMethods as $method) {
            if ($method->logo != null && $method->logo != "" && file_exists($methodPath . $method->logo)) {
                ImageCross::make($methodPath . $method->logo)->orientate()->save($filePath . $method->logo);
                $files[] = $this->genThumb($method->logo, 150, 150);
                $files[] = $this->genThumb($method->logo, 48, 48);
            }
        }

        //Area Image
        $allAreas = Area::all();
        $areaPath = public_path('uploads/image/area/');
        foreach ($allAreas as $area) {
            if ($area->logo != null && $area->logo != "" && file_exists($areaPath . $area->logo)) {
                ImageCross::make($areaPath . $area->logo)->orientate()->save($filePath . $area->logo);
                $files[] = $this->genThumb($area->logo, 70, 70);
            }
        }

        //Convert to Webp
        foreach ($files as $file) {
            $newName = basename($file);
            $newName = str_replace(".jpg" . $key, "", $newName . $key);
            $newName = str_replace(".png" . $key, "", $newName);
            $newName = str_replace(".jpeg" . $key, "", $newName);
            $newName = str_replace(".PNG" . $key, "", $newName);
            $newName = str_replace(".JPG" . $key, "", $newName);
            $newName = str_replace(".JPEG" . $key, "", $newName);
            $newName = str_replace(".gif" . $key, "", $newName);
            $newName .= ".webp";
            ImageCross::make($filePath . $file)->encode('webp', 90)->save($filePath . $newName);
            print "Converted: " . $newName . "\r\n";
        }
    }

    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;

        $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";
        return $nameFile;
    }
}
