<?php

namespace App\Library\Helpers;

//Internal Library
use Log;
use Carbon\Carbon;

//App Library
use App\Jobs\Library\JobHelper;
use App\Library\Esi\Esi;
use Seat\Eseye\Exceptions\RequestFailedException;

//Models
use App\Models\Jobs\JobStatus;
use App\Models\Structure\Asset;

class AssetHelper {

    private $charId;
    private $corpId;

    public function __construct($char, $corp, $type) {
        $this->charId = $char;
        $this->corpId = $corp;
        $this->type = $type;
    }

    /**
     * Process Assets
     * 
     * @param $assets
     */
    public function ProcessCharacterAssets($assets) {
        foreach($assets as $asset) {
            Asset::updateOrCreate([
                'item_id' => $asset->item_id,
            ], [
                'character_id' => $this->charId,
                'is_blueprint_copy' => $asset->is_blueprint_copy,
                'is_singleton' => $asset->is_singleton,
                'item_id' => $asset->item_id,
                'location_flag' => $asset->location_flag,
                'location_id' => $asset->location_id,
                'location_type' => $asset->location_type,
                'quantity' => $asset->quantity,
                'type_id' => $asset->type_id,
            ]);
        }
    }

    /**
     * Process Corporation Assets
     * 
     * @param $corporationId
     * @param $assets
     */
    public function ProcessCorporationAsset($assets) {
        foreach($assets as $asset) {
            Asset::updateOrCreate([
                'item_id' => $asset->item_id,
            ],[
                'corporation_id' => $this->corpId,
                'is_blueprint_copy' => $asset->is_blueprint_copy,
                'is_singleton' => $asset->is_singleton,
                'item_id' => $asset->item_id,
                'location_flag' => $asset->location_flag,
                'location_id' => $asset->location_id,
                'location_type' => $asset->location_type,
                'quantity' => $asset->quantity,
                'type_id' => $asset->type_id,
            ]);
        }
    }

    /**
     * Get Character Assets by page in order to store in the database
     * 
     * @param $page
     */
    public function GetCharacterAssetsByPage($page) {
        $esiHelper = new Esi;

        $config = config('esi');

        $hasScope = $esiHelper->HaveEsiScope($this->charId, 'esi-assets.read_character_assets.v1');

        if($hasScope == false) {
            Log::warning('ESI Scope check for esi-assets.read_character_assets.v1 failed.');
            if($config['eveMailEnabled'] === true) {
                //Send a mail about the incorrect scope to the character
                $subject = 'Alliance Services - Incorrect ESI Scope';
                $body = 'Please register with the scope: ' . $scope;
                SendEveMail::dispatch($body, (int)$charId, 'character', $subject, $config['primary'])->delay(Carbon::now()->addSeconds(5));
            }
        } 

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

        try {
            $assets = $esi->page($this->page)
                          ->invoke('get', '/characters/{character_id}/assets/', [
                            'character_id' => $charId,
                          ]);
        } catch (RequestFailedException $e) {
            Log::warning('Failed to get page of character assets from ESI.');
            $assets = null;
        }

        return $assets;
    }

    /**
     * Get Corporation Assets By Page in order to store in the database
     */
    public function GetCorporationAssetsByPage($page) {
        //Declare the variable for the esi helper
        $esiHelper = new Esi;

        //Setup the esi authentication container
        $config = config('esi');

        //Check for the scope needed
        $hasScope = $esiHelper->HaveEsiScope($this->charId, 'esi-assets.read_corporation_assets.v1');
        if($hasScope == false) {
            Log::critical('ESI Scope check has failed for esi-assets.read_corporation_assets.v1 for character id: ' . $this->charId);
            return null;
        }
        
        //Get the refresh token from the database
        $token = $esiHelper->GetRefreshToken($this->charId);
        //Setup the esi authentication container
        $esi = $esiHelper->SetupEsiAuthentication($token);
        
        try {
            $assets = $esi->page($this->page)
                          ->invoke('get', '/corporations/{corporation_id}/assets/', [
                              'corporation_id' => $this->corpId,
                          ]);
        } catch(RequestFailedException $e) {
            Log::critical("Failed to get page of assets from ESI.");
            $assets = null;
        }

        return $assets;
    }
}

?>