<?php

namespace App\Library\Helpers;

//Internal Library
use Log;
Use Carbon\Carbon;
use Seat\Eseye\Eseye;
use Seat\Eseye\Containers\EsiAuthentication;
use Seat\Eseye\Exceptions\RequestFailedException;
use Seat\Eseye\Configuration;
use Seat\Eseye\Cache\NullCache;

//App Library
use App\Jobs\Library\JobHelper;
use App\Library\Esi\Esi;


class ImplantsHelper {
    private $charId;
    private $scopeCheck;
    private $esiHelper = new Esi;
    private $esi = new Eseye;
    private $token = new EsiToken;
    private $config;

    /**
     * Class Constructor
     */
    public function __construct($char) {
        $this->$charId = $char;
        $this->esiHelper = new Esi;
        $this->config = config('esi');

        $hasScopeImplants = $esiHelper->HaveEsiScope($this->charId, 'esi-clones.read_implants.v1');
        $hasScopeLocation = $esiHelper->HaveEsiScope($this->charId, 'esi-location.read_location.v1');
        $hasScopeOnline = $esiHelper->HaveEsiScope($this->charId, 'esi-location.read_online.v1');
        $hasScopeShip = $esiHelper->HaveEsiScope($this->charId, 'esi-location.read_ship_type.v1');
        $hasScopeMail = $esiHelper->HaveEsiScope($this->charId, 'esi-mail.send_mail.v1');

        if($hasScopeImplants == false || $hasScopeLocation == false || $hasScopeOnline == false || $hasScopeShip == false || $hasScopeMail == false) {
            if($hasScopeMail == true) {
                //Send mail about needing scopes.
                $subject = 'Implant Services - Incorrect ESI Scope';
                $body = 'Please register with the following scopes.\r\n esi-clones.read_implants.v1\r\n esi-location.read_location.v1\r\n esi-location.read_online.v1\r\n esi-location.read_ship_type.v1\r\n esi-mail.send_mail.v1\r\n\r\n Sincerely,\r\n Implant Services';
                SendEveMail::dispatch($body, (int)$charId, 'character', $subject, (int)$charId);
            } else {
                //Send mail from primary config character about scopes.
                $subject = 'Implant Services - Incorrect ESI Scope';
                $body = 'Please register with the following scopes.\r\n esi-clones.read_implants.v1\r\n esi-location.read_location.v1\r\n esi-location.read_online.v1\r\n esi-location.read_ship_type.v1\r\n esi-mail.send_mail.v1\r\n\r\n Sincerely,\r\n Implant Services';
                SendEveMail::dispatch($body, (int)$charId, 'character', $subject, $config['primary']);
            }

            $this->token = $this->EsiHelper->GetRefreshToken($this->charId);
            $this->esi = $this->EsiHelper->SetupEsiAuthentication($this->token);
        }
    }

    public function ReadCharacterImplants() {
        try {
            $implants = $esi->invoke('get', '/characters/{character_id}/implants/', [
                'character_id' => $this->charId,
            ]);
        } catch (RequestFailedException $e) {
            Log::warning('Failed to get implants for character id: ' . $this->charId);
            return null;
        }

        return $implants;
    }

    public function ReadCharacterLocation() {
        try {
            $location = $esi->invoke('get', '/characters/{character_id}/location/', [
                'character_id' => $this->charId,
            ]);
        } catch (RequestFailedException $e) {
            Log::warning('Failed to get location for character id: ' . $this->charId);
            return null;
        }

        return $location;
    }

    public function ReadCharacterOnline() {
        try {
            $online = $esi->invoke('get', '/characters/{character_id}/online/', [
                'character_id' => $this->charId,
            ]);
        } catch (RequestFailedException $e) {
            Log::warning('Failed to get online status for character id: ' . $this->charId);
            return null;
        }

        return $online;
    }

    public function ReadShipType() {
        try {
            $ship = $esi->invoke('get', '/characters/{character_id}/ship/', [
                'character_id' => $this->charId,
            ]);
        } catch (RequestFailedException $e) {
            Log::warning('Failed to get ship for character id: ' . $this->charId);
            return null;
        }

        return $ship;
    }
}