contract Basic is Ownable {
    using SafeMath for uint256;

    // This creates an array with all balances
    mapping(address => uint256) public totalAmount;
    mapping(address => uint256) public availableAmount;
    mapping(address => uint256) public withdrawedAmount;
    uint[] public periods;
    uint256 public currentPeriod;
    smartContract public contractAddress;
    uint256 public ownerWithdrawalDate;

    // fix for short address attack
    modifier onlyPayloadSize(uint size) {
        assert(msg.data.length == size + 4);
        _;
    }

    /**
     * Constructor function
     *
     * transfer tokens to the smart contract here
     */
    function Basic(address _contractAddress) public onlyOwner {
        contractAddress = smartContract(_contractAddress);
    }

    function _recalculateAvailable(address _addr) internal {
        _updateCurrentPeriod();
        uint256 available;
        uint256 calcPeriod = currentPeriod + 1;
        if (calcPeriod < periods.length) {
            available = totalAmount[_addr].div(periods.length).mul(calcPeriod);
            //you don't have anything to withdraw
            require(available > withdrawedAmount[_addr]);
            //remove already withdrawed tokens
            available = available.sub(withdrawedAmount[_addr]);
        } else {
            available = totalAmount[_addr].sub(withdrawedAmount[_addr]);
        }
        availableAmount[_addr] = available;
    }

    function addRecipient(address _from, uint256 _amount) external onlyOwner onlyPayloadSize(2 * 32) {
        require(_from != 0x0);
        require(totalAmount[_from] == 0);
        totalAmount[_from] = _amount;
        availableAmount[_from] = 0;
        withdrawedAmount[_from] = 0;
    }

    function withdraw() public payable {
        _withdraw(msg.sender);
    }

    function _withdraw(address _addr) internal {
        require(_addr != 0x0);
        require(totalAmount[_addr] > 0);

        //Recalculate available balance if time has come
        _recalculateAvailable(_addr);
        require(availableAmount[_addr] > 0);
        uint256 available = availableAmount[_addr];
        withdrawedAmount[_addr] = withdrawedAmount[_addr].add(available);
        availableAmount[_addr] = 0;

        contractAddress.transfer(_addr, available);
    }

    function triggerWithdraw(address _addr) public payable onlyOwner {
        _withdraw(_addr);
    }

    // owner may withdraw funds after some period of time
    function withdrawToOwner(uint256 _amount) external onlyOwner {
        // no need to create modifier for one case
        require(now > ownerWithdrawalDate);
        contractAddress.transfer(msg.sender, _amount);
    }

    function _updateCurrentPeriod() internal {
        require(periods.length >= 1);
        for (uint i = 0; i < periods.length; i++) {
            if (periods[i] <= now && i >= currentPeriod) {
                currentPeriod = i;
            }
        }
    }
}
function Basic(address _contractAddress) public onlyOwner {
        contractAddress = smartContract(_contractAddress);
    }
/**
 * Constructor function
 *
 * transfer tokens to the smart contract here
 */