<?php

class Investment {

    private $name;
    private $percentage;
    private $days;
    private $group;
    private $min;
    private $max;
    private $type;
    private $id;

    function setId($id) {
        $this->id = $id;
    }

    function getId() {
        return $this->id;
    }

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

    function getName() {
        return $this->name;
    }

    function setPercentage($percentage) {
        $this->percentage = $percentage;
    }

    function getPercentage() {
        return $this->percentage;
    }

    function setDays($days) {
        $this->days = $days;
    }

    function getDays() {
        return $this->days;
    }

    function setGroup($group) {
        $this->group = $group;
    }

    function getIgroup() {
        return $this->igroup;
    }

    function setIgroup($igroup) {
        $this->igroup = $igroup;
    }

    function getGroup() {
        return $this->group;
    }

    function setMin($min) {
        $this->min = $min;
    }

    function getMin() {
        return number_format($this->min);
    }

    function getMinUnformatted() {
        return $this->min;
    }

    function setMax($max) {
        $this->max = $max;
    }

    function getMax() {
        return ($this->max == -1) ? UNLIMITED : number_format($this->max);
    }

    function getMaxUnformatted() {
        return $this->max;
    }

    function setType($type) {
        $this->type = $type;
    }

    function getType() {
        return $this->type;
    }

    /**
     * Get investment groups
     *
     * @param mysqli $link
     * @access public
     *
     * @return Investment Array of groups or null;
     */
    public static function getInvesmentGroup($link) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $query = "select * from igroup ";
        $groups = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $group = mysqli_fetch_object($result, Investment::class);
            array_push($groups, $group);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $groups;
    }

    /**
     * Get investments on system
     *
     * @param mysqli $link
     * @access public
     *
     * @return Investment Array of investments or null;
     */
    public static function getInvesments($link) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $query = "select * from investment ";
        $groups = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $group = mysqli_fetch_object($result, Investment::class);
            array_push($groups, $group);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $groups;
    }

    /**
     * Get investments on system by name
     *
     * @param mysqli $link
     * @access public
     *
     * @return Investment Array of investments or null;
     */
    public static function getInvesmentByName($link, $name) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $query = "select * from investment WHERE igroup = '$name'";
        $groups = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $group = mysqli_fetch_object($result, Investment::class);
            array_push($groups, $group);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $groups;
    }

    /**
     * Get investments on system by name
     *
     * @param mysqli $link
     * @access public
     *
     * @return Investment Array of investments or null;
     */
    public static function getInvesmentPlansByName($link, $name) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $query = "select * from investment WHERE `name` = '$name'";
        $groups = array();
        $result = mysqli_query($link, $query);
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $group = mysqli_fetch_object($result, Investment::class);
            array_push($groups, $group);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $groups[0];
    }

    /**
     * Delete an investment group
     *
     * @param Int $id Id of the investment to delete
     * @param mysqli $link
     * @access public
     * @return true or false on failure
     */
    public static function deleteInvestmentGroup($id, $link) {
        $stmt = mysqli_prepare($link, "delete from igroup where id = ?");
        $stmt->bind_param('i', $id);
        $stmt->execute();
        if ($stmt->affected_rows === 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Delete an investment
     *
     * @param String $id Id of the investment to delete
     * @param mysqli $link
     * @access public
     * @return true or false on failure
     */
    public static function deleteInvestment($id, $link) {
        $stmt = mysqli_prepare($link, "delete from investment where id = ?");
        $stmt->bind_param('i', $id);
        $stmt->execute();
        if ($stmt->affected_rows === 1) {
            return true;
        } else {
            return false;
        }
    }

}
