<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Hava Durumu Verilerini Burada Çekiyoruz';

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



    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
dd("1");
      
      $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://betsapi.com/t/6219/Kocaelispor',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Cookie: sid=7pktq38vmb4ba1ptj3d98i17v3'
  ),
));

$response = curl_exec($curl);


curl_close($curl);

$sonucmaclar=explode('<h3 class="card-title">Results</h3>',$response);
$sonucmaclar=explode('<div class="card">',$sonucmaclar[1]);

$sonmac=explode('<tr>',$sonucmaclar[0]);
$sonmac=explode('</tr>',$sonmac[1]);


$hafta=explode('<td>',$sonmac[0]);
$hafta=explode('</td>',$hafta[1]);

$liglink=explode('<a href="',$sonmac[0]);
$liglink=explode('"',$liglink[1]);

$maclink=explode('<a href="',$sonmac[0]);
$maclink=explode('"',$maclink[3]);


$liglinkreplace=str_replace('l/', 'lt/', $liglink[0]);

$macsonuc=explode('<a href="'.$maclink[0].'">',$sonmac[0]);
$macsonuc=explode('</a>',$macsonuc[1]);

$macgit = curl_init();

curl_setopt_array($macgit, array(
  CURLOPT_URL => 'https://betsapi.com'.$maclink[0],
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
));


$responsemac = curl_exec($macgit);


$titleparcala=explode('<title>',$responsemac);
$titleparcala=explode(' -',$titleparcala[1]);

$teams=explode(' vs ',$titleparcala[0]);

$hometeam=$teams[0];
$awayteam=$teams[1];

$macsonucum=ltrim(rtrim($macsonuc[0]));
$macsonucumparcala=explode('-',$macsonucum);

$homescore=$macsonucumparcala[0];
$awayscore=$macsonucumparcala[1];

$result = array(
    'hometeam' => $hometeam,
    'awayteam' => $awayteam,
    'homescore' => $homescore,
    'awayscore' => $awayscore,
    'hafta' => trim($hafta[0])
);

// JSON olarak yazdır
$json=json_encode($result, JSON_PRETTY_PRINT);
$Setting=Setting::where('key','last_match')->update(['value'=>$json]);
echo $json.'<br /></br />';



$liggit = curl_init();

curl_setopt_array($liggit, array(
  CURLOPT_URL => 'https://betsapi.com'.$liglinkreplace,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Cookie: sid=7pktq38vmb4ba1ptj3d98i17v3'
  ),
));


$responselig = curl_exec($liggit);


$puandurumugenel=explode('<table class="table table-sm">',$responselig);
$puandurumugenel=explode('</table>',$puandurumugenel[1]);

$results = [];
for($a=2;$a<=7;$a++){
$puanrow=explode('<tr>',$puandurumugenel[0]);
$puanrow=explode('</tr>',$puanrow[$a]);

$teamsira=explode('<td class="text-center">',$puanrow[0]);
$teamsira=explode('</td>',$teamsira[1]);

$teamid=explode('<a href="/t/',$puanrow[0]);
$teamid=explode('/',$teamid[1]);

$team=explode('<a href="/t/'.$teamid[0].'/',$puanrow[0]);
$team=explode('"',$team[1]);

$teamwin=explode('<td>',$puanrow[0]);
$teamwin=explode('</td>',$teamwin[3]);

$teamberaber=explode('<td>',$puanrow[0]);
$teamberaber=explode('</td>',$teamberaber[4]);

$teamlose=explode('<td>',$puanrow[0]);
$teamlose=explode('</td>',$teamlose[5]);

$teampuan=explode('<td>',$puanrow[0]);
$teampuan=explode('</td>',$teampuan[7]);

$teamtotal=$teamwin[0]+$teamberaber[0]+$teamlose[0];


  $results[] = [
        'team' => $team[0],
        'teamsira' => $teamsira[0],
        'teamwin' => $teamwin[0],
        'teamlose' => $teamlose[0],
        'teamberaber' => $teamberaber[0],
        'teampuan' => $teampuan[0],
        'teamtotal' => $teamtotal
    ];
}

$standingsjson=json_encode($results, JSON_PRETTY_PRINT);
$Setting=Setting::where('key','last_standings')->update(['value'=>$standingsjson]);

echo $standingsjson;
        return Command::SUCCESS;
    }

}
