<?php

namespace App\Http\Controllers\GUI;

use App\Http\Controllers\Controller;
use App\Http\Resources\oAuth\AccessToken;
use App\Models\App;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AppController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function appList() {
        if(!Auth::check()) {
            abort(401);
        }
        $apps = App::query()->where("user_id", "=", Auth::user()->id)->get();

        return view('app/list', ["msg"=>"", "apps" => $apps]);
    }
    public function newAppView() {
        return view('app/new', ["msg"=>""]);
    }
    public function newApp(Request $request) {
        $this->validate($request, [
            'name' => 'required|max:255|min:3|regex:@^[a-zA-Z0-9]*$@|unique:apps',
            'description' => 'required|min:3',
            'url' => 'required|url'
        ]);

        $app = App::createApp($request->input("name"), htmlspecialchars($request->input("description")), $request->input("url"), Auth::user());

        return "App created";
    }

    public function viewApp(Request $request, $id) {
        $app = App::query()->where("id", "=", $id)->first();

        if($app->user_id != Auth::user()->id) {
            abort(401);
        }

        return view('app/details', ["msg"=>"", "app" => $app]);
    }

    public function updateApp(Request $request, $id) {
        $this->validate($request, [
            'name' => 'required|max:255|min:3|regex:@^[a-zA-Z0-9]*$@',
            'description' => 'required|min:3',
            'url' => 'required|url',
            'direct_url' => 'url'
        ]);

        // ToDO Unique App Name


        $app = App::query()->where("id", "=", $id)->first();

        if($app->user_id != Auth::user()->id) {
            abort(401);
        }

        $app->name = $request->input("name");
        $app->description = htmlspecialchars($request->input("description"));
        $app->url = $request->input("url");
        $app->direct_url = $request->input("direct_url");

        $app->saveOrFail();
        return redirect('/gui/apps/'.$id);
    }

    public function changeIcon(Request $request, $id) {
        // Todo: Replace prove of concept with better code
        $newTmp = tempnam("", "icon_upload");
        $request->file("icon")->move("/tmp", $newTmp);
        $info = getimagesize($newTmp);
        if($info["0"] != $info["1"]) {
            echo "Icon is not a squader. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
        }

        if($info[0] > 500) {
            echo "Icon is to big, max 500 px. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
        }
        if($info[0] < 50) {
            echo "Icon is to small, min 50px. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
        }

        $app = App::query()->where("id", "=", $id)->firstOrFail();
        if($app->user_id != Auth::user()->id) {
            echo "Its not your app. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
        }

        $app->icon = file_get_contents($newTmp);
        $app->saveOrFail();
        return redirect('/gui/apps/'.$id);
    }

    public function getAppIcon($id) {
        $app = App::query()->where("id", "=", $id)->firstOrFail();

        if(empty($app->icon)) {
            $app->icon = file_get_contents(resource_path("images/app.png"));
        }

        $r = getimagesizefromstring($app->icon);

        return response($app->icon)
            ->header('Content-Type',$r["mime"]);
    }

    public function regenerateAppSecret(Request $request, $id) {
        $app = App::query()->where("id", "=", $id)->firstOrFail();

        if($app->user_id != Auth::user()->id) {
            abort(401);
        }

        $app->regenerateApiSecret();
        $app->saveOrFail();

        return redirect('/gui/apps/'.$id);
    }
}