<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * 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);
        //$query = str_replace(" ","+",$query);
        $result_arr = [];
        for($i=1;$i<5;$i++){
            $ch = curl_init ();
            curl_setopt($ch, CURLOPT_URL, "http://fbsearch.ru/search.php?q={$query}&page={$i}");
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            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',
            ]);
            $result = curl_exec($ch);
            $books = $this->getElementValue($result,'Найдено книг:','<ul class="pagination pagination-lg">');
            $books_arr = explode('<h3 class="title',$books);
            foreach ($books_arr as $item) {
                if(strpos($item,'flibusta') !==false){
                    $item = $this->getElementValue($item,'">','<span class="header-misc">');
                    if(mb_strpos($item,'flibusta') !==false and !empty($this->getElementValue($item,'href="','"'))){
                        $id = urldecode($this->getElementValue($item,'href="','"'));
                        $id = explode('/b/',$id)[1];
                        $name =  trim(strip_tags($this->getElementValue($item,'">','</a>')));
                        $actor =  $this->getElementValue($item,'<a class="title"','/a>');
                        $actor = $this->getElementValue($actor,'>','<');
                        $result_arr[] = [
                            'id' => $id,
                            'name' => $name,
                            'actor' => $actor
                        ];
                        echo json_encode([
                                'id' => $id,
                                'name' => $name,
                                'actor' => $actor
                        ],JSON_UNESCAPED_UNICODE)."\n";
                    }
                }

            }
        }
        //echo json_encode($result_arr, 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;
    }

}
