contract TendiesFarm is ERC20, ERC20Detailed, ReentrancyGuard {
    using SafeMath for uint256;
    using SafeERC20 for ERC20;
    
    ERC20 public constant TEND = ERC20(0x1453Dbb8A29551ADe11D89825CA812e05317EAEB);
    ITendies public constant ITEND = ITendies(0x1453Dbb8A29551ADe11D89825CA812e05317EAEB);

    /**
     * @dev Set contract deployer as owner
     */
    constructor() public ERC20Detailed("TendiesFarm", "weebTEND", 18) {}
    
    // Mint weebTEND with TEND
    function mint(uint256 amount) public {
        grillPool();
        claimRewards();
        uint256 totalStakedTendiesBefore = getTotalStakedTendies();
        
        // Stake tendies
        TEND.safeTransferFrom(msg.sender, address(this), amount);
    
        uint256 totalStakedTendiesAfter = getTotalStakedTendies();
        
        if (totalSupply() == 0) {
            _mint(msg.sender, amount);
        } else {
            uint256 mintAmount = (totalStakedTendiesAfter.sub(totalStakedTendiesBefore))
            .mul(totalSupply())
            .div(totalStakedTendiesBefore);
        
            _mint(msg.sender, mintAmount);   
        }
    }
    
    // Burn weebTEND to get collected TEND
    function burn(uint256 amount) public nonReentrant {
        grillPool();
        claimRewards();
        // Burn weebTEND
        uint256 proRataTend = getTotalStakedTendies().mul(amount).div(totalSupply());
        _burn(msg.sender, amount);
        TEND.safeTransfer(msg.sender, proRataTend);
    }
    
    function getPricePerFullShare() public view returns (uint256 price) {
        price = getTotalStakedTendies().mul(1e18).div(totalSupply());
    }
    
    function getTotalStakedTendies() public view returns (uint256 total) {
        total = TEND.balanceOf(address(this));
    }
    
    // TEND functions
    
    function grillPool() public {
        ITEND.grillPool();
    }
    
    function claimRewards() public {
        if (ITEND.unclaimedRewards(address(this)) > 0) {
            ITEND.claimRewards();
        }
    }
}
/**
 * @title TendiesFarm
 * @author Weeb_Mcgee on twitter / YieldFarming.info
 */
function grillPool() public {
    ITEND.grillPool();
}

// TEND functions