<?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 Extras_values extends Value_object {

    protected $project_number;
    protected $project_name;
    protected $extra_type;
    protected $amount;
    protected $work_package;
    protected $is_client_change;
    protected $purpose;
    protected $description;
    protected $date;
    protected $staff_name;

    protected $project_id;
    protected $staff_id;
    protected $work_package_id;
    protected $extra_type_id;

    public static function init_from_values(stdClass $values) {

        $obj = new self();
        $obj->CI = CI_Controller::get_instance();
        $obj->project_number = $values->project_number;
        $obj->project_name = $values->project_name;
        $obj->extra_type = $values->extra_type;
        $obj->amount = $values->amount;
        $obj->work_package = $values->work_package;
        $obj->is_client_change = (bool)$values->is_client_change;
        $obj->purpose = $values->purpose;
        $obj->description = $values->description;
        $obj->date = new UsDate(self::fixDateFormat($values->date));
        $obj->staff_name = $values->staff_name;

        $obj->_prepare_ids();

        return $obj;
    }

    private static function fixDateFormat($excelDate) {

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

        return $result;
    }

    private function _prepare_ids() {

        $this->CI->load->model('Projects_model', 'projects');
        $this->CI->load->model('Staff_model', 'staff');
        $this->CI->load->model('Work_packages_model', 'packages');
        $this->CI->load->model('Extras_types_model', 'extras_types');

        $proj_req_vars = new stdClass();

        if (isset($this->project_number))
            $proj_req_vars->project_number = $this->project_number;
        else
            $proj_req_vars->project_name = $this->project_name;


        $this->project_id = $this->CI->projects->get_list($proj_req_vars, null, null, null, 'id', 1, false, false)[0]->id;

        $all_staff = $this->CI->staff->get_list();
        foreach ($all_staff as $staff) {
            if ($this->staff_name === $staff->staff_name)
                $this->staff_id = $staff->id;
        }

        $all_work_packages = $this->CI->packages->get_list($this->project_id);
        foreach ($all_work_packages as $package) {
            if ($this->work_package === $package->name)
                $this->work_package_id = $package->id;
        }

        $all_extras_types = $this->CI->extras_types->get_list();
        foreach ($all_extras_types as $type) {
            if ($this->extra_type === $type->type)
                $this->extra_type_id = $type->id;
        }
    }

    public function get_extra_to_save() {

        $invalid_msg = $this->validate();

        return array(
            'project_number' => $this->project_number,
            'project_name' => $this->project_name,
            'extra_type' => $this->extra_type,
            'amount' => $this->amount,
            'work_package' => $this->work_package,
            'is_client_change' => $this->is_client_change,
            'purpose' => $this->purpose,
            'description' => $this->description,
            'date' => $this->date,
            'staff_name' => $this->staff_name,
            'project_id' => $this->project_id,
            'staff_id' => $this->staff_id,
            'work_package_id' => $this->work_package_id,
            'extra_type_id' => $this->extra_type_id,
        );
    }

    /**
     * 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!';
        }

        return false;
    }

    public function __sleep() {

        return array(
            'project_number',
            'project_name',
            'extra_type',
            'amount',
            'work_package',
            'purpose',
            'is_client_change',
            'description',
            'date',
            'staff_name',
            'project_id',
            'staff_id',
            'work_package_id',
            'extra_type_id',
        );
    }

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

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