<?php

use Illuminate\Database\Seeder;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Models\Config;
use App\Models\Moon;
use App\Models\Price;
use App\Models\ItemComposition;

class OrePricesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->FetchNewPrices();
        $this->UpdateItemPricing();
    }

    private function UpdateItemPricing() {
        //Get the configuration from the config table
        $config = DB::table('Config')->first();
        //Calculate refine rate
        $refineRate = $config->RefineRate / 100.00;
        //Calculate the current time
        $time = time();
        //Get the max time from the database
        $maxTime = DB::table('Prices')->where('ItemId', 34)->max('Time');
        //Get the price of the basic minerals
        $tritaniumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [34, $maxTime]);
        $tritanium = DB::select( DB::raw('SELECT Price FROM Prices WHERE ItemId= :id AND Time= :time'), array('id' => 34, 'time' => $maxTime));
        $pyeritePrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [35, $maxTime]);
        $mexallonPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [36, $maxTime]);
        $isogenPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [37, $maxTime]);
        $nocxiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [38, $maxTime]);
        $zydrinePrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [39, $maxTime]);
        $megacytePrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [40, $maxTime]);
        $morphitePrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [11399, $maxTime]);
        $heliumIsotopesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16274, $maxTime]);
        $nitrogenIsotopesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [17888, $maxTime]);
        $oxygenIsotopesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [17887, $maxTime]);
        $hydrogenIsotopesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [17889, $maxTime]);
        $liquidOzonePrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16273, $maxTime]);
        $heavyWaterPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16272, $maxTime]);
        $strontiumClathratesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16275, $maxTime]);
        //Get the price of the moongoo
        $atmosphericGasesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16634, $maxTime]);
        $evaporiteDepositsPirce = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16635, $maxTime]);
        $hydrocarbonsPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16633, $maxTime]);
        $silicatesPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16636, $maxTime]);
        $cobaltPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16640, $maxTime]);
        $scandiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16639, $maxTime]);
        $titaniumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16638, $maxTime]);
        $tungstenPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16637, $maxTime]);
        $cadmiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16643, $maxTime]);
        $platinumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16644, $maxTime]);
        $vanadiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16642, $maxTime]);
        $chromiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16641, $maxTime]);
        $technetiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16649, $maxTime]);
        $hafniumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16648, $maxTime]);
        $caesiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16647, $maxTime]);
        $mercuryPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16646, $maxTime]);
        $dysprosiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16650, $maxTime]);
        $neodymiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16651, $maxTime]);
        $promethiumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16652, $maxTime]);
        $thuliumPrice = DB::select('SELECT Price FROM Prices WHERE ItemId = ? AND Time = ?', [16653, $maxTime]);
        //Get the item compositions
        $items = DB::select('SELECT Name,ItemId FROM ItemComposition');
        //Go through each of the items and update the price
        foreach($items as $item) {
            //Get the item composition
            $composition = DB::select('SELECT * FROM ItemComposition WHERE ItemId = ?', [$item->ItemId]);
            //Calculate the Batch Price
            $batchPrice = ( ($composition[0]->Tritanium * $tritaniumPrice[0]->Price) +
                            ($composition[0]->Pyerite * $pyeritePrice[0]->Price) +
                            ($composition[0]->Mexallon * $mexallonPrice[0]->Price) +
                            ($composition[0]->Isogen * $isogenPrice[0]->Price) +
                            ($composition[0]->Nocxium * $nocxiumPrice[0]->Price) +
                            ($composition[0]->Zydrine * $zydrinePrice[0]->Price) +
                            ($composition[0]->Megacyte * $megacytePrice[0]->Price) + 
                            ($composition[0]->Morphite * $morphitePrice[0]->Price) +
                            ($composition[0]->HeavyWater * $heavyWaterPrice[0]->Price) +
                            ($composition[0]->LiquidOzone * $liquidOzonePrice[0]->Price) +
                            ($composition[0]->NitrogenIsotopes * $nitrogenIsotopesPrice[0]->Price) +
                            ($composition[0]->HeliumIsotopes * $heliumIsotopesPrice[0]->Price) + 
                            ($composition[0]->HydrogenIsotopes * $hydrogenIsotopesPrice[0]->Price) +
                            ($composition[0]->OxygenIsotopes * $oxygenIsotopesPrice[0]->Price) +
                            ($composition[0]->StrontiumClathrates * $strontiumClathratesPrice[0]->Price) +
                            ($composition[0]->AtmosphericGases * $atmosphericGasesPrice[0]->Price) +
                            ($composition[0]->EvaporiteDeposits * $evaporiteDepositsPirce[0]->Price) +
                            ($composition[0]->Hydrocarbons * $hydrocarbonsPrice[0]->Price) +
                            ($composition[0]->Silicates * $silicatesPrice[0]->Price) +
                            ($composition[0]->Cobalt * $cobaltPrice[0]->Price) +
                            ($composition[0]->Scandium * $scandiumPrice[0]->Price) +
                            ($composition[0]->Titanium * $titaniumPrice[0]->Price) +
                            ($composition[0]->Tungsten * $tungstenPrice[0]->Price) +
                            ($composition[0]->Cadmium * $cadmiumPrice[0]->Price) +
                            ($composition[0]->Platinum * $platinumPrice[0]->Price) +
                            ($composition[0]->Vanadium * $vanadiumPrice[0]->Price) +
                            ($composition[0]->Chromium * $chromiumPrice[0]->Price)+
                            ($composition[0]->Technetium * $technetiumPrice[0]->Price) +
                            ($composition[0]->Hafnium * $hafniumPrice[0]->Price) +
                            ($composition[0]->Caesium * $caesiumPrice[0]->Price) +
                            ($composition[0]->Mercury * $mercuryPrice[0]->Price) +
                            ($composition[0]->Dysprosium * $dysprosiumPrice[0]->Price) +
                            ($composition[0]->Neodymium * $neodymiumPrice[0]->Price) + 
                            ($composition[0]->Promethium * $promethiumPrice[0]->Price) +
                            ($composition[0]->Thulium * $thuliumPrice[0]->Price));
            //Calculate the batch price with the refine rate included
            //Batch Price is base price for everything
            $batchPrice = $batchPrice * $refineRate;
            //Calculate the unit price
            $price = $batchPrice / $composition[0]->BatchSize;
            //Calculate the m3 price
            $m3Price = $price / $composition[0]->m3Size;  
            //Insert the prices into the Prices table
            DB::table('OrePrices')->insert([
                'Name' => $composition[0]->Name,
                'ItemId' => $composition[0]->ItemId,
                'BatchPrice' => $batchPrice,
                'UnitPrice' => $price,
                'm3Price' => $m3Price,
                'Time' => $time
            ]);
        }
    }

    private function FetchNewPrices() {
        $ItemIDs = array(
            "Tritanium" => 34,
            "Pyerite" => 35,
            "Mexallon" => 36,
            "Isogen" => 37,
            "Nocxium" => 38,
            "Zydrine" => 39,
            "Megacyte" => 40,
            "Morphite" => 11399,
            "HeliumIsotopes" => 16274,
            "NitrogenIsotopes" => 17888,
            "OxygenIsotopes" => 17887,
            "HydrogenIsotopes" => 17889,
            "LiquidOzone" => 16273,
            "HeavyWater" => 16272,
            "StrontiumClathrates" => 16275,
            "AtmosphericGases" => 16634,
            "EvaporiteDeposits" => 16635,
            "Hydrocarbons" => 16633,
            "Silicates" => 16636,
            "Cobalt" => 16640,
            "Scandium" => 16639,
            "Titanium" => 16638,
            "Tungsten" => 16637,
            "Cadmium" => 16643,
            "Platinum" => 16644,
            "Vanadium" => 16642,
            "Chromium" => 16641,
            "Technetium" => 16649,
            "Hafnium" => 16648,
            "Caesium" => 16647,
            "Mercury" => 16646,
            "Dysprosium" => 16650,
            "Neodymium" => 16651,
            "Promethium" => 16652,
            "Thulium" => 16653,
        );
        $time = time();
        $item = array();
        //Get the json data for each ItemId from https://market.fuzzwork.co.uk/api/
        //Base url is https://market.fuzzwork.co.uk/aggregates/?region=10000002&types=34
        //Going to use curl for these requests
        foreach($ItemIDs as $key => $value) {
            $client = new Client(['base_uri' => 'https://market.fuzzwork.co.uk/aggregates/']);
            $uri = '?region=10000002&types=' . $value;
            $result = $client->request('GET', $uri);
            $item = json_decode($result->getBody(), true);
            
            DB::table('Prices')->insert([
                'Name' => $key,
                'ItemId' => $value,
                'Price' => $item[$value]['sell']['median'],
                'Time' => $time
            ]);
        }
    }
}
