<?php

namespace App\Modules\Mattermost\Controllers\Api\V1;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Status;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Modules\Bookings\Task;
use App\Models\User;
use App\Modules\Clients\Client;
use App\Modules\Clients\Customer;
use App\Modules\Clients\Site;
use App\Modules\Mattermost\MattermostHelper;
use App\Http\Controllers\BaseController;

class MattermostIcingaControllerREST  extends BaseController {

	public function __construct() {
		//Load helper functions
		$this->MattermostHelper = new MattermostHelper();
	}

	public function showIcinga(Request $request) {
		$text = "";

		$hosts = $this->MattermostHelper->setupIcingaHosts()->hosts;
		$services = $this->MattermostHelper->setupIcingaHosts()->services;

		if (!$request->has('text')) {
			if (!Cache::has('icingaNotAckText')) {

				$text = $this->MattermostHelper->makeIcingaNotAckTable($hosts, $services);
				$expiresAt = Carbon::now()->addMinutes(2);
				Cache::put('icingaNotAckText', $text, $expiresAt);
			}
			else {
				$text = Cache::get('icingaNotAckText');
			}
		}
		else if ($this->MattermostHelper->containsWord($request->input('text'), 'all') && !Cache::has('icingaAllText')) {

			$text = $this->MattermostHelper->makeIcingaAllTable($hosts, $services);

			$text = $text . "---";
			$expiresAt = Carbon::now()->addMinutes(2);
			Cache::put('icingaAllText', $text, $expiresAt);
		}
		else {
			$text = Cache::get('icingaAllText');
		}
		return $this->MattermostHelper->sendResponse($text);
	}
}
