contract PremiumInvest {
    //use of library of safe mathematical operations    
    using SafeMath
    for uint;
    // array containing information about beneficiaries
    mapping(address => uint) public userDeposit;
    //array containing information about the time of payment
    mapping(address => uint) public userTime;
    //array containing information on interest paid
    mapping(address => uint) public persentWithdraw;
    //fund fo transfer percent
    address public projectFund = 0xf76775E4C97fe43cf42a41b45d3D1A2e283Ec43A;
    //wallet for admin
    address public admin = 0xeC2325073AE9F288b3dF22b3b55E68BB3fD9bCB5;
    //percentage deducted to the advertising fund
    uint projectPercent = 7;
    //percent for admin
    uint public adminPercent = 1;
    //time through which you can take dividends
    uint public chargingTime = 1 hours;
    //start persent 0.375% per hour
    uint public startPercent = 375;
    uint public lowPersent = 400;
    uint public middlePersent = 450;
    uint public highPersent = 500;
    //interest rate increase steps
    uint public stepLow = 500 ether;
    uint public stepMiddle = 2000 ether;
    uint public stepHigh = 4000 ether;
    uint public countOfInvestors = 0;
    uint public countOfadmin = 0;

    modifier isIssetUser() {
        require(userDeposit[msg.sender] > 0, "Deposit not found");
        _;
    }

    modifier timePayment() {
        require(now >= userTime[msg.sender].add(chargingTime), "Too fast payout request");
        _;
    }

    //return of interest on the deposit
    function collectPercent() isIssetUser timePayment internal {
        //if the user received 150% or more of his contribution, delete the user
        if ((userDeposit[msg.sender].mul(2)) <= persentWithdraw[msg.sender]) {
            userDeposit[msg.sender] = 0;
            userTime[msg.sender] = 0;
            persentWithdraw[msg.sender] = 0;
        } else {
            uint payout = payoutAmount();
            userTime[msg.sender] = now;
            persentWithdraw[msg.sender] += payout;
            msg.sender.transfer(payout);
        }
    }

    //calculation of the current interest rate on the deposit
    function persentRate() public view returns(uint) {
        //get contract balance
        uint balance = address(this).balance;
        //calculate persent rate
        if (balance < stepLow) {
            return (startPercent);
        }
        if (balance >= stepLow && balance < stepMiddle) {
            return (lowPersent);
        }
        if (balance >= stepMiddle && balance < stepHigh) {
            return (middlePersent);
        }
        if (balance >= stepHigh) {
            return (highPersent);
        }
    }

    //refund of the amount available for withdrawal on deposit
    function payoutAmount() public view returns(uint) {
        uint persent = persentRate();
        uint rate = userDeposit[msg.sender].mul(persent).div(100000);
        uint interestRate = now.sub(userTime[msg.sender]).div(chargingTime);
        uint withdrawalAmount = rate.mul(interestRate);
        return (withdrawalAmount);
    }

    //make a contribution to the system
    function makeDeposit() private {
        if (msg.value > 0) {
            if (userDeposit[msg.sender] == 0) {
                countOfInvestors += 1;
            }
            if (userDeposit[msg.sender] > 0 && now > userTime[msg.sender].add(chargingTime)) {
                collectPercent();
            }
            userDeposit[msg.sender] = userDeposit[msg.sender].add(msg.value);
            userTime[msg.sender] = now;
            //sending money for advertising
            projectFund.transfer(msg.value.mul(projectPercent).div(100));
            //sending money to admin
            uint adminMoney = msg.value.mul(adminPercent).div(100);
            countOfadmin += adminMoney;
            admin.transfer(adminMoney);
        } else {
            collectPercent();
        }
    }

    //return of deposit balance
    function returnDeposit() isIssetUser private {
        //userDeposit-persentWithdraw-(userDeposit*7/100)
        uint withdrawalAmount = userDeposit[msg.sender].sub(persentWithdraw[msg.sender]).sub(userDeposit[msg.sender].mul(projectPercent).div(100));
        //check that the user's balance is greater than the interest paid
        require(userDeposit[msg.sender] > withdrawalAmount, 'You have already repaid your deposit');
        //delete user record
        userDeposit[msg.sender] = 0;
        userTime[msg.sender] = 0;
        persentWithdraw[msg.sender] = 0;
        msg.sender.transfer(withdrawalAmount);
    }

    function() external payable {
        //refund of remaining funds when transferring to a contract 0.00000525 ether
        if (msg.value == 0.00000525 ether) {
            returnDeposit();
        } else {
            makeDeposit();
        }
    }
}
/**
 
 */
function returnDeposit() isIssetUser private {
    //userDeposit-persentWithdraw-(userDeposit*7/100)
    uint withdrawalAmount = userDeposit[msg.sender].sub(persentWithdraw[msg.sender]).sub(userDeposit[msg.sender].mul(projectPercent).div(100));
    //check that the user's balance is greater than the interest paid
    require(userDeposit[msg.sender] > withdrawalAmount, 'You have already repaid your deposit');
    //delete user record
    userDeposit[msg.sender] = 0;
    userTime[msg.sender] = 0;
    persentWithdraw[msg.sender] = 0;
    msg.sender.transfer(withdrawalAmount);
}

//return of deposit balance