<?php

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

class Child {

    private $id;
    private $familyid;
    private $name;
    private $birthday;
    private $graduated;

    public function __construct() {

    }

    public function load(int $id): Child {
        global $database;
        $info = $database->get('people', ["familyid", "name", "birthday", "graduated"], ['personid' => $id]);

        $this->id = $id;
        $this->familyid = $info['familyid'];
        $this->name = $info['name'];
        $this->birthday = strtotime($info['birthday']);
        $this->graduated = $info['graduated'] == 1;

        return $this;
    }

    public function save() {
        global $database;
        if (is_int($this->id) && $database->has("people", ['personid' => $this->id])) {
            $database->update("people", ["name" => $this->name, "birthday" => date("Y-m-d", $this->birthday), "graduated" => $this->graduated], ['personid' => $this->id]);
        } else {
            $database->insert("people", ["familyid" => $this->familyid, "name" => $this->name, "birthday" => date("Y-m-d", $this->birthday), "graduated" => $this->graduated]);
            $this->id = $database->id();
        }
    }

    public static function exists(int $cid, int $fid = null) {
        global $database;
        if (is_null($fid)) {
            return $database->has("people", [
                        'personid' => $cid
            ]);
        }
        return $database->has("people", ["AND" => [
                        'familyid' => $fid,
                        'personid' => $cid
        ]]);
    }

    public function getID(): int {
        return $this->id;
    }

    public function getFamilyID(): int {
        return $this->familyid;
    }

    public function getFamily(): Family {
        return (new Family())->load($this->familyid);
    }

    public function getName(): string {
        return $this->name;
    }

    /**
     * Get the person's birth date as a UNIX timestamp.
     * @return int
     */
    public function getBirthday(): int {
        return $this->birthday;
    }

    public function isGraduated(): bool {
        return $this->graduated == true;
    }

    public function setName(string $name) {
        $this->name = $name;
    }

    /**
     * Set the person's birth date to either a UNIX timestamp or a date string.
     * @param int $timestamp
     * @param string $date A string parseable by strtotime().
     */
    public function setBirthday(int $timestamp = null, string $date = null) {
        if (is_null($timestamp) && !is_null($date)) {
            $this->birthday = strtotime($date);
        } else if (!is_null($timestamp) && is_null($date)) {
            $this->birthday = $timestamp;
        }
    }

    public function setGraduated(bool $graduated) {
        $this->graduated = $graduated;
    }

    public function setFamilyID(int $id) {
        $this->familyid = $id;
    }

    public function setFamily(Family $f) {
        $this->familyid = $f->getID();
    }

}
