<?php
/**
 * Created by PhpStorm.
 * User: Benedikt
 * Date: 19.07.19
 * Time: 09:02
 */
if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

require_once APPPATH . 'us_libraries/Value_object.php';

class Timelogging_values extends Value_object {

    protected $hours;
    protected $project_name;
    protected $project_number;
    protected $date;
    protected $package;
    protected $staff;
    protected $from;
    protected $to;
    protected $is_client_changed;
    protected $purpose;
    protected $purpose_internal;

    public static function init_from_values(stdClass $values) {

        $obj = new self();
        $obj->CI = CI_Controller::get_instance();
        $obj->hours = $values->hours;
        $obj->project_number = $values->project_number;
        $obj->project_name = $values->project_name;
        $obj->package = $values->package;
        $obj->staff = $values->staff;
        $obj->purpose = $values->purpose;
        $obj->purpose_internal = $values->purpose_internal;

        $obj->date = ($values->date !== 0) ? new UsDate(self::fixDateFormat($values->date)) : new UsDate(now());
        $obj->from = ($values->from !== 0) ? new UsDateTime(self::fixDateFormat($values->from)) : new UsDateTime();
        $obj->to = ($values->to !== 0) ? new UsDateTime(self::fixDateFormat($values->to)) : new UsDateTime();

        $obj->is_client_changed = (bool)$values->is_client_changed;

        return $obj;
    }

    private static function fixDateFormat($excelDate) {

        $result = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::toFormattedString($excelDate, 'YYYY-MM-DD HH:MM:SS');

        return $result;
    }

    public function get_time_to_save() {

        $invalid_msg = $this->validate();

        return array(
            'hours' => $this->hours,
            'project_name' => $this->project_name,
            'project_number' => $this->project_number,
            'package' => $this->package,
            'staff' => $this->staff,
            'date' => $this->date,
            'from' => $this->from->us_stime(),
            'to' => $this->to->us_stime(),
            'is_client_changed' => $this->is_client_changed,
            'purpose' => $this->purpose,
            'purpose_internal' => $this->purpose_internal,
        );
    }

    /**
     * Validate the values
     *
     * @return  boolean
     */
    public function validate() {

        if (!is_numeric($this->add_or_id) && $this->add_or_id !== 'add') {
            return 'add_or_id has to be "add" or an id!';
        }

        if (!is_numeric($this->hours)) {
            return "Hours have to be numeric!";
        }

        if (!isset($this->project_name) && !isset($this->project_number)) {
            return "No project identifier!";
        }

        if (!isset($this->package)) {
            return "No work package specified!";
        }

        return false;
    }

    public function __sleep() {

        return array(
            'hours',
            'project_name',
            'project_number',
            'date',
            'package',
            'staff',
            'from',
            'to',
            'is_client_changed',
            'purpose',
            'purpose_internal',
        );
    }

    /**
     * Reconstruct any resources
     *
     * @return  void
     */
    public function __wakeup() {

        $this->CI = CI_Controller::get_instance();
    }
}
