<?php

namespace App\Console\Commands;

use App\Models\Category;
use App\Models\Exchange;
use App\Models\News;
use App\Models\AgencyBotCategory;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use App\Models\Setting;

class GetStockExchange extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:getStockExchange';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Ekonomi Verilerini burada Çekiyoruz';

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



    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

    $site = file_get_contents('https://borsa.doviz.com/');

    preg_match_all('@class="currency-short">(.*?)</a>@si', $site, $name);
    preg_match_all('@<span class="value">(.*?)</span>@si', $site, $value);
    preg_match_all('@<span class="change (.*?)">(.*?)</span>@si', $site, $status);

    $nameupdate     =   $name[1];
    $valueupdate    =   $value[1];
    $changevalueupdate    =   $status[2];
    $statusupdate    =   $status[1];


        $i=0;

        $deleteExchange=Exchange::where('type',2)->delete();
        $deleteExchange=Exchange::where('type',3)->delete();
        $deleteExchange=Exchange::where('type',4)->delete();

        foreach ($nameupdate as $nameup => $key) {

                    $changevalueupdate[$i]=str_replace(' ','',$changevalueupdate[$i]);
                    $changevalueupdate[$i]= preg_replace('/\n/', '', $changevalueupdate[$i]);
                    $valueupdate[$i]= preg_replace('/\n/', '', $valueupdate[$i]);
                    $vkey= preg_replace('/\n/', '', $key);
                        $DBstatus="0";
                        if($statusupdate[$i]=="up"){
                            $DBstatus="1";
                        }
                        if($i<4){
                            $type=2;
                        }
                        elseif($i<=9 and $i>=5){
                            $type=3;
                        }
                        elseif($i<=14 and $i>=10){
                            $type=4;
                        }
                       $setExchange = Exchange::updateOrCreate(
                            ['long_name' => $key], // Koşul (bulunacak veya güncellenecek kayıt)
                            [
                                'value' => $valueupdate[$i],
                                'status' => $DBstatus,
                                'type' => $type,
                                'short_name' => $key,
                                'last' => $changevalueupdate[$i]
                            ] // Güncellenecek veya oluşturulacak değerler
                        );
        $i=$i+1;
        }


        
    

    $site = file_get_contents('https://kur.doviz.com/serbest-piyasa/amerikan-dolari');

    preg_match_all('@<span class="name">(.*?)</span>@si', $site, $name);
    preg_match_all('@ data-socket-attr="s" data-socket-animate="true">(.*?)</span>@si', $site, $value);
    preg_match_all('@<div class="change-rate status (.*?)"@si', $site, $status);
    preg_match_all('@ data-socket-attr="c">(.*?)<@si', $site, $changevalue);
 
    $nameupdate     =   $name[1];
    $valueupdate    =   $value[1];
    $statusupdate    =   $status[1];
    $changevalueupdate    =   $changevalue[1];


        $i=0;
        foreach ($nameupdate as $nameup => $key) {
                    $changevalueupdate[$i]=str_replace(' ','',$changevalueupdate[$i]);
                    $changevalueupdate[$i]= preg_replace('/\n/', '', $changevalueupdate[$i]);
                    $valueupdate[$i]= preg_replace('/\n/', '', $valueupdate[$i]);
                    $vkey= preg_replace('/\n/', '', $key);
                    $getExchange=Exchange::where('long_name',$key)->first();
                    if(isset($getExchange)){
                        $DBstatus="0";
                        if($statusupdate[$i]=="up"){
                            $DBstatus="1";
                        }

                        $setExchange = Exchange::where('long_name', $key)->update(['value' => $valueupdate[$i],'status'=>$DBstatus,'last'=>$changevalueupdate[$i]]);
            }
        $i=$i+1;
        }

  
            $site = file_get_contents('https://kur.doviz.com/serbest-piyasa/amerikan-dolari');

            preg_match_all('@<span class="name">(.*?)</span>@si', $site, $name);
            preg_match_all('@ data-socket-attr="s" data-socket-animate="true">(.*?)</span>@si', $site, $value);
            preg_match_all('@<div class="change-rate status (.*?)"@si', $site, $status);
            preg_match_all('@ data-socket-attr="c">(.*?)<@si', $site, $changevalue);
         
            $nameupdate     =   $name[1];
            $valueupdate    =   $value[1];
            $statusupdate    =   $status[1];
            $changevalueupdate    =   $changevalue[1];


                $i=0;
                foreach ($nameupdate as $nameup => $key) {
                            $changevalueupdate[$i]=str_replace(' ','',$changevalueupdate[$i]);
                            $changevalueupdate[$i]= preg_replace('/\n/', '', $changevalueupdate[$i]);
                            $valueupdate[$i]= preg_replace('/\n/', '', $valueupdate[$i]);
                            $vkey= preg_replace('/\n/', '', $key);
                            $getExchange=Exchange::where('long_name',$key)->first();
                            if(isset($getExchange)){
                                $DBstatus="0";
                                if($statusupdate[$i]=="up"){
                                    $DBstatus="1";
                                }

                                $setExchange = Exchange::where('long_name', $key)->update(['value' => $valueupdate[$i],'status'=>$DBstatus,'last'=>$changevalueupdate[$i]]);
                    }
                $i=$i+1;
                }


        return Command::SUCCESS;
    }
}
