<?php


namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;


use ModulesGarden\ProxmoxAddon\App\Libs\Format;
use ModulesGarden\ProxmoxAddon\App\Services\Utility;

class Resource
{

    protected $name;
    protected $unit;
    protected $min;
    protected $used;
    protected $max;
    protected $total;
    protected $free;

    /**
     * Resource constructor.
     * @param $name
     */
    public function __construct($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getUnit()
    {
        return $this->unit;
    }

    /**
     * @param mixed $unit
     * @return Resource
     */
    public function setUnit($unit)
    {
        $this->unit = $unit;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getMin()
    {
        return $this->min;
    }

    /**
     * @param mixed $min
     * @return Resource
     */
    public function setMin($min)
    {
        $this->min = $min;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getMax()
    {
        return $this->max;
    }

    /**
     * @param mixed $max
     * @return Resource
     */
    public function setMax($max)
    {
        $this->max = $max;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getUsed()
    {
        return $this->used;
    }

    /**
     * @param mixed $used
     * @return Resource
     */
    public function setUsed($used)
    {
        $this->used = $used;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getTotal()
    {
        return $this->total;
    }

    /**
     * @param mixed $total
     * @return Resource
     */
    public function setTotal($total)
    {
        $this->total = $total;
        return $this;
    }

    public function free(){
        $totalUsed = $this->freeTotal();
        if($this->max && $this->max < $totalUsed){
            return  $this->max;
        }
        return $totalUsed;
    }

    public function freeTotal(){
        return $this->total - $this->used;
    }

    public function hasFreeTotal(){
        return $this->freeTotal() > 0;
    }

    public function getPercent(){
        if(!$this->total){
            return 0;
        }
        return round($this->used / $this->total * 100);
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    public function isUnlimited(){
        return $this->total == -1;
    }


}