<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        $query = "гарри поттер";
        $query = urlencode($query);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "referer: https://knigavuhe.org/",
            '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',
        ]);
        curl_setopt($ch, CURLOPT_URL, "https://knigavuhe.org/search/?q={$query}&page=1");
        $result = curl_exec($ch);

        echo $result;
        curl_close($ch);

        exit();



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

        $count_pages = $this->getElementValue($result,'<div class="pn_page_buttons">','</div>');

        if(!empty($count_pages)){
            $pages = explode("a class",$count_pages);
            $count_pages = trim($this->getElementValue($pages[count($pages)-1],'false">','</a>'));
        }else{
            $count_pages = 0;
        }

        echo $count_pages;
        $books_arr = [];

        foreach ($items as $item) {
            if(strpos($item,'bookkitem_litres_icon')===false){


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


        if($count_pages>1){
            for($i=2;$i<=$count_pages;$i++){
                if($i>=6){
                    break;
                }

                $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 = $this->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($this->getElementValue($item,'class="bookkitem_name">','</a>'))),
                            "author" => trim(strip_tags($this->getElementValue($item,'<span class="bookkitem_author_label">автор</span>','</span>'))),
                            "genre" => trim(strip_tags($this->getElementValue($item,'<div class="bookkitem_genre">','</div>'))),
                            "duration" => trim(strip_tags($this->getElementValue($item,'<span class="bookkitem_meta_time">','</span>'))),
                            "likes" => trim(strip_tags($this->getElementValue($item,'<span class="bookkitem_icon -likes"></span> <span class="bookkitem_meta_label -not_last">','</span>'))),
                            "link" => "https://knigavuhe.org".trim(strip_tags($this->getElementValue($item,'<a href="','"'))),
                        ];
                    }
                }
            }
        }


        echo json_encode([
            'books' => $books_arr,
            'count_pages'  =>$count_pages
        ],JSON_UNESCAPED_UNICODE);



    }




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