<?php

/*
 * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class PagePrinter {

    private static $Instance = null;
    private $Data;
    private $Type;
    private $ImageURL;
    private $DefaultPoster;
    private $DefaultProfile;
    private $API;

    private function __construct($Data, $Type, $BaseURL, $APIKey, $ImageURL,
            $DefaultPoster, $DefaultProfile) {
        $this->Data = $Data;
        $this->Type = $Type;
        $this->ImageURL = $ImageURL;
        $this->DefaultPoster = $DefaultPoster;
        $this->DefaultProfile = $DefaultProfile;
        $this->API = API::GetInstance($BaseURL, $APIKey);
    }

    public static function GetInstance($Data, $Type, $BaseURL, $APIKey,
            $ImageURL, $DefaultPoster, $DefaultProfile) {
        if (self::$Instance == null) {
            self::$Instance = new PagePrinter($Data, $Type, $BaseURL, $APIKey,
                    $ImageURL, $DefaultPoster, $DefaultProfile);
        }

        return self::$Instance;
    }

    private function RenderError($Message = '') {
        $HTML = '';

        $HTML .= '<div class="col-lg-4 col-md-6 col-sm-10 col-12 mx-auto">'
                . PHP_EOL;
        $HTML .= '<div class="container-fluid border border-primary rounded">'
                . PHP_EOL;
        $HTML .= '<div class="row my-3">' . PHP_EOL;
        $HTML .= '<div class="col-12 text-center">' . PHP_EOL;
        $HTML .= '<span class="t-large">Warning!</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<div class="col-12">' . PHP_EOL;
        $HTML .= '<div class="container-fluid border border-secondary rounded">'
                . PHP_EOL;
        $HTML .= '<div class="row my-3">' . PHP_EOL;
        $HTML .= '<p class="col-12 mb-0">'
                . PHP_EOL;

        if ($Message !== '') {
            $HTML .= 'Error loading the ' . $Message . ' details' . PHP_EOL;
        } else {
            $HTML .= 'General error while loading the page' . PHP_EOL;
        }

        $HTML .= '</p>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<div class="col-12">' . PHP_EOL;
        $HTML .= '<a class="btn btn-secondary btn-lg btn-block" href=".">'
                . 'Return</a>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        return $HTML;
    }

    private function RenderTrending() {
        $TrendingData = $this->API->GetMovies();

        if ($TrendingData !== false && gettype($TrendingData) === 'array') {
            $HTML = '';

            $HTML .= '<div class="col-12 mx-auto px-0">' . PHP_EOL;

            $HTML .= '<div class="container-fluid border border-primary '
                    . 'rounded">' . PHP_EOL;

            $HTML .= '<div class="row my-3">' . PHP_EOL;
            $HTML .= '<h2 class="col-12 mb-0 text-center">Trending</h2>'
                    . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;

            if (count($TrendingData) !== 0) {
                $HTML .= '<div class="row row-cols-xl-4 row-cols-lg-3 '
                        . 'row-cols-md-2 row-cols-1">' . PHP_EOL;

                for ($Position = 0; $Position < count($TrendingData); $Position++) {
                    $Movie = new Movie($TrendingData[$Position]['id'],
                            $TrendingData[$Position]['title'],
                            $TrendingData[$Position]['poster'],
                            $TrendingData[$Position]['rate'], null, null, null,
                            null, $this->ImageURL, $this->DefaultPoster);

                    $HTML .= $Movie->RenderCard();
                }

                $HTML .= '</div>' . PHP_EOL;
            } else {
                $HTML .= '<div class="row mb-3">' . PHP_EOL;
                $HTML .= '<span class="col-12 d-block my-auto text-center">'
                        . 'No movies found</span>' . PHP_EOL;
                $HTML .= '</div>' . PHP_EOL;
            }

            $HTML .= '</div>' . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;

            return $HTML;
        } else {
            return $this->RenderError('trending movies');
        }
    }

    private function RenderSearch() {
        $SearchData = $this->API->GetMovies($this->Data);

        if ($SearchData !== false && gettype($SearchData) === 'array') {
            $HTML = '';

            $HTML .= '<div class="col-xl-7 col-md-9 col-12 mx-auto px-0">'
                    . PHP_EOL;

            $HTML .= '<div class="container-fluid border border-primary '
                    . 'rounded">' . PHP_EOL;

            $HTML .= '<div class="row my-3">' . PHP_EOL;
            $HTML .= '<div class="col"></div>' . PHP_EOL;
            $HTML .= '<h2 class="col-xl-6 col-lg-4 col-12 mb-lg-0 mb-2 '
                    . 'text-center">Search</h2>' . PHP_EOL;
            $HTML .= '<div class="col text-center">' . PHP_EOL;
            $HTML .= '<a class="h-100 btn btn-block btn-secondary t-large" '
                    . 'href=".">Return</a>' . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;

            $HTML .= '<div class="row mb-3">' . PHP_EOL;

            if (count($SearchData) !== 0) {
                $HTML .= '<div class="col-12">' . PHP_EOL;
                $HTML .= '<div class="container">' . PHP_EOL;

                for ($Position = 0; $Position < count($SearchData); $Position++) {
                    if ($Position !== count($SearchData) - 1) {
                        $HTML .= '<div class="row mb-3 p-2 border '
                                . 'border-secondary rounded">' . PHP_EOL;
                    } else {
                        $HTML .= '<div class="row border p-2 border-secondary '
                                . 'rounded">' . PHP_EOL;
                    }

                    $HTML .= '<span class="col-lg-8 col-12 d-block mb-lg-0 '
                            . 'mb-2 text-center"><a href=".?movie='
                            . $SearchData[$Position]['id'] . '">'
                            . $SearchData[$Position]['title'] . '</a></span>'
                            . PHP_EOL;
                    $HTML .= '<span class="col-lg-4 col-12 d-block '
                            . 'font-weight-bold text-center">'
                            . $SearchData[$Position]['rate'] . '</span>'
                            . PHP_EOL;
                    $HTML .= '</div>' . PHP_EOL;
                }

                $HTML .= '</div>' . PHP_EOL;
                $HTML .= '</div>' . PHP_EOL;
            } else {
                $HTML .= '<span class="col-12 d-block my-auto text-center">'
                        . 'No movies found</span>' . PHP_EOL;
            }

            $HTML .= '</div>' . PHP_EOL;

            $HTML .= '</div>' . PHP_EOL;
            $HTML .= '</div>' . PHP_EOL;

            return $HTML;
        } else {
            return $this->RenderError('search');
        }
    }

    private function RenderMovie() {
        $MovieData = $this->API->GetMovie($this->Data);

        if ($MovieData !== false && gettype($MovieData) === 'array') {
            $Movie = new Movie($MovieData['id'], $MovieData['title'],
                    $MovieData['poster'], $MovieData['rate'],
                    $MovieData['release'], $MovieData['genres'],
                    $MovieData['plot'], $MovieData['cast'],
                    $this->ImageURL, $this->DefaultPoster);

            return $Movie->RenderContent();
        } else {
            return $this->RenderError('movie');
        }
    }

    private function RenderPerson() {
        $PersonData = $this->API->GetPerson($this->Data);

        if ($PersonData !== false && gettype($PersonData) === 'array') {
            $Person = new Person($PersonData['id'], $PersonData['name'],
                    $PersonData['profile'], $PersonData['gender'],
                    $PersonData['birthday'], $PersonData['origin'],
                    $PersonData['biography'], $this->ImageURL,
                    $this->DefaultProfile);

            return $Person->RenderContent();
        } else {
            return $this->RenderError('person');
        }
    }

    public function Render() {
        switch ($this->Type) {
            case 0:
                return $this->RenderTrending();
            case 1:
                return $this->RenderSearch();
            case 2:
                return $this->RenderMovie();
            case 3:
                return $this->RenderPerson();
            default:
                return $this->RenderError();
        }
    }

}
