<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use App\Helpers\UploadHelper;
use App\Helpers\UploadUrlHelper;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\FileUploadRequest;
use App\Http\Requests\FileUploadUrlRequest;
use App\Http\Requests\FetchFileRequest;
use App\Helpers\FileFetchHelper;
use Illuminate\Http\Request;

class UploadController extends Controller
{
    public function upload(): View
    {
        return view('user.upload.index', [
            'functions' => 'user.upload.function',
            'sidebar' => 'upload',
            'pageName' => pageName([__('lang.upload_file')]),
        ]);
    }

    public function upload_post(
        FileUploadRequest $request
    ): JsonResponse|Response {
        try {
            $errors = $request->failedValidation(
                $request->getValidator()
            );
            if ($errors) {
                return response()->json([
                    'result' => false,
                    'errors' => $errors, 
                ]);
            };

            $formData = $request->only(
                'type',
                'compress',
                'size.width',
                'size.height',
                'rotation-reflect',
                'watermark',
                'preferences.brightness',
                'preferences.contrast',
                'preferences.gamma',
                'preferences.red',
                'preferences.green',
                'preferences.blue',
                'effects.grayscale',
                'effects.blur',
                'effects.invert',
                'effects.pixelation',
                'effects.reduce',
                'auto-remove',
                'password',
                'exif.basic',
                'exif.deep',
            );

            $uploader = new UploadHelper();
            $file = $request->file('file');
            $upload = $uploader->upload(
                $file,
                $formData
            );

            if ($upload[0]) {
                $slug = pageSlug('file');
                return response()->json([
                    'result' => true,
                    'key' => $upload[1], 
                    'url' => url("/{$slug}/{$upload[1]}"),
                    'thumb' => $upload[2],
                ]);
            }
    
            return response()->json([
                'result' => false,
                'data' => $upload[1], 
            ]);
        } catch (\Exception $e) {
            Log::error($e);
            return response()->json([
                'result' => false,
                'data' => __('lang.error'), 
            ]);
        }
    }

    public function upload_link(
        FileUploadUrlRequest $request
    ): JsonResponse {
        try {
            $errors = $request->failedValidation(
                $request->getValidator()
            );
            if ($errors) {
                return response()->json([
                    'result' => false,
                    'errors' => $errors, 
                ]);
            };
        
            $formData = $request->only(
                'file-url',
                'type',
                'compress',
                'size.width',
                'size.height',
                'rotation-reflect',
                'watermark',
                'preferences.brightness',
                'preferences.contrast',
                'preferences.gamma',
                'preferences.red',
                'preferences.green',
                'preferences.blue',
                'effects.grayscale',
                'effects.blur',
                'effects.invert',
                'effects.pixelation',
                'effects.reduce',
                'auto-remove',
                'password',
                'exif',
                'exif.deep',
            );

            $uploadUrlHelper = new UploadUrlHelper();
            $fileUrl = $request->input('file-url');
            $upload = $uploadUrlHelper->upload(
                $fileUrl,
                $formData
            );

            if ($upload[0]) {
                $slug = pageSlug('file');
                return response()->json([
                    'result' => true,
                    'key' => $upload[1], 
                    'url' => url("/{$slug}/{$upload[1]}"),
                    'thumb' => $upload[2],
                ]);
            }
    
            return response()->json([
                'result' => false,
                'data' => $upload[1], 
            ]);

        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return response()->json([
                'result' => false,
                'data' => __('lang.error'), 
            ]);
        }
    }

    public function get_file_data(
        FetchFileRequest $request
    ): JsonResponse {
        $errors = $request->failedValidation(
            $request->getValidator()
        );
        if ($errors) {
            return response()->json([
                'result' => false,
                'errors' => $errors, 
            ]);
        };

        $formData = $request->only(
            'url',
        );

        try {
            $fileFetcher = new FileFetchHelper();
            $result = $fileFetcher->get($formData['url']);

            if ($result[0]) {
                return response()->json([
                    'result' => true,
                    'data' => $result[1] ?? false,
                    'fileName' => $result[2] ?? false,
                    'fileSizeBytes' => $result[3] ?? false,
                    'fileUrl' => $result[4] ?? false,
                ]);
            }

            return response()->json([
                'result' => false,
                'data' => $result[1], 
            ]);

        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return response()->json([
                'result' => false,
                'data' => __('lang.error'), 
            ]);
        }
    }

    public function fetch_file(
        Request $request
    ): mixed {
        try {
            $url = $request->input('url');
            $fileFetcher = new FileFetchHelper();
            $data = $fileFetcher->fetch($url);

            if ($data[0]) {
                return response()->json([
                    'result' => true,
                    'data' => __('lang.file_added'),
                    'file' => $data[1],
                    'mime' => $data[2],
                ]);
            }

            return response()->json([
                'result' => false,
                'data' => __('lang.error'), 
                'error' => $data[1],
            ]);
        } catch (\Exception $e) {
            Log::error($e);
            return response()->json([
                'result' => false,
                'data' => __('lang.error'), 
            ]);
        }

    }

}
