<?php



namespace App\Http\Controllers;



use App\Models\Authors;

use App\Models\Exchange;

use Illuminate\Http\Request;

use App\Utilities\FlashNotification;


class BotController extends Controller

{

    public function getstockexchange(){
        

    function replace_tr($text) {
    $text = trim($text);
    $search = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
    $replace = array('c','c','g','g','i','i','o','o','s','s','u','u',' ');
    $new_text = str_replace($search,$replace,$text);
    return $new_text;
    } 

    $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://www.doviz.com/kripto-paralar');

     $genelveri=explode('<div class="value-table">',$site);
     $genelveri=explode('<div class="pagination">',$genelveri[3]);


     for($z=2;$z<=10;$z++){
         $tek=explode('<tr>',$genelveri[0]);
         $tek=explode('</tr>',$tek[$z]);

         $shortname=explode('<div>',$tek[0]);
         $shortname=explode('</div>',$shortname[1]);

         $name=explode('<div class="cname">',$tek[0]);
         $name=explode('</div>',$name[1]);

         $value=explode('<td class="text-bold">',$tek[0]);
         $value=explode('</td>',$value[1]);

         $status=explode('<td class="text-bold color-',$tek[0]);
         $status=explode('"',$status[1]);

         $changevalue=explode('<td class="text-bold color-'.$status[0].'">',$tek[0]);
         $changevalue=explode('</td>',$changevalue[1]);

         $changevalue[0]=str_replace(' ','',$changevalue[0]);
         $changevalue[0]= preg_replace('/\n/', '', $changevalue[0]);
         $value[0]= preg_replace('/\n/', '', $value[0]);
            if($status[0]=="up"){
               $DBstatus="1";
            }else{
                $DBstatus="0";
            }

          $setExchange = Exchange::updateOrCreate(
                                ['long_name' => $name[0]], // Koşul (bulunacak veya güncellenecek kayıt)
                                [
                                    'value' => $value[0],
                                    'status' => $DBstatus,
                                    'type' => '10',
                                    'short_name' => $shortname[0],
                                    'last' => $changevalue[0]
                                ] // Güncellenecek veya oluşturulacak değerler
                            );    

      }
  
    }

    public function getstockexchangePopular(){

    function replace_tr($text) {
    $text = trim($text);
    $search = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
    $replace = array('c','c','g','g','i','i','o','o','s','s','u','u',' ');
    $new_text = str_replace($search,$replace,$text);
    return $new_text;
    } 

    $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;
        }

    }

}

