<?php

namespace Tests\Browser;

use Dompdf\Exception;
use Facebook\WebDriver\Exception\NoSuchDocumentException;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\WebDriver;
use Facebook\WebDriver\WebDriverBy;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class HotelTest extends DuskTestCase {
	/**
	 * A Dusk test example.
	 *
	 * @return void
	 */
	public function testExample() {
	    $links = [
	        'https://mytour.vn/c65/khach-san-tai-da-nang.html?p=13,14',
            'https://mytour.vn/c5/khach-san-tai-ba-ria-vung-tau.html?p=13,14',
            'https://mytour.vn/d325/khach-san-tai-nha-trang.html?p=13,14',
            'https://mytour.vn/d309/khach-san-tai-phu-quoc.html?p=13,14',
            'https://mytour.vn/d346/khach-san-tai-sapa.html?p=13,14',
            'https://mytour.vn/d431/khach-san-tai-ha-long.html?p=13,14',
            'https://mytour.vn/c3/khach-san-tai-ho-chi-minh.html?p=13,14',
            'https://mytour.vn/d352/khach-san-tai-da-lat.html?p=13,14',
            'https://mytour.vn/c2/khach-san-tai-ha-noi.html?p=13,14',
            'https://mytour.vn/d156/khach-san-tai-phan-thiet.html?p=13,14',
            'https://mytour.vn/d419/khach-san-tai-hoi-an.html?p=13,14',
            'https://mytour.vn/d568/khach-san-tai-hue.html?p=13,14'
        ];

	    foreach ($links as $link) {
            $this->browse( function ( Browser $browser ) use ($link) {
                $browser->visit( $link );
                $browser->pause(5000);
                $this->process($browser);

            } );
        }

	}

	public function process(Browser $browser)
	{
	    try {
            $links = $browser->driver->findElements(WebDriverBy::cssSelector('.title-sm.h3'));
            $addresses = $browser->driver->findElements(WebDriverBy::cssSelector('.text-df.mg-bt-5 > a'));
            $checkStars = $browser->driver->findElements(WebDriverBy::cssSelector('.product-item .star'));

            foreach ($links as $key => $link) {
                $name = $link->getAttribute('title');
                $address = $addresses[$key]->getAttribute('title');
                $star = 4;

                $checkStar = $checkStars[$key]->findElements(WebDriverBy::cssSelector('.star-5'));
                if(count($checkStar) > 0)
                {
                    $star = 5;
                }

                $check = \DB::connection('data2')->table('hotels')
                    ->where('name', $name)
                    ->where('address', $address)
                    ->count();
                if($check == 0) {
                    \DB::connection('data2')
                        ->table('hotels')
                        ->insert([
                            'name' => $name,
                            'address' => $address,
                            'star' => $star
                        ]);

                    dump($name, $address, $star);
                }



            }


            try {
                $next = $browser->driver->findElement(WebDriverBy::cssSelector('ul.pagination > li:last-child > a'));
            } catch (NoSuchElementException $e)
            {
                return;
            }

            if($next->getAttribute('href') == $browser->driver->getCurrentURL())
            {
                return;
            }
            dump($browser->driver->getCurrentURL());
            $browser->visit($next->getAttribute('href'));
            $browser->pause(5000);
            $this->process($browser);
        } catch (NoSuchElementException $exception)
        {

        }
	}
}
