<?php

require_once 'Movie.php';

class Rental {
    private $_movie;
    private $_daysRented;

    public function __construct(Movie $movie, $daysRented) {
        $this->_movie = $movie;
        $this->_daysRented = $daysRented;
    }

    public function getMovie() {
        return $this->_movie;
    }

    public function getDaysRented() {
        return $this->_daysRented;
    }
}

class Customer {
    private $_name;
    private $_rentals = [];

    public function __construct($name) {
        $this->_name = $name;
    }

    public function addRental(Rental $arg) {
        $this->_rentals[] = $arg;
    }

    public function getName() {
        return $this->_name;
    }

    public function statement() {
        $totalAmount = 0;
        $frequentRenterPoints = 0;
        $rentals = $this->_rentals;

        $result = "Rental records for {$this->getName()} \n";
        foreach ($rentals as $rental) {
            $thisAmount = $this->amountFor($rental);

            // add frequent renter points
            $frequentRenterPoints++;

            // add bonus for a two days new release rental
            if (($rental->getMovie()->getPriceCode() == Movie::NEW_RELEASE) &&
                ($rental->getDaysRented() > 1)) {
                    $frequentRenterPoints++;
            }

            // show figures for this rental
            $result .= "{$rental->getMovie()->getTitle()} \t {$thisAmount} \n";

            $totalAmount += $thisAmount;
        }

        $result .= "Amount owned is {$totalAmount} \n";
        $result .= "You earned {$frequentRenterPoints} frequent renter points \n";

        return $result;
    }

    private function amountFor(Rental $rental) {
        $result = 0;

        switch ($rental->getMovie()->getPriceCode()) {
            case Movie::REGULAR :
                $result += 2;
                if ($rental->getDaysRented() > 2) {
                    $result += ($rental->getDaysRented() - 2) * 1.5;
                }
            break;
            case Movie::NEW_RELEASE :
                $result += $rental->getDaysRented() * 3;
            break;
            case Movie::CHILDREN :
                $result += 1.5;
                if ($rental->getDaysRented() > 3) {
                    $result += ($rental->getDaysRented() - 3) * 1.5;
                }
            break;
        }

        // comment on SRC
        return $result;
    }
}

?>