<?php

namespace App\Http\Controllers\GUI;

use App\Exceptions\HTTPException;
use App\Http\Controllers\Controller;
use App\Http\Resources\oAuth\AccessToken;
use App\Jobs\Mails\ValidateMailAddressJob;
use App\Models\App;
use App\Models\Invite;
use App\Models\Mail;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    public function inviteView() {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $invites = Invite::query()->get();
        return view('admin/invites_list', ["invites"=>$invites]);
    }
    public function inviteNewView() {
        return view('admin/invites_new', []);
    }

    public function settingsView() {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $settings = Setting::query()->get("*");
        return view('admin/settings_list', ["settings"=>$settings]);
    }
    public function resendValidationMail(Request $request) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $mail = Mail::query()->where("id", "=", $request->input("id"))->firstOrFail();

        $this->dispatch(new ValidateMailAddressJob($mail));

        return "OK";
    }
    public function saveSettings(Request $request) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $settings = Setting::query()->get("*");

        foreach($settings as $setting) {
            switch ($setting->typ) {
                case "checkbox":
                    if($request->input($setting->name, false)) {
                        $setting->value = true;
                    } else {
                        $setting->value = false;
                    }
                    $setting->saveOrFail();
                case "textinput":
                    $setting->value = $request->input($setting->name, "");
                    $setting->saveOrFail();
                case "password":
                    $setting->value = $request->input($setting->name, "");
                    $setting->saveOrFail();
            }
        }
        $settings = Setting::query()->get("*");
        return view('admin/settings_list', ["settings"=>$settings]);
    }

    public function appList() {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }


        $apps = App::query()->get("*");
        return view('admin/app_list', ["msg"=>"", "apps" => $apps]);
    }

    public function appDetails($id) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }


        $app = App::query()->where("id", "=", $id)->first("*");
        return view('admin/app_details', ["msg"=>"", "app" => $app]);
    }

    public function appDetailsSave(Request $request, $id) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }

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

        $app->auto_accept = (bool)$request->input("auto_accept", false);
        $app->testing_warning = (bool)$request->input("testing_warning", false);
        $app->untrusted_warning = (bool)$request->input("untrusted_warning", false);
        $app->show_on_webpage = (bool)$request->input("show_on_webpage", false);
        $app->hidden_in_app_list = (bool)$request->input("hidden_in_app_list", false);
        $app->user_cant_remove_app = (bool)$request->input("user_cant_remove_app", false);
        $app->stop_auto_redirect = (bool)$request->input("stop_auto_redirect", false);
        $app->saveOrFail();

        $app = App::query()->where("id", "=", $id)->first("*");
        return view('admin/app_details', ["msg"=>"", "app" => $app]);
    }

    public function inviteNew(Request $request) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }

        $invite = new Invite();
        $invite->user_id = Auth::user()->id;
        $invite->username = $request->input("username", null);
        $invite->comment = $request->input("comment", null);
        $invite->status = "active";
        $invite->createToken();
        $invite->saveOrFail();


        return redirect("/gui/admin/invites");
    }


    public function listUser() {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $users = User::query()->get();

        return view('admin/user_list', ["msg"=>"", "users" => $users]);
    }

    public function userDetails($id) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $user = User::query()->where("id", "=", $id)->firstOrFail();

        $mails = Mail::query()->where("user_id", "=", $id)->get();

        return view('admin/user_details', ["msg"=>"", "editUser" => $user, "mails" => $mails]);
    }

    public function saveUserDetails(Request $request, $id) {
        if(!Auth::user()->admin) {
            throw new HTTPException("Need Admin Access");
        }
        $user = User::query()->where("id", "=", $id)->firstOrFail();

        $user->admin = (bool)$request->input("admin", false);
        $user->developer = (bool)$request->input("developer", false);
        $user->username = $request->input("username");

        $user->saveOrFail();

        return redirect("/gui/admin/users/".$id);
    }


    //
}