<?php

class PaymentWallet {

    private $id;
    private $name;
    private $address;
    private $symbol;
    private $note;

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

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

    function getNote() {
        return $this->note;
    }

    function setSymbol($symbol) {
        $this->symbol = $symbol;
    }

    function getSymbol() {
        return $this->symbol;
    }

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

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

    function setAddress($address) {
        $this->address = $address;
    }

    function getAddress() {
        return $this->address;
    }

    /**
     * Fetch payment wallet using the name provided
     *
     * @param mysqli $link
     * @param String $name payment wallet to fetch
     * @access public
     *
     * @return PaymentWallet returns payment wallet or null on failure
     */
    public static function getPaymentWallet($link, $name) {
        $result = mysqli_query($link, "select * from paymentwallet where `name` = '$name'");
        $wallet = (mysqli_num_rows($result) === 1) ? mysqli_fetch_object($result, PaymentWallet::class) : NULL;
        return $wallet;
    }

    /**
     * Fetch all payment wallet
     *
     * @param mysqli $link
     * @access public
     *
     * @return PaymentWallet returns all payment wallet on system or null on failure
     */
    public static function getAllPaymentsWallets($link) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $wallets = array();
        $result = mysqli_query($link, "select * from paymentwallet order BY `name` asc");
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $wallet = mysqli_fetch_object($result, PaymentWallet::class);
            array_push($wallets, $wallet);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $wallets;
    }

    /**
     * Fetch unique payment wallet name
     *
     * @param mysqli $link
     * @access public
     *
     * @return PaymentWallet returns all payment wallet on system or null on failure
     */
    public static function getAllPaymentsWalletsName($link) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $wallets = array();
        $result = mysqli_query($link, "select distinct `name`, symbol, note from paymentwallet order BY `name` asc");
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $wallet = mysqli_fetch_object($result, PaymentWallet::class);
            array_push($wallets, $wallet);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $wallets;
    }

    public static function getAllPaymentsWalletsAddress($link, $name, $note = NULL) {
        if (!$link instanceof mysqli) {
            throw new Exception("Bad arguments passed: \n");
        }
        $wallets = array();
        if($note == null || empty(trim($note))) {
            $result = mysqli_query($link, "select address, note, name from paymentwallet WHERE `name` = '$name'");
        }else {
            $result = mysqli_query($link, "select address, note, name from paymentwallet WHERE `name` = '$name' and note = '$note'");
        }
        if (mysqli_num_rows($result) === 0) {
            return NULL;
        }
        for ($i = 0; $i < mysqli_num_rows($result); $i++) {
            $wallet = mysqli_fetch_object($result, PaymentWallet::class);
            array_push($wallets, $wallet);
            if (mysqli_more_results($link)) {
                mysqli_next_result($link);
            }
        }
        return $wallets;
    }

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

}
