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

    private $componentid = "";
    private $component = [];
    private $exists = false;

    public function __construct($componentid) {
        global $database;
        $this->componentid = $componentid;
        if (Component::exists($componentid)) {
            $this->exists = true;
            $this->component = $database->get("components", ["machineid", "serial", "typeid", "tested", "capacity", "model", "price", "publicnotes", "privatenotes"], ["compid" => $componentid]);
        }
    }

    public static function create(): Component {
        return new Component(Component::generateId());
    }

    public static function exists($id): bool {
        global $database;
        return $database->has('components', ['compid' => $id]);
    }

    public static function getTypes() {
        global $database;
        $types = $database->select("component_types", ["typeid (id)", "typename (name)"]);

        $list = [];
        foreach ($types as $t) {
            $list[$t['id']] = $t['name'];
        }
        return $list;
    }

    public function save() {
        global $database;
        if ($this->exists) {
            $database->update("components", $this->component, ["compid" => $this->componentid]);
        } else {
            $data = $this->component;
            $data["compid"] = $this->componentid;
            $database->insert("components", $data);
            $this->exists = true;
        }
    }

    public function getID(): string {
        return $this->componentid . "";
    }

    public function getMachineID(): string {
        if (!empty($this->component["machineid"])) {
            return $this->component["machineid"];
        }
        return "";
    }

    public function getSerial(): string {
        if (!empty($this->component["serial"])) {
            return $this->component["serial"];
        }
        return "";
    }

    public function getTypeID(): int {
        if (!empty($this->component["typeid"])) {
            return $this->component["typeid"] * 1;
        }
        return 0;
    }

    public function getTypeName(): string {
        return Component::getTypes()[$this->getTypeID()];
    }

    public function getTestedDate(): string {
        if (!empty($this->component["tested"])) {
            return $this->component["tested"];
        }
        return "";
    }

    public function getCapacity(): string {
        if (!empty($this->component["capacity"])) {
            return $this->component["capacity"];
        }
        return "";
    }

    public function getModel(): string {
        if (!empty($this->component["model"])) {
            return $this->component["model"];
        }
        return "";
    }

    public function getPrice(): float {
        if (!empty($this->component["price"])) {
            return $this->component["price"] * 1.0;
        }
        return 0.0;
    }

    public function getPublicNotes(): string {
        if (!empty($this->component["publicnotes"])) {
            return $this->component["publicnotes"];
        }
        return "";
    }

    public function getPrivateNotes(): string {
        if (!empty($this->component["privatenotes"])) {
            return $this->component["privatenotes"];
        }
        return "";
    }

    public function setMachineID($id) {
        $this->component["machineid"] = $id;
    }

    public function setSerial(string $serial) {
        $this->component["serial"] = $serial;
    }

    public function setTypeID(int $id) {
        if (!empty(Component::getTypes()[$id])) {
            $this->component["typeid"] = $id;
        }
    }

    public function setTestedDate(string $tested) {
        $this->component["tested"] = date("Y-m-d H:i:s", strtotime($tested));
    }

    public function setCapacity(string $capacity) {
        $this->component["capacity"] = $capacity;
    }

    public function setModel(string $model) {
        $this->component["model"] = $model;
    }

    public function setPrice(float $price) {
        $this->component["price"] = $price;
    }

    public function setPrivateNotes(string $notes) {
        $this->component["privatenotes"] = $notes;
    }

    public function setPublicNotes(string $notes) {
        $this->component["publicnotes"] = $notes;
    }

    /**
     * Generate a random machine ID number that is not in use.
     * @global $database
     * @param int $min Optional minimum number.
     * @param int $max Optional maximum number.
     * @return int
     */
    public static function generateId(int $min = 1000000000, int $max = 9999999999): int {
        global $database;

        $id = random_int(1000000000, 9999999999);
        ;

        do {
            $id = random_int(1000000000, 9999999999);
        } while ($database->has('components', ['compid' => $id]) || $database->has('machines', ['machineid' => $id]));

        return $id;
    }

}
