pragma solidity ^0.8.9;

contract NooneTeam is NooneInterface, Ownable {
    mapping(address => bool) public isCallData;
    mapping(uint256 => PaymentTo) private paymentTo_;
    
    function paymentTo(uint256 index)
        external
        view
        override
        returns (PaymentTo memory)
    {
        return paymentTo_[index];
    }

    uint256 public override paymentToCount;

    function addArrayToMapping(PaymentTo[] memory array) private {
        paymentToCount = array.length;
        for (uint256 i; i < array.length; i++) {
            paymentTo_[i] = array[i];
        }
    }

    function SetPaymentTo(PaymentTo[] calldata toPayTo)
        external
        onlyOwner
    {
        if (paymentToCount != 0) pay();
        uint256 totalPercentage = 0;
        for (uint256 i; i < toPayTo.length; i++) {
            totalPercentage += toPayTo[i].percentage;
        }
        require(totalPercentage == 100, "Percentage must be 100");

        addArrayToMapping(toPayTo);
        emit PaymentToChanged(toPayTo);
    }

    function setisCallData(address _address, bool onoff) external onlyOwner {
        isCallData[_address] = onoff;
    }

    function pay() public override {
        require(paymentToCount != 0, "No addresses to distribute set");
        if (address(this).balance == 0) return;
        uint256 totalBalance = address(this).balance;

        for (uint256 i; i < paymentToCount; i++) {
            address to = paymentTo_[i].addr;
            uint256 amount = totalBalance * paymentTo_[i].percentage / 100;
            if (isCallData[to]) {
            // Calls with non empty calldata to trigger fallback()
                payable(to).call{value: amount}("a"); 
            }
            else {
                payable(to).call{value: amount}("");
            }
        }
        emit Paid(totalBalance, _msgSender());
    }

    // solhint-disable-next-line
    receive() external payable {}
}
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"address","name":"sender","type":"address"}],"name":"Paid","type":"event"},{"anonymous":false,"inputs":[{"components":[{"internalType":"address","name":"addr","type":"address"},{"internalType":"uint256","name":"percentage","type":"uint256"}],"indexed":false,"internalType":"struct NooneInterface.PaymentTo[]","name":"payTo","type":"tuple[]"}],"name":"PaymentToChanged","type":"event"},{"inputs":[{"components":[{"internalType":"address","name":"addr","type":"address"},{"internalType":"uint256","name":"percentage","type":"uint256"}],"internalType":"struct NooneInterface.PaymentTo[]","name":"toPayTo","type":"tuple[]"}],"name":"SetPaymentTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isCallData","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pay","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"paymentTo","outputs":[{"components":[{"internalType":"address","name":"addr","type":"address"},{"internalType":"uint256","name":"percentage","type":"uint256"}],"internalType":"struct NooneInterface.PaymentTo","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"paymentToCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"},{"internalType":"bool","name":"onoff","type":"bool"}],"name":"setisCallData","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]