<?php

namespace App\Http\Controllers;

use App\Resource;
use App\ResourceCategory;
use App\ResourceExtension;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\Mime\MimeTypes;

class ResourceController extends Controller
{
    public function __construct()
    {
        $this->middleware([ "auth" ]);
        $this->middleware([ "lang" ]);

        $this->middleware([ "check.auth:resource.list" ])->only("index");
        $this->middleware([ "check.auth:resource.show" ])->only("show");
        $this->middleware([ "check.auth:resource.create" ])->only("create", "store");
        $this->middleware([ "check.auth:resource.edit" ])->only("edit", "update");
        $this->middleware([ "check.auth:resource.delete" ])->only("delete");
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $resources = Resource::query()->paginate($request->input("limit", 20));

        return Response::detect("resources.index", [ "resources" => $resources ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Response::detect("resources.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public static function store(Request $request)
    {
        $file = $request->file("resource");

        $resourceExtension = ResourceExtension::query()->where("extension", "=", $file->extension())->first();

        //Create resourceExtension if it doesn't exist
        if($resourceExtension === null) {
            $category = substr($file->getMimeType(), 0, strpos($file->getMimeType(), "/"));

            $resourceCategory = ResourceCategory::query()->where("name", "=", $category)->first();

            //Create resourceCategory if it doesn't exist
            if($resourceCategory === null){
                $data = [
                    "name" => $category,
                    "description" => "",
                    "slug" => ""
                ];

                $resourceCategory = new ResourceCategory($data);
                $resourceCategory->save();
            }

            $data = [
                "extension" => $file->extension(),
                "description" => "",
                "resource_category_id" => $resourceCategory->id
            ];

            $resourceExtension = new ResourceExtension($data);
            $resourceExtension->save();
        }

        $resource = new Resource();

        $resource->extension_id = $resourceExtension->id;

        $fileName = time().'_'.$file->getClientOriginalName();
        $filePath = $file->storeAs('uploads', $fileName, 'public');

        /* /uploads/filename.ext */
        $resource->filename = '/' . $filePath;


        $resource->save();

        return $resource;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public static function storeMime(Request $request, ResourceCategory $category)
    {
        $file = $request->file("resource");

        $resourceExtension = ResourceExtension::query()->where("extension", "=", $file->extension())->first();

        //Create resourceExtension if it doesn't exist
        if($resourceExtension === null) {
            $category = substr($file->getMimeType(), 0, strpos($file->getMimeType(), "/"));

            $resourceCategory = ResourceCategory::query()->where("name", "=", $category)->first();

            //If the uploaded file doesn't match the requested mime type
            if($resourceCategory) {
                if ($resourceCategory->name !== $category)
                    return null;
            } else {
                return null;
            }

            //Create resourceCategory if it doesn't exist
            if($resourceCategory === null){
                $data = [
                    "name" => $category,
                    "description" => "",
                    "slug" => ""
                ];

                $resourceCategory = new ResourceCategory($data);
                $resourceCategory->save();
            }

            $data = [
                "extension" => $file->extension(),
                "description" => "",
                "resource_category_id" => $resourceCategory->id
            ];

            $resourceExtension = new ResourceExtension($data);
            $resourceExtension->save();
        }

        $resource = new Resource();

        $resource->extension_id = $resourceExtension->id;

        $fileName = time().'_'.$file->getClientOriginalName();
        $filePath = $file->storeAs('uploads', $fileName, 'public');

        /* /uploads/filename.ext */
        $resource->filename = '/' . $filePath;


        $resource->save();

        return $resource;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

    }
}
