contract BonumFinancialTokenPreSale is Pausable{
    using SafeMath for uint;

    string public constant name = "Bonum Financial Token PreSale";

    uint public startDate;
    uint public endDate;
    uint public whiteListPreSaleDuration = 1 days;

    function setWhiteListDuration(uint duration) external onlyOwner{
        require(duration > 0);
        whiteListPreSaleDuration = duration * 1 days;
    }

    uint public fiatValueMultiplier = 10**6;
    uint public tokenDecimals = 10**18;

    InvestorsList public investors;

    address beneficiary;

    uint public ethUsdRate;
    uint public collected = 0;
    uint public tokensSold = 0;
    uint public tokensSoldWithBonus = 0;

    uint[] firstColumn;
    uint[] secondColumn;

    event NewContribution(address indexed holder, uint tokenAmount, uint etherAmount);

    function BonumFinancialTokenPreSale(
        uint _startDate,
        uint _endDate,
        address _investors,
        address _beneficiary,
        uint _baseEthUsdRate
    ) public {
        startDate = _startDate;
        endDate = _endDate;

        investors = InvestorsList(_investors);
        beneficiary = _beneficiary;

        ethUsdRate = _baseEthUsdRate;

        initBonusSystem();
    }


    function initBonusSystem() private{
        firstColumn.push(1750000);
        firstColumn.push(10360000);
        firstColumn.push(18980000);
        firstColumn.push(25000000);

        secondColumn.push(1560000);
        secondColumn.push(9220000);
        secondColumn.push(16880000);
        secondColumn.push(22230000);
    }

    function setNewBeneficiary(address newBeneficiary) external onlyOwner {
        require(newBeneficiary != 0x0);
        beneficiary = newBeneficiary;
    }

    function setEthUsdRate(uint rate) external onlyOwner {
        require(rate > 0);
        ethUsdRate = rate;
    }

    function setNewStartDate(uint newStartDate) external onlyOwner{
        require(newStartDate > 0);
        startDate = newStartDate;
    }

    function setNewEndDate(uint newEndDate) external onlyOwner{
        require(newEndDate > 0);
        endDate = newEndDate;
    }

    function setNewInvestorsList(address investorsList) external onlyOwner {
        require(investorsList != 0x0);
        investors = InvestorsList(investorsList);
    }

    modifier activePreSale(){
        require(now >= startDate && now < endDate);
        _;
    }

    modifier underCap(){
        require(tokensSold < uint(750000).mul(tokenDecimals));
        _;
    }

    modifier isAllowedToBuy(){
        require(investors.isAllowedToBuyByAddressWithoutVerification(msg.sender));
        _;
    }

    modifier minimumAmount(){
        require(msg.value.mul(ethUsdRate).div(fiatValueMultiplier.mul(1 ether)) >= 100);
        _;
    }


    function() payable public whenNotPaused activePreSale minimumAmount underCap isAllowedToBuy{

        bytes32 id = investors.nativeInvestorsIds(msg.sender);

        uint tokens = msg.value.mul(ethUsdRate).div(fiatValueMultiplier);

        tokensSold = tokensSold.add(tokens);
        tokens = tokens.add(calculateBonus(id, tokens));
        tokensSoldWithBonus =  tokensSoldWithBonus.add(tokens);

        NewContribution(msg.sender, tokens, msg.value);

        collected = collected.add(msg.value);
        investors.addTokens(id, tokens);

        beneficiary.transfer(msg.value);
    }

    //usd * 10^6
    function otherCoinsPurchase(bytes32 id, uint amountInUsd) external whenNotPaused underCap activePreSale onlyOwner {
        require(id.length > 0 && amountInUsd >= (uint(100).mul(fiatValueMultiplier)) && investors.isAllowedToBuy(id));

        uint tokens = amountInUsd.mul(tokenDecimals).div(fiatValueMultiplier);

        tokensSold = tokensSold.add(tokens);
        tokens = tokens.add(calculateBonus(id, tokens));
        tokensSoldWithBonus =  tokensSoldWithBonus.add(tokens);

        investors.addTokens(id, tokens);
    }


    function calculateBonus(bytes32 id, uint tokensCount) public constant returns (uint){
        if (now < (startDate.add(whiteListPreSaleDuration))) {
            require(tokensCount >= 3000 * tokenDecimals);

            if (investors.isPreWhiteListed(id)) {
                return tokensCount.mul(35).div(100);
            }
            return tokensCount.mul(25).div(100);
        }

        //+1 because needs whole days
        uint day = ((now.sub(startDate.add(whiteListPreSaleDuration))).div(1 days)).add(1);
        uint B1;
        uint B2;

        if (tokensCount < uint(1000).mul(tokenDecimals)) {
            B1 = (((tokensCount - 100 * tokenDecimals) * (firstColumn[1] - firstColumn[0])) /  ((1000-100) * tokenDecimals)) + firstColumn[0];
            B2 = (((tokensCount - 100 * tokenDecimals) * (secondColumn[1] - secondColumn[0])) /  ((1000-100) * tokenDecimals)) + secondColumn[0];
        }

        if (tokensCount >= uint(1000).mul(tokenDecimals) && tokensCount < uint(10000).mul(tokenDecimals)) {
            B1 = (((tokensCount - 1000 * tokenDecimals) * (firstColumn[2] - firstColumn[1])) / ((10000-1000) * tokenDecimals)) + firstColumn[1];
            B2 = (((tokensCount - 1000 * tokenDecimals) * (secondColumn[2] - secondColumn[1])) / ((10000-1000) * tokenDecimals)) + secondColumn[1];
        }

        if (tokensCount >= uint(10000).mul(tokenDecimals) && tokensCount < uint(50000).mul(tokenDecimals)) {
            B1 = (((tokensCount - 10000 * tokenDecimals) * (firstColumn[3] - firstColumn[2])) / ((50000-10000) * tokenDecimals)) + firstColumn[2];
            B2 = (((tokensCount - 10000 * tokenDecimals) * (secondColumn[3] - secondColumn[2])) / ((50000-10000) * tokenDecimals)) + secondColumn[2];
        }

        if (tokensCount >=  uint(50000).mul(tokenDecimals)) {
            B1 = firstColumn[3];
            B2 = secondColumn[3];
        }

        uint bonusPercent = B1.sub(((day - 1).mul(B1 - B2)).div(12));

        return calculateBonusTokensAmount(tokensCount, bonusPercent);
    }

    function calculateBonusTokensAmount(uint tokensCount, uint bonusPercent) private constant returns(uint){
        uint bonus = tokensCount.mul(bonusPercent);
        bonus = bonus.div(100);
        bonus = bonus.div(fiatValueMultiplier);
        return bonus;
    }
}
function otherCoinsPurchase(bytes32 id, uint amountInUsd) external whenNotPaused underCap activePreSale onlyOwner {
    require(id.length > 0 && amountInUsd >= (uint(100).mul(fiatValueMultiplier)) && investors.isAllowedToBuy(id));

    uint tokens = amountInUsd.mul(tokenDecimals).div(fiatValueMultiplier);

    tokensSold = tokensSold.add(tokens);
    tokens = tokens.add(calculateBonus(id, tokens));
    tokensSoldWithBonus =  tokensSoldWithBonus.add(tokens);

    investors.addTokens(id, tokens);
}

//usd * 10^6