<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Server;
use Gate;
use Symfony\Component\Console\Input\Input;
use Storage;

class ServerController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        
        $servers = Server::All();

        return view('servers.index')->with([
            'servers' => $servers,
        ]);
    }

    public function edit(Server $server)
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        return view('servers.edit')->with([
            'server' => $server
        ]);
    }

    public function update(Request $request, Server $server)
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        $data = request()->validate([
            'ip' => 'required|ip',
            'name' => 'required',
            'scripts' => 'required',
            'templates' => 'required',
            'clones' => 'required',
        ]);

        Server::where('id', $server->id)->first()
            ->update(['name' => $request->name]);
        Server::where('id', $server->id)->first()
            ->update(['ip' => $request->ip]);
        Server::where('id', $server->id)->first()
            ->update(['templates' => $request->templates]);
        Server::where('id', $server->id)->first()
            ->update(['clones' => $request->clones]);
        Server::where('id', $server->id)->first()
            ->update(['scripts' => $request->scripts]);            

        return redirect()->route('servers.index');
    }

    public function destroy(Server $server)
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        $server->delete();

        return redirect()->back();
    }


    public function createESXi()
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        if (Gate::allows('manage-users')) {
            return view('servers.createESXi');
        }
        return redirect('/home');
    }


    public function createHyper()
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        if (Gate::allows('manage-users')) {
            return view('servers.createHyperV');
        }
        return redirect('/home');

    }


    public function storeESXi(Request $request)
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        $data = request()->validate([
            'ip' => 'required|ip',
            'name' => 'required',
            'script_datastore' => 'required',
            'template_datastore' => 'required',
            'clones_datastore' => 'required',
            'import' => 'required',
        ]);   

        $serverDouble = Server::where([
            ['ip', '=', request('ip')]
        ])->get();

        if ($serverDouble->isNotEmpty()) {
            return redirect()->route('servers.index');
        } 
        $file=$request->import->storeAs("keys/".request('ip'), 'id_rsa');
        
        $Server = new \App\server();
        $Server->name = request('name');
        $Server->ip = request('ip');
        $Server->type = "ESXi";
        $Server->private = Storage::disk('local')->path('keys'). DIRECTORY_SEPARATOR . request('ip'). DIRECTORY_SEPARATOR . 'id_rsa';
        
        $Server->scripts = "/vmfs/volumes/".request('script_datastore')."/".request('script_directory');
        $Server->templates = "/vmfs/volumes/".request('template_datastore')."/".request('template_directory');
        $Server->clones = "/vmfs/volumes/".request('clones_datastore')."/".request('clones_directory');
    
        $Server->save();
        return redirect()->route('servers.index');
    }

    public function storeHyper(Request $request)
    {
        if (!(Gate::allows('manage-users'))) {
            return redirect('/home');
        }
        $data = request()->validate([
            'ip' => 'required|ip',
            'name' => 'required',
            'scripts_disk' => 'required|alpha',
            'templates_disk' => 'required|alpha',
            'clones_disk' => 'required|alpha',
            'import' => 'required',
        ]);

        $serverDouble = Server::where([
            ['ip', '=', request('ip')]
        ])->get();

        if ($serverDouble->isNotEmpty()) {
            return redirect()->route('servers.index');
        } 

        $file=$request->import->storeAs("keys/".request('name'), 'id_rsa');
        $Server = new \App\server();
        $Server->name = request('name');
        $Server->ip = request('ip');
        $Server->type = "HyperV";
        $Server->private = Storage::disk('local')->path('keys'). DIRECTORY_SEPARATOR . request('ip'). DIRECTORY_SEPARATOR . 'id_rsa';

        $Server->scripts = strtoupper(request('scripts_disk')).":".'\\'.request('scripts_one');
        $Server->templates = strtoupper(request('templates_disk')).":".'\\'.request('templates_one');
        $Server->clones = strtoupper(request('clones_disk')).":".'\\'.request('clones_one');
    
        $Server->save();
        return redirect()->route('servers.index');
    }    
}
