contract Community is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;

// --------------------------------------------------------
//          VARIABLE
// --------------------------------------------------------
    IERC20 public token;
    
    uint256 day = 60 * 60 * 24;
    uint256 month = day.mul(30);
    
    uint256 public totalFBSDistributor;  // Total lock token

    uint256 public timestampCreated = block.timestamp;
    uint256 public timestampLastReceived = 0;

    address[6] address_admin = [
        0x643a9719E412FFD15fEbc7b792CD597080b27889,
        0xB322aE270d6138A9484a97cC9Ca57f335eAAE919,
        0xa08196646bA898188f3368DE6E3AC7EaDaE266e6,
        0x09A9c448a547B7902C6338C333C62b3161B62289,
        0xf88fa7E2B4d3c9F7c0306B7EE81392089b386c85,
        0x3F88A5cB72dcCa11bD37241dc893C4f2Cb9B3096
    ];

    bool [13] times = [ false, false, false, false, false, false, false, false, false, false, false, false, false ];
    uint8[13] percents = [ 18, 25, 33, 42, 49, 56, 64, 72, 80, 88, 93, 98, 100 ];
    
    event Withdraw(uint256 amount);
// --------------------------------------------------------
//          CONSTRUCTOR
// --------------------------------------------------------

    constructor(address _tokenFBS, uint256 _totalFBS) {
        token = IERC20(_tokenFBS);
        totalFBSDistributor = _totalFBS;
    }

// --------------------------------------------------------
//          FUNCTION ADMIN
// --------------------------------------------------------

    function sendTransferToken(address _to, uint256 _value) internal {
        token.safeTransfer(_to, _value);
    }

    function setTokenFBSReward(address _token) public onlyOwner {
        token = IERC20(_token);
    }

// --------------------------------------------------------
//          FUNCTION HANDLER
// --------------------------------------------------------

    function withdraw() public {
        uint256 tokenPending = 0;
        bool isWithdraw = false;

        require(block.timestamp - timestampLastReceived >= 5 * month, 'You can not withdraw token now');

        for(uint8 i = 0; i < 13 ; i++) {
            if( times[i] != true ) {
                times[i] = true;
                isWithdraw = true;
                tokenPending = i == 0 ? totalFBSDistributor.mul(percents[i]).div(100) : totalFBSDistributor.mul(percents[i] - percents[i-1]).div(100);
                break;
            }
        }

        if( isWithdraw ) {
            timestampLastReceived = timestampLastReceived.add(5 * month);
            for(uint8 j = 0; j < 10; j++) {
                sendTransferToken(address_admin[j], tokenPending.div(10));
            }

            emit Withdraw(tokenPending);
        }
    }

    function refundToOwner() public onlyOwner {
        uint256 balance = token.balanceOf(address(this));
        require(balance > 0, 'Nothing to refund');
        sendTransferToken(owner(), balance);
    }
}
// 
function withdraw() public {
    uint256 tokenPending = 0;
    bool isWithdraw = false;

    require(block.timestamp - timestampLastReceived >= 5 * month, 'You can not withdraw token now');

    for(uint8 i = 0; i < 13 ; i++) {
        if( times[i] != true ) {
            times[i] = true;
            isWithdraw = true;
            tokenPending = i == 0 ? totalFBSDistributor.mul(percents[i]).div(100) : totalFBSDistributor.mul(percents[i] - percents[i-1]).div(100);
            break;
        }
    }

    if( isWithdraw ) {
        timestampLastReceived = timestampLastReceived.add(5 * month);
        for(uint8 j = 0; j < 10; j++) {
            sendTransferToken(address_admin[j], tokenPending.div(10));
        }

        emit Withdraw(tokenPending);
    }
}

// --------------------------------------------------------
//          FUNCTION HANDLER
// --------------------------------------------------------