contract BIGFARMCoin is Authorizable, TokenERC20 {

    using SafeMath for uint256;
    
    /// Maximum tokens to be allocated on the sale
    uint256 public tokenSaleHardCap;
    /// Base exchange rate is set to 1 ETH = BIF.
    uint256 public baseRate;

   /// no tokens can be ever issued when this is set to "true"
    bool public tokenSaleClosed = false;

    mapping (address => bool) public frozenAccount;

    /* This generates a public event on the blockchain that will notify clients */
    event FrozenFunds(address target, bool frozen);

    modifier inProgress {
        require(totalSupply < tokenSaleHardCap
            && !tokenSaleClosed);
        _;
    }

    modifier beforeEnd {
        require(!tokenSaleClosed);
        _;
    }

    /* Initializes contract with initial supply tokens to the creator of the contract */
    constructor(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {
        tokenSaleHardCap = 3376050000 * 10**uint256(decimals); // Default Crowsale Hard Cap amount with decimals
        baseRate = 4000 * 10**uint256(decimals); // Default base rate BIF :1 eth amount with decimals
    }

    /// @dev This default function allows token to be purchased by directly
    /// sending ether to this smart contract.
    function () public payable {
       purchaseTokens(msg.sender);
    }
    
    /// @dev Issue token based on Ether received.
    /// @param _beneficiary Address that newly issued token will be sent to.
    function purchaseTokens(address _beneficiary) public payable inProgress{
        // only accept a minimum amount of ETH?
        require(msg.value >= 0.01 ether);

        uint _tokens = computeTokenAmount(msg.value); 
        doIssueTokens(_beneficiary, _tokens);
        /// forward the raised funds to the contract creator
        owner.transfer(address(this).balance);
    }
    
    /* Internal transfer, only can be called by this contract */
    function _transfer(address _from, address _to, uint _value) internal {
        require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
        require (balanceOf[_from] >= _value);               // Check if the sender has enough
        require (balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        require(!frozenAccount[_from]);                     // Check if sender is frozen
        require(!frozenAccount[_to]);                       // Check if recipient is frozen
        balanceOf[_from] -= _value;                         // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
        emit Transfer(_from, _to, _value);
    }

    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintToken(address target, uint256 mintedAmount) onlyAuthorized public {
        balanceOf[target] += mintedAmount;
        totalSupply += mintedAmount;
        emit Transfer(0, this, mintedAmount);
        emit Transfer(this, target, mintedAmount);
    }

    /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
    /// @param target Address to be frozen
    /// @param freeze either to freeze it or not
    function freezeAccount(address target, bool freeze) onlyAuthorized public {
        frozenAccount[target] = freeze;
        emit FrozenFunds(target, freeze);
    }

    /// @notice Allow users to buy tokens for `newRatePrice` eth 
    /// @param newRate Price the users can sell to the contract
    function setRatePrices(uint256 newRate) onlyAuthorized public {
        baseRate = newRate;
    }

    /// @notice Allow users to buy tokens for `newTokenSaleHardCap` BIF 
    /// @param newTokenSaleHardCap Amount of BIF token sale hard cap
    function setTokenSaleHardCap(uint256 newTokenSaleHardCap) onlyAuthorized public {
        tokenSaleHardCap = newTokenSaleHardCap;
    }

    function doIssueTokens(address _beneficiary, uint256 _tokens) internal {
        require(_beneficiary != address(0));
        balanceOf[_beneficiary] += _tokens;
        totalSupply += _tokens;
        emit Transfer(0, this, _tokens);
        emit Transfer(this, _beneficiary, _tokens);
    }

    /// @dev Compute the amount of BIF token that can be purchased.
    /// @param ethAmount Amount of Ether in WEI to purchase BIF.
    /// @return Amount of BIF token to purchase
    function computeTokenAmount(uint256 ethAmount) internal view returns (uint256) {
        uint256 tokens = ethAmount.mul(baseRate) / 10**uint256(decimals);
        return tokens;
    }

    /// @notice collect ether to owner account
    function collect() external onlyAuthorized {
        owner.transfer(address(this).balance);
    }

    /// @notice getBalance ether
    function getBalance() public view onlyAuthorized returns (uint) {
        return address(this).balance;
    }

    /// @dev Closes the sale, issues the team tokens and burns the unsold
    function close() public onlyAuthorized beforeEnd {
        tokenSaleClosed = true;
        /// forward the raised funds to the contract creator
        owner.transfer(address(this).balance);
    }

    /// @dev Open the sale status
    function openSale() public onlyAuthorized{
        tokenSaleClosed = false;
    }

}
/******************************************/
function setRatePrices(uint256 newRate) onlyAuthorized public {
        baseRate = newRate;
    }
/// @notice Allow users to buy tokens for `newRatePrice` eth 
/// @param newRate Price the users can sell to the contract