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

    private static $Instance = null;
    private $BaseURL;
    private $APIKey;

    private function __construct($BaseURL, $APIKey) {
        $this->BaseURL = $BaseURL;
        $this->APIKey = $APIKey;
    }

    public static function GetInstance($BaseURL, $APIKey) {
        if (self::$Instance == null) {
            self::$Instance = new API($BaseURL, $APIKey);
        }

        return self::$Instance;
    }

    private function CURL($URL) {
        $Request = curl_init();
        curl_setopt($Request, CURLOPT_URL, $this->BaseURL . $URL);
        curl_setopt($Request, CURLOPT_RETURNTRANSFER, 1);

        $Data = curl_exec($Request);

        curl_close($Request);

        return json_decode($Data, true);
    }

    public function GetMovies($Query = '') {
        $URL = '';

        if ($Query !== '') {
            $URL = '/search/movie?api_key=' . $this->APIKey
                    . '&language=en-US&include_adult=true&query='
                    . urlencode($Query);
        } else {
            $URL = '/trending/movie/week?api_key=' . $this->APIKey
                    . '&language=en-US';
        }

        $Data = $this->CURL($URL);

        if (!isset($Data['status_message'])) {
            $Results = $Data['results'];

            $MoviesList = [];

            if (count($Data['results']) !== 0) {
                for ($Position = 0; $Position < count($Results); $Position++) {
                    $Result = $Results[$Position];

                    $Movie = [];

                    $Movie['id'] = $Result['id'];
                    $Movie['title'] = $Result['title'];
                    $Movie['poster'] = $Result['poster_path'];
                    $Movie['rate'] = $Result['vote_average'] . ' / 10';

                    array_push($MoviesList, $Movie);
                }
            }

            return $MoviesList;
        } else {
            return false;
        }
    }

    public function GetMovie($MovieId) {
        $URLDetails = '/movie/' . $MovieId . '?api_key=' . $this->APIKey
                . '&language=en-US';
        $URLCredits = '/movie/' . $MovieId . '/credits?api_key='
                . $this->APIKey . '&language=en-US';

        $DataDetails = $this->CURL($URLDetails);
        $DataCredits = $this->CURL($URLCredits);

        if (!isset($DataDetails['status_message']) && !isset($DataCredits['status_message'])) {
            $Cast = isset($DataCredits['cast']) ? $DataCredits['cast'] : [];

            $Movie = [];

            $Movie['id'] = $DataDetails['id'];
            $Movie['title'] = $DataDetails['title'];
            $Movie['poster'] = $DataDetails['poster_path'];
            $Movie['rate'] = $DataDetails['vote_average'] . ' / 10';
            $Movie['release'] = $DataDetails['release_date'];

            $Movie['genres'] = [];

            for ($Position = 0; $Position < count($DataDetails['genres']); $Position++) {
                $Genre = $DataDetails['genres'][$Position];

                array_push($Movie['genres'], $Genre['name']);
            }

            $Movie['plot'] = $DataDetails['overview'];

            $Movie['cast'] = [];

            for ($Position = 0; $Position < count($Cast); $Position++) {
                $PersonData = $Cast[$Position];

                $Person = [];

                $Person['id'] = $PersonData['id'];
                $Person['name'] = $PersonData['name'];
                $Person['character'] = $PersonData['character'];

                array_push($Movie['cast'], $Person);
            }

            return $Movie;
        } else {
            return false;
        }
    }

    public function GetPerson($PersonId) {
        $URL = '/person/' . $PersonId . '?api_key=' . $this->APIKey
                . '&language=en-US';

        $Data = $this->CURL($URL);

        if (!isset($Data['status_message'])) {
            $Person = [];

            $Person['id'] = $Data['id'];
            $Person['name'] = $Data['name'];
            $Person['profile'] = $Data['profile_path'];

            switch ($Data['gender']) {
                case 0:
                    $Person['gender'] = 'Not specified';
                    break;
                case 1:
                    $Person['gender'] = 'Female';
                    break;
                case 2:
                    $Person['gender'] = 'Male';
                    break;
                case 3:
                    $Person['gender'] = 'Non-binary';
                    break;
            }

            $Person['birthday'] = $Data['birthday'];
            $Person['origin'] = $Data['place_of_birth'];
            $Person['biography'] = $Data['biography'];

            return $Person;
        } else {
            return false;
        }
    }

}
