// File: CloudChatWallet.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

import "./Ownable.sol";
import "./IERC20.sol";
import "./ReentrancyGuard.sol";

/**
 * CloudChat Official Wallet Contract
 *
 * Contract Purpose
 *
 * 1. Allocate control of the wallet to multiple individuals and decentralize permissions
 * 2. The wallet funds locked in the contract, everyone can see the contract's * financial status and financial flow
 *
 * Contract Process
 *
 * Financial wallets are only allowed to submit transfer requests
 * Multiple bosses approve the transfer request based on
 * If 2/3 of the bosses agree, the transfer will be executed automatically
 * If 2/3 of the bosses reject, the transfer will be blocked and rejected
 * The transfer address is in the contract and cannot be changed by the bosses
 */
contract CloudChatWallet is Ownable, ReentrancyGuard {
    // deposit order struct
    struct Order {
        // deposit order id
        uint256 id;
        // from address
        address from;
        // deposit token address
        address token;
        // deposit token amount
        uint256 amount;
        // deposit time
        uint256 timestamp;
        // is deposit exist
        bool exist;
    }

    // finance wallet address
    address public financeWallet;
    // hot wallet address
    address public hotWallet;
    // boss address list
    address[] public bosses;
    // boss mapping
    mapping(address => bool) public bossData;
    // order mapping buy order id
    mapping(uint256 => Order) public orders;
    // approve apply mapping by token
    mapping(address => uint256) public approveData;
    // boss approve mapping by token
    mapping(address => address[]) public bossPassedData;
    // boss reject mapping by token
    mapping(address => address[]) public bossRejectedData;

    // all the events
    event setBossEvent(address boss, bool online);
    event setFinanceWalletEvent(address wallet);
    event setHotWalletEvent(address wallet);
    event DepositSuccess(uint256 id, address indexed from, uint256 value);
    event submitTransferApplySuccess(address indexed token, uint256 amount);
    event cancelTransferApplySuccess(address indexed token);
    event ApproveSuccess(
        address indexed token,
        address indexed boss,
        bool isPass
    );
    event TransferSuccess(
        address indexed token,
        address indexed to,
        uint256 value
    );
    event TransferApplyRejected(address indexed token, uint256 value);

    modifier onlyFinance() {
        require(
            financeWallet == _msgSender(),
            "Ownable: caller is not the finance"
        );
        _;
    }

    modifier onlyBoss() {
        require(bossData[_msgSender()], "Ownable: caller is not the boss");
        _;
    }

    /**
     * User deposits into the contract
     * Use the order id in the centralized server for deposits
     */
    function deposit(
        uint256 id,
        address token,
        uint256 amount
    ) external nonReentrant {
        require(!orders[id].exist, "CCError: The order already exists");
        bool success = IERC20(token).transferFrom(
            msg.sender,
            address(this),
            amount
        );
        require(success, "CCError: transfer failed");
        orders[id] = Order(
            id,
            msg.sender,
            token,
            amount,
            block.timestamp,
            true
        );
        emit DepositSuccess(id, msg.sender, amount);
    }

    /**
     * Set up the boss list
     * Add or remove
     */
    function setBoss(address boss, bool online) external onlyOwner {
        require(boss != address(0), "CCError: boss can not be zero");
        for (uint256 i = 0; i < bosses.length; i++) {
            if (bosses[i] == boss) {
                if (online) return;
                bosses[i] = bosses[bosses.length - 1];
                bosses.pop();
                delete bossData[boss];
                emit setBossEvent(boss, online);
                return;
            }
        }
        if (online) {
            bossData[boss] = true;
            bosses.push(boss);
            emit setBossEvent(boss, online);
        }
    }

    /**
     * Set up a financial wallet
     */
    function setFinanceWallet(address wallet) external onlyOwner {
        require(wallet != address(0), "CCError: wallet can not be zero");
        financeWallet = wallet;
        emit setFinanceWalletEvent(wallet);
    }

    /**
     * Set up a hot wallet
     * The contracted funds will be transferred to this address
     */
    function setHotWallet(address wallet) external onlyOwner {
        require(wallet != address(0), "CCError: wallet can not be zero");
        hotWallet = wallet;
        emit setHotWalletEvent(wallet);
    }

    /**
     * Financial address to submit transfer request
     */
    function submitTransferApply(address token, uint256 amount)
        external
        onlyFinance
    {
        require(
            approveData[token] == 0,
            "CCError: Applications for this token have been submitted"
        );
        require(
            IERC20(token).balanceOf(address(this)) > amount,
            "CCError: Insufficient token balance"
        );
        approveData[token] = amount;
        bossPassedData[token] = new address[](0);
        bossRejectedData[token] = new address[](0);
        emit submitTransferApplySuccess(token, amount);
    }

    /**
     * Reset approve data
     */
    function resetApproveData(address token) private {
        delete approveData[token];
        delete bossPassedData[token];
    }

    /**
     * Financial address cancellation transfer request
     */
    function cancelTransferApply(address token) external onlyFinance {
        require(
            approveData[token] > 0,
            "CCError: There are no cancellable applications at this time"
        );
        resetApproveData(token);
        emit cancelTransferApplySuccess(token);
    }

    /**
     * Check if the current boss has approved or rejected
     */
    function isBossApproved(address token) public view returns (bool) {
        for (uint256 i = 0; i < bossPassedData[token].length; i++) {
            if (bossPassedData[token][i] == msg.sender) {
                return true;
            }
        }
        for (uint256 i = 0; i < bossRejectedData[token].length; i++) {
            if (bossRejectedData[token][i] == msg.sender) {
                return true;
            }
        }
        return false;
    }

    /**
     * Get the final approval
     * 3 types of results:
     * approved: 1
     * rejected: 2
     * approval in progress: 0
     */
    function getApproveResult(address token) public view returns (uint256) {
        uint256 approvedCount = bossPassedData[token].length;
        uint256 rejectedCount = bossRejectedData[token].length;
        uint256 count = (bosses.length * 2) / 3;
        if (approvedCount >= count) return 1;
        if (rejectedCount >= count) return 2;
        return 0;
    }

    /**
     * The boss performs the operation of approval
     * Pass or reject this apply
     */
    function transferApprove(address token, bool isPass)
        external
        onlyBoss
        nonReentrant
    {
        require(approveData[token] > 0, "CCError: Approval cannot be made");
        require(
            !isBossApproved(token),
            "CCError: The current boss has already been approved"
        );
        if (isPass) {
            bossPassedData[token].push(msg.sender);
        } else {
            bossRejectedData[token].push(msg.sender);
        }
        emit ApproveSuccess(token, msg.sender, isPass);
        // check and execute the action of approval completion
        uint256 approveResult = getApproveResult(token);
        if (approveResult == 1) {
            uint256 amount = approveData[token];
            bool success = IERC20(token).transfer(hotWallet, amount);
            require(success, "CCError: transfer failed");
            resetApproveData(token);
            emit TransferSuccess(token, hotWallet, amount);
        } else if (approveResult == 2) {
            uint256 amount = approveData[token];
            resetApproveData(token);
            emit TransferApplyRejected(token, amount);
        }
    }
}

// File: Context.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }
}

// File: IERC20.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function transfer(address to, uint256 amount) external returns (bool);

    function allowance(address owner, address spender)
        external
        view
        returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);

    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );
}

// File: Ownable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./Context.sol";

abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(
        address indexed previousOwner,
        address indexed newOwner
    );

    constructor() {
        _transferOwnership(_msgSender());
    }

    function owner() public view virtual returns (address) {
        return _owner;
    }

    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    function renounceOwnership() external virtual onlyOwner {
        _transferOwnership(address(0));
    }

    function transferOwnership(address newOwner) external virtual onlyOwner {
        require(
            newOwner != address(0),
            "Ownable: new owner is the zero address"
        );
        _transferOwnership(newOwner);
    }

    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: ReentrancyGuard.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}