<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BotController;
use App\Http\Controllers\AudioBooksController;
use App\Http\Controllers\AdminController;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/play/{id}', [AudioBooksController::class,'index']);
Route::get('/playg/{id}', [AudioBooksController::class,'playg']);

Route::get('/get_chart',[AdminController::class,'get_chart']);
Route::get('/ar_chart',[AdminController::class,'ar_chart']);
Route::get('/get_stats',[AdminController::class,'get_stats']);

Route::post('/callback', function (){
    $data = file_get_contents('php://input');
    $data = str_replace("'","",$data);
    exec("php /var/www/html/booksbot/artisan callback:run '{$data}' > /dev/null 2>&1 &");
    exit();
});

Route::get('/convert',function(){
    exec("ebook-convert '/home/torrent/books/fb2/110119.fb2' '/home/book.pdf'");
    echo 1;
});



Route::post('/api_books_callback',function (Request $request){
    $data = $request->data;
    $callback = json_decode($data,true);

    $api = new Api($callback['data']['chat_id'],"1342416920:AAFUOJ5MkAC_31PdEpKUB0qYI34rfGFvbAY");

    $api->execute("sendDocument",[
        "document" => $callback['book'][$callback['data']['type']],
        'caption' => '[@reader_robot](t.me/reader_robot?start=share)'
    ]);
});




Route::get('/search',function (){
    $query = urlencode("Гарри поттер");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_PROXY, "185.147.128.48:8000");
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "NYSBYF:bxfHvM");
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
        "referer: https://knigavuhe.org/",
        'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,nl;q=0.6,pl;q=0.5',
        'authority: knigavuhe.org',
    ]);
    curl_setopt($ch, CURLOPT_URL, "https://knigavuhe.org/search/?q={$query}&page=1");
    $result = curl_exec($ch);
    curl_close($ch);


    $books_arr = [];
    $books = getElementValue($result,'<div id="books_list">','<div class="pn" id="books_list__pn">');
    $items = array_slice(explode('<div class="bookkitem">',$result),1);

    foreach ($items as $item) {
        if(strpos($item,'bookkitem_litres_icon')===false){
            $books_arr[] = [
                "title" => trim(strip_tags(getElementValue($item,'class="bookkitem_name">','</a>'))),
                "author" => trim(strip_tags(getElementValue($item,'<span class="bookkitem_author_label">автор</span>','</span>'))),
                "genre" => trim(strip_tags(getElementValue($item,'<div class="bookkitem_genre">','</div>'))),
                "duration" => trim(strip_tags(getElementValue($item,'<span class="bookkitem_meta_time">','</span>'))),
                "likes" => trim(strip_tags(getElementValue($item,'<span class="bookkitem_icon -likes"></span> <span class="bookkitem_meta_label -not_last">','</span>'))),
                "link" => "https://knigavuhe.org".trim(strip_tags(getElementValue($item,'<a href="','"'))),
            ];
        }
    }
    return $books_arr;
});

 function getElementValue($string,$strStart,$strEnd){
    $str_search = strpos($string,$strStart);
    if($str_search===false){
        return false;
    }
    $str_1 = substr($string,$str_search+strlen($strStart));
    $str_search = strpos($str_1,$strEnd);
    $res = substr($str_1,0,$str_search);
    return $res;
}
