<?php

/*
 * Copyright 2020 Netsyms Technologies.
 * 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 Container {

    public $containerid = "";
    public $barcode = "";
    public $exists = false;
    private $machines = [];
    private $components = [];
    private $others = [];

    public function __construct($containerid) {
        global $database;

        if ($database->has("containers", ["containerid" => $containerid])) {
            $this->exists = true;
            $info = $database->get("containers", ["barcode"], ["containerid" => $containerid]);
            $this->barcode = $info["barcode"];
        } else {
            $this->exists = false;
        }
    }

    public function save() {
        global $database;

        if ($database->has("containers", ["containerid" => $this->containerid])) {
            $database->update("containers", [
                "barcode" => $this->barcode
                    ], ["containerid" => $this->containerid]);
        } else {
            $database->insert("containers", ["barcode" => $this->barcode]);
            $this->containerid = $database->id();
            $this->exists = true;
        }

        $database->action(function ($database) {
            $database->delete("container_contents", ["containerid" => $this->containerid]);

            foreach ($this->machines as $id) {
                $database->insert("container_contents", [
                    "machineid" => $id,
                    "containerid" => $this->containerid
                ]);
            }

            foreach ($this->components as $id) {
                $database->insert("container_contents", [
                    "componentid" => $id,
                    "containerid" => $this->containerid
                ]);
            }

            foreach ($this->others as $id) {
                $database->insert("container_contents", [
                    "otherid" => $id,
                    "containerid" => $this->containerid
                ]);
            }
        });
    }

    public function getMachineIDs(): array {
        return $this->machines;
    }

    public function getComponentIDs(): array {
        return $this->components;
    }

    public function getOtherIDs(): array {
        return $this->others;
    }

    public function addMachine($machineid) {
        global $database;
        if ($database->has("machines", ["machineid" => $machineid])) {
            $this->machines[] = $machineid;
        } else {
            throw new Exception("No machine with ID $machineid exists.");
        }
    }

    public function addComponent($componentid) {
        global $database;
        if ($database->has("components", ["compid" => $componentid])) {
            $this->components[] = $componentid;
        } else {
            throw new Exception("No component with ID $componentid exists.");
        }
    }

    public function addOtherID($id) {
        $this->others[] = $id;
    }

    public function rmMachine($machineid) {
        foreach (array_keys($this->machines, $machineid) as $id) {
            unset($this->machines[$id]);
        }
    }

    public function rmComponent($componentid) {
        foreach (array_keys($this->components, $componentid) as $id) {
            unset($this->components[$id]);
        }
    }

    public function rmOtherID($oid) {
        foreach (array_keys($this->others, $oid) as $id) {
            unset($this->others[$id]);
        }
    }

}
