<?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 Movie {

    private $Id;
    private $Title;
    private $Poster;
    private $Rate;
    private $Release;
    private $Genres;
    private $Plot;
    private $Cast;
    private $ImageURL;
    private $DefaultImage;

    public function __construct($Id, $Title, $Poster, $Rate, $Release, $Genres, 
            $Plot, $Cast, $ImageURL, $DefaultImage) {
        $this->Id = $Id;
        $this->Title = $Title;
        $this->Poster = $Poster;
        $this->Rate = $Rate;
        $this->Release = $Release;
        $this->Genres = $Genres;
        $this->Plot = $Plot;
        $this->Cast = $Cast;
        $this->ImageURL = $ImageURL;
        $this->DefaultImage = $DefaultImage;
    }

    public function RenderCard() {
        $HTML = '';

        $HTML .= '<div class="col mb-3 mx-auto">' . PHP_EOL;
        $HTML .= '<div class="card">' . PHP_EOL;

        if ($this->Poster !== null && $this->Poster !== '') {
            $HTML .= '<img src="' . $this->ImageURL . $this->Poster
                    . '" class="card-img-top" alt="' . $this->Title . '"/>'
                    . PHP_EOL;
        } else {
            $HTML .= '<img src="' . $this->DefaultImage . '" '
                    . 'class="card-img-top" alt="Not specified"/>' . PHP_EOL;
        }

        $HTML .= '<div class="card-body">' . PHP_EOL;
        $HTML .= '<div class="card-title">' . PHP_EOL;
        $HTML .= '<div class="container-fluid">' . PHP_EOL;

        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<span class="col-5 d-block mx-auto px-0 border '
                . 'border-secondary rounded-pill font-weight-bold text-center '
                . 't-large">' . $this->Rate . '</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        
        $HTML .= '<div class="row">' . PHP_EOL;
        $HTML .= '<span class="col-12 d-block px-0 t-large">'
                . $this->Title . '</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        $HTML .= '<a class="btn btn-block btn-lg btn-primary" href=".?movie='
                . $this->Id . '">Show details</a>' . PHP_EOL;

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

        return $HTML;
    }

    public function RenderContent() {
        $HTML = '';

        $HTML .= '<div class="col-lg-10 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-md-5 col-12 mb-lg-0 mb-2 '
                . 'text-center">Movie</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">' . PHP_EOL;
        $HTML .= '<div class="col-lg-4 col-md-6 col-12 mx-auto mb-3">'
                . PHP_EOL;

        if ($this->Poster !== null && $this->Poster !== '') {
            $HTML .= '<img src="' . $this->ImageURL . $this->Poster . '" '
                    . 'class="d-block w-100 mx-auto" alt="' . $this->Title
                    . '"/>' . PHP_EOL;
        } else {
            $HTML .= '<img src="' . $this->DefaultImage . '" class="d-block '
                    . 'w-100 mx-auto" alt="Not specified"/>' . PHP_EOL;
        }

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

        $HTML .= '<div class="col-lg-8 col-12 mb-3">' . PHP_EOL;
        $HTML .= '<div class="container-fluid border border-secondary rounded">'
                . PHP_EOL;

        $HTML .= '<div class="row my-3">' . PHP_EOL;
        $HTML .= '<h3 class="col-12 mb-0">' . PHP_EOL;
        $HTML .= $this->Title . PHP_EOL;
        $HTML .= '</h3>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        
        $HTML .= '<div class="row mb-3">' . PHP_EOL;
        $HTML .= '<span class="col-sm-6 col-12 mb-sm-0 mb-3"><span '
                . 'class="t-large">Rate:</span> ' . PHP_EOL;
        $HTML .= $this->Rate . PHP_EOL;
        $HTML .= '</span>' . PHP_EOL;
        $HTML .= '<span class="col-sm-6 col-12"><span class="t-large">'
                . 'Release:</span> ' . PHP_EOL;

        if ($this->Release !== null && $this->Release !== '') {
            $HTML .= date("d/m/Y", 
                    strtotime(str_replace('-"', '/', $this->Release))) 
                    . PHP_EOL;
        } else {
            $HTML .= 'Not specified' . PHP_EOL;
        }

        $HTML .= '</span>' . 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">' . PHP_EOL;

        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<h4 class="col-12 mb-0 px-0">Genres</h4>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        $HTML .= '<div class="row">' . PHP_EOL;
        $HTML .= '<p class="col-12 mb-0 px-0">' . PHP_EOL;

        if ($this->Genres !== null && count($this->Genres) !== 0) {
            for ($Position = 0; $Position < count($this->Genres); $Position++) {
                if ($Position !== count($this->Genres) - 1) {
                    $HTML .= $this->Genres[$Position] . ', ';
                } else {
                    $HTML .= $this->Genres[$Position] . PHP_EOL;
                }
            }
        } else {
            $HTML .= 'Not specified' . 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 .= '<div class="container">' . PHP_EOL;

        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<h4 class="col-12 mb-0 px-0">Plot<h4>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        $HTML .= '<div class="row">' . PHP_EOL;
        $HTML .= '<p class="col-12 mb-0 px-0">' . PHP_EOL;

        if ($this->Plot !== null && $this->Plot !== '') {
            $HTML .= $this->Plot . PHP_EOL;
        } else {
            $HTML .= 'Not specified' . 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 .= '<div class="container">' . PHP_EOL;

        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<h4 class="col-12 mb-0 px-0">Cast</h4>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        $HTML .= '<div class="row">' . PHP_EOL;
        $HTML .= '<p class="col-12 mb-0 px-0">' . PHP_EOL;

        if ($this->Cast !== null && count($this->Cast) !== 0) {
            for ($Position = 0; $Position < count($this->Cast); $Position++) {
                if ($this->Cast[$Position]['name'] !== null && $this->Cast[$Position]['character'] !== '') {
                    $HTML .= $this->Cast[$Position]['character'];
                } else {
                    $HTML .= 'Not specified';
                }

                $HTML .= ' (<a href=".?person=' . $this->Cast[$Position]['id']
                        . '">' . $this->Cast[$Position]['name'] . '</a>)';

                if ($Position !== count($this->Cast) - 1) {
                    $HTML .= ', ' . PHP_EOL;
                } else {
                    $HTML .= '.' . PHP_EOL;
                }
            }
        } else {
            $HTML .= 'Not specified' . PHP_EOL;
        }

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

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

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

        return $HTML;
    }

}
