<?php

namespace App\Http\Controllers;

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

class Repertoire extends Controller
{

    private $main;

    public function __construct()
    {
        $this->main = new Main();
    }


    public function today_cinemacity()
    {
        $date = date('d/m/Y');
        $repertuarText = $this->cinemacityRepertuar($date);
        $this->main->sendPrint('', $repertuarText);
        return back();
    }


    public function tomorrow_cinemacity()
    {
        $date = new \DateTime();
        $date->modify('+1 day');
        $repertuarText = $this->cinemacityRepertuar($date->format('d/m/Y'));
        $this->main->sendPrint('', $repertuarText);
        return back();
    }

    public function cinemacityRepertuar($date)
    {
        $url = 'https://www.cinema-city.pl/scheduleInfo?locationId=1010308&date=' . $date . '&venueTypeId=0&hideSite=false&openedFromPopup=1';

        $document = new Document($url, true);
        $text = "Repertuar Cinema-City\n";
        $text .= str_replace('/', '-', $date) . "\n\n";

        $movies = $document->find('table tbody tr');
        foreach ($movies as $movie) {
//            dump($movie);
            $text .= $movie->find('.featureLink')[0]->text() . "\n";
            $hours = [];
            foreach ($movie->find('.prsnt a') as $projection) {
                $hours[] = trim($projection->text());
            }
            $text .= implode(', ', $hours) . "\n\n";
        }
        return $text;
    }


    private function multikinoRepertuar($date)
    {

        $text = '';
        $repertuar = json_decode(file_get_contents('https://multikino.pl/pl/repertoire/cinema/seances?id=4&from=' . $date));
        $text .= "Repertuar Multikino Gdańsk\n";
        $text .= $date . "\n\n";

        foreach ($repertuar->results as $movie) {
            $text .= $movie->title . "\n";
            $text .= $movie->print_version . "\n";

            $hours = [];
            foreach ($movie->seances as $seans) {
                $hours[] = date('H:i', strtotime($seans->beginning_date));
            }
            $text .= implode(', ', $hours) . "\n\n";
        }

        return $text;
    }

    public function today_multikino()
    {
        $multikino = new Paper\CinemaMultikino();
        $repertuarText = $multikino->convertToPrint(date('Y-m-d'), 4);
        $this->main->sendPrint('', $repertuarText);
        return back();
    }

    public function tomorrow_multikino()
    {
        $date = new \DateTime();
        $date->modify('+1 day');

        $multikino = new Paper\CinemaMultikino();
        $repertuarText = $multikino->convertToPrint($date->format('Y-m-d'), 4);
        $this->main->sendPrint('', $repertuarText);
        return back();



//        $repertuarText = $this->multikinoRepertuar($date->format('Y-m-d'));
//        $this->main->sendPrint('', $repertuarText);
//        return back();
    }


    public function tomorrow_gdynskiecentrumfilmowe()
    {
        $date = new \DateTime();
        $date->modify('+1 day');

        $repertuarText = $this->gdynskieCentrumFilmowe($date->format('d_m_Y'));
        $this->main->sendPrint('', $repertuarText);
        return back();
    }

    public function today_gdynskiecentrumfilmowe()
    {
        $date = date('d_m_Y');
        $repertuarText = $this->gdynskieCentrumFilmowe($date);
        $this->main->sendPrint('', $repertuarText);
        return back();
    }

    public function today_helios()
    {
        $this->main->sendPrint('', $this->helios(0, 2));
        $this->main->sendPrint('', $this->helios(0, 49));
        return back();
    }

    public function tomorrow_helios()
    {
        $this->main->sendPrint('', $this->helios(1, 2));
        $this->main->sendPrint('', $this->helios(1, 49));
        return back();
    }

    public function today_repertoire()
    {

        $this->today_multikino();
        $this->today_cinemacity();
        $this->today_helios();
        $this->today_gdynskiecentrumfilmowe();

    }

    public function tomorrow_repertoire()
    {

        $this->tomorrow_multikino();
        $this->tomorrow_cinemacity();
        $this->tomorrow_helios();
        $this->tomorrow_gdynskiecentrumfilmowe();
    }

    private function helios($day, $cinemaId)
    {
        $url = 'http://www.helios.pl/2,Gdansk/Repertuar/axRepertoire/?dzien=' . $day . '&kino=' . $cinemaId;


        $data = json_decode(file_get_contents($url));
        $document = new Document();
        $data->html = str_replace(["\n", "\t"], '', $data->html);
        $document->loadHtml($data->html);


        $cinemas = [2 => 'Helios Alfa', 49 => 'Helios Metropolia'];

        $text = '';
        $text .= "Repertuar $cinemas[$cinemaId]\n";

        $date = new \DateTime();
        $date->modify('+' . $day . ' day');

        $text .= $date->format('d-m-Y') . "\n\n";


        foreach ($document->find('.seance') as $seans) {
            $text .= trim($seans->find('.movie-title')[0]->text()) . "\n";
            $hours = [];
            foreach ($seans->find('.time li') as $hour) {
                $hours[] = $hour->text();
            }
            $text .= implode(', ', array_unique($hours)) . "\n\n";
        }

        return $text;
    }


    private function gdynskieCentrumFilmowe($date)
    {


        $url = 'http://www.gdynskiecentrumfilmowe.pl/kino_studyjne/repertuar/,' . strtotime($date) . ',_' . $date . '.html';
        $document = new Document($url, true);
        $text = '';
        $text .= "Repertuar Gdyńskie Centrum Filmowe\n";
        $text .= str_replace('_', '-', $date) . "\n\n";

        $movies = $document->find('.articles .article-item');
        foreach ($movies as $movie) {

            $text .= $movie->find('.item-title-int')[0]->text() . "\n";
            $hours = [];
            foreach ($movie->find('.projection span') as $projection) {
                $hours[] = $projection->innerHtml();
            }
            $text .= implode(', ', array_unique($hours)) . "\n\n";
        }

        return $text;
    }

}
