<?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 Job implements JsonSerializable {

    private $jobid = -1;
    private $exists = false;
    private $machineid = "";
    private $jobname = "";
    private $clientid = "";
    private $startdate = 0;
    private $deadline = 0;
    private $finishdate = 0;
    private $info = "";
    private $notes = "";

    public function __construct($jobid = false) {
        global $database;
        if ($jobid !== false) {
            $this->jobid = $jobid;
            if (Job::exists($jobid)) {
                $this->exists = true;
                $info = $database->get("jobs", ["[>]machines" => ["machineid"]], ["jobid", "jobs.machineid", "jobname", "clientid", "startdate", "deadline", "finishdate", "jobinfo", "jobnotes"], ["jobid" => $jobid]);
                $this->machineid = $info["machineid"];
                $this->jobname = $info["jobname"];
                $this->clientid = $info["clientid"];
                $this->startdate = strtotime($info["startdate"]);
                $this->deadline = strtotime($info["deadline"]);
                $this->finishdate = strtotime($info["finishdate"]);
                $this->info = $info["jobinfo"];
                $this->notes = $info["jobnotes"];
            }
        }
    }

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

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

    public function toArray() {
        global $Strings;
        if ($this->exists) {
            return [
                "info" => [
                    "jobid" => $this->getID(),
                    "jobname" => $this->getName(),
                    "machineid" => $this->getMachineID(),
                    "startdate" => $this->getStartDate(),
                    "deadline" => $this->getDeadline(),
                    "finishdate" => $this->getFinishDate(),
                    "jobinfo" => $this->getInfo(),
                    "jobnotes" => $this->getNotes()
                ],
                "formdata" => [
                    "inputtypes" => [
                        "jobname" => "text",
                        "machineid" => "text",
                        "startdate" => "datetime",
                        "deadline" => "datetime",
                        "finishdate" => "datetime",
                        "jobinfo" => "textarea",
                        "jobnotes" => "textarea"
                    ],
                    "labels" => [
                        "machineid" => $Strings->get("Machine ID", false),
                        "jobname" => $Strings->get("Job Title", false),
                        "startdate" => $Strings->get("Started", false),
                        "deadline" => $Strings->get("Deadline", false),
                        "finishdate" => $Strings->get("Finished", false),
                        "jobinfo" => $Strings->get("Info", false),
                        "jobnotes" => $Strings->get("Notes", false)
                    ],
                    "icons" => []
                ]
            ];
        }
        return [];
    }

    public function jsonSerialize() {
        return $this->toArray();
    }

    public function save() {
        global $database;
        $data = [
            "machineid" => $this->getMachineID(),
            "jobname" => $this->getName(),
            "startdate" => date("Y-m-d H:i:s", $this->getStartDate()),
            "deadline" => date("Y-m-d H:i:s", $this->getDeadline()),
            "finishdate" => date("Y-m-d H:i:s", $this->getFinishDate()),
            "jobinfo" => $this->getInfo(),
            "jobnotes" => $this->getNotes()
        ];
        if ($this->exists) {
            $database->update("jobs", $data, ["jobid" => $this->jobid]);
        } else {
            $database->insert("jobs", $data);
            $this->jobid = $database->id();
            $this->exists = true;
        }
    }

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

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

    public function getName(): string {
        if (!empty($this->jobname)) {
            return $this->jobname;
        }
        return "";
    }

    public function getStartDate(): int {
        if (!empty($this->startdate)) {
            return $this->startdate;
        }
        return 0;
    }

    public function getDeadline(): int {
        if (!empty($this->deadline)) {
            return $this->deadline;
        }
        return 0;
    }

    public function getFinishDate(): int {
        if (!empty($this->finishdate)) {
            return $this->finishdate;
        }
        return 0;
    }

    public function getInfo(): string {
        if (!empty($this->info)) {
            return $this->info;
        }
        return "";
    }

    public function getNotes(): string {
        if (!empty($this->notes)) {
            return $this->notes;
        }
        return "";
    }

    public function setMachineID($id) {
        $this->machineid = $id;
    }

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

    public function setStartDate($date) {
        $this->startdate = strtotime($date);
    }

    public function setDeadline($date) {
        $this->deadline = strtotime($date);
    }

    public function setFinishDate($date) {
        $this->finishdate = strtotime($date);
    }

    public function setInfo(string $info) {
        $this->info = $info;
    }

    public function setNotes(string $notes) {
        $this->notes = $notes;
    }

}
