<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Html2Text\Html2Text;
use Illuminate\Support\Str;

class ParsingController extends Controller
{
    private $domain;
    private $urls = array();
    private $index = 0;
    private $length=0;
    private $siteText ='';

    public function index(Request $request)
    {
        $html = file_get_contents($request->link);
        array_push($this->urls, rtrim($request->link, '/'));
        //Set domain name for links filter
        $this->domain = parse_url($request->link)['host'];

        $this->extractAllLinks();

        // file name that will be used in the download
        $fileName = str_replace('.', '_', $this->domain).'.txt';

        $content = 'Words: '.$this->length.'    '.$this->siteText;

        // use headers in order to generate the download
        $headers = [
            'Content-type' => 'text/plain',
            'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName),
            'Content-Length' => strlen($content)
        ];


        return response($content)
            ->withHeaders($headers);

        //return view('welcome', ['len' => $this->length]);
    }

    public function extractAllLinks()
    {
        //Set url for parsing
        $link = $this->urls[$this->index];

        @$html = file_get_contents($link);
        if ($html) {
            //Create a new DOM document
            $dom = new \DOMDocument();

            @$dom->loadHTML($html);

            $text = new Html2Text($html);
            $text = $text->getText();
            $this->siteText.=$text;
            $this->length += str_word_count($text);

            $links = $dom->getElementsByTagName('a');

            foreach ($links as $link) {
                $link = rtrim($link->getAttribute('href'), '/');

                //Check if url contains domain name
                if (Str::contains($link, $this->domain)) {
                    //Check for existing link in array
                    if (!in_array($link, $this->urls)) {
                        array_push($this->urls, $link);
                    }
                }
            }
        }
        $this->index++;
        if (($this->index + 1) < count($this->urls)) {
            $this->extractAllLinks();
        }
    }
}
