<?php

namespace App\Console\Commands\Market;

use App\Events\MarketStatsLiteUpdated;
use App\Models\Market\Market;
use App\Services\Liquidity\Binance\BinanceApi;
use App\Services\Liquidity\Mexc\MexcApi;
use App\Services\Market\MarketService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class MarketMexcStatsWatcherCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'market-watcher:stats-mexc';

    protected $markets;

    protected $initialMarkets;

    protected $lastMarkets;

    protected $quoteMarkets;

    protected $discounts;

    protected $baseMarkets;

    protected $lastRefreshed;

    protected $iteration = 0;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->prepareMarkets();

        $binanceApi = new MexcApi();

        $marketService = new MarketService();

        //try {

        $binanceApi->ticker(false, function ($data, $symbol, $ticker) use ($marketService) {

            $marketName = $this->markets[$ticker['symbol']] ?? false;

            if ($marketName) {

                $discount = $this->discounts[$this->markets[$ticker['symbol']]];

                $ratio = $discount * 0.01;

                $close = $ticker['close'] + ($ticker['close'] * $ratio);
                $high = $ticker['high'] + ($ticker['high'] * $ratio);
                $low = $ticker['low'] + ($ticker['low'] * $ratio);

                $marketService->updateStatsForce($this->markets[$ticker['symbol']], 'high', $high);
                $marketService->updateStatsForce($this->markets[$ticker['symbol']], 'low', $low);
                $marketService->updateStatsForce($this->markets[$ticker['symbol']], 'volume', $ticker['volume']);
                $marketService->updateStatsForce($this->markets[$ticker['symbol']], 'last', $close);
                $marketService->updateStatsForce($this->markets[$ticker['symbol']], 'qVolume', $ticker['qVolume']);

                event(new MarketStatsLiteUpdated([
                    'name' => $this->initialMarkets[$this->markets[$ticker['symbol']]],
                    'last' => math_formatter($close, $this->quoteMarkets[$this->markets[$ticker['symbol']]]),
                    'high' => math_formatter($high, $this->quoteMarkets[$this->markets[$ticker['symbol']]]),
                    'low' => math_formatter($low, $this->quoteMarkets[$this->markets[$ticker['symbol']]]),
                    'volume' => math_formatter($ticker['volume'], $this->baseMarkets[$this->markets[$ticker['symbol']]]),
                    'qVolume' => math_formatter($ticker['qVolume'], $this->baseMarkets[$this->markets[$ticker['symbol']]]),
                    'change' => math_percentage_between($close, $this->lastMarkets[$this->markets[$ticker['symbol']]]),
                ]));

                if ((time() - $this->lastRefreshed) > 60) {
                    $this->prepareMarkets();
                }
            }
        });
    }

    protected function prepareMarkets() {

        $marketList = Market::where('liq', true)->pluck('name');

        $this->markets = Market::whereIn('name', $marketList)->pluck('id', 'name')->toArray();
        $this->discounts = Market::whereIn('name', $marketList)->pluck('discount', 'id')->toArray();
        $this->lastMarkets = Market::whereIn('name', $marketList)->pluck('last', 'id')->toArray();
        $this->quoteMarkets = Market::whereIn('name', $marketList)->pluck('quote_precision', 'id')->toArray();
        $this->baseMarkets = Market::whereIn('name', $marketList)->pluck('base_precision', 'id')->toArray();

        foreach($this->markets as $market=>$id) {
            unset($this->markets[$market]);
            $this->markets[market_sanitize($market)] = $id;
            $this->initialMarkets[$id] = $market;
        }

        $this->lastRefreshed = time();

    }
}

