<?php

namespace App\Http\Controllers;

use App\Paper\CinemaMultikino;
use DiDom\Query;
use Illuminate\Http\Request;
use DiDom\Document;
use App\Paper\Paper;

class PacktPub extends Controller
{

    private $main;
    private $icon = '/small/book.png';

    private $freeBook = 'https://www.packtpub.com/packt/offers/free-learning';


    private $loginData;

    public function __construct()
    {
        $this->paper = new Paper();
        $this->loginData = array(
            'email' => 'krzysiej@gmail.com',
            'password' => 'korki1korki1',
            'op' => 'Login',
            'form_build_id' => '',
            'form_id' => 'packt_user_login_form',
        );

    }


    public function today()
    {

        $data = file_get_contents($this->freeBook);
        $document = new Document($data);

//        list(,,$id,)= explode('/', $document->first('//form[@id="free-learning-form"]/@action', Query::TYPE_XPATH));
        $id = $document->first('//input[@id="free-learning-register-claim-title-nid"]/@value', Query::TYPE_XPATH);

        $myBooksRaw = $this->c('https://www.packtpub.com/account/my-ebooks', $this->loginData);

        print_r($myBooksRaw);
        $myBooks = new Document($myBooksRaw);

//        $x = $myBooks->first('//div[@nid="' . $id . '"]', Query::TYPE_XPATH);

        var_dump($id);

        var_dump('//div[@nid="' . $id . '"]');
        $isOwnd = (bool)$myBooks->first('//div[@nid="' . $id . '"]', Query::TYPE_XPATH);

        var_dump($isOwnd);
        die();
        $titleNode = $document->first('.dotd-title h2');

        if ($titleNode) {
            $title = trim($titleNode->text());
        }

        $descriptionNodes = $document->find('//div[contains(@class,"dotd-main-book-summary")]/div[not(@class)]', Query::TYPE_XPATH);
        $descriptionText = '';
        if ($descriptionNodes) {
            foreach ($descriptionNodes as $node) {
                $descriptionText .= trim($node->text()) . "\n";
            }
        }

        $descriptionText .= "\n\n" . (($isOwnd) ? 'Masz już tę książkę w biblioteczce.' : 'Nie masz jeszcze tej książki.');
        $descriptionText .= "\n\n" . date('Y-m-d');


        $imageNode = $document->first('//div[contains(@class, "dotd-main-book")]//img[contains(@class, "bookimage")]/@src', Query::TYPE_XPATH);
        if ($imageNode) {
            $imageNode = str_replace(' ', '%20', $imageNode);

            $this->paper->sendPrint($title, $descriptionText, 'https:' . $imageNode, false);
        }
    }

    private function c($url, $postArray = null)
    {
        $cookie = __DIR__ . '/cookie.txt';
        $ch = curl_init();
        $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
        curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!is_null($postArray)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArray));
        }
        $result = curl_exec($ch);

        curl_close($ch);
        return $result;
    }


}
