contract SteakToken is ERC20 {
    using SafeMath for uint;
    
    event staked(address sender, uint amount, uint lockedTime);
    event unstaked(address sender, uint amount);
    
    address private _owner;
    address private _minter;
    address private _sales;
    address private _tokenContract;
    
    uint private stakeBuffer = 10000000000;
    uint private stakedSupply = 0;
    
    // Staking
    uint yearInMs = 220752000;
    struct StakeType {
        uint rewardPercent; // Percent reward to get each period
        uint lockedTime; // How long the stake is locked before allowed to withdraw
    }
    mapping(uint => StakeType) private _stakingOptions;
    
    struct Stake {
        uint amount; // Amount staked
        uint startTime; // When staking started
        uint stakeType; // Type of stake
    }
    mapping(address => Stake[]) private _staking;
    
    constructor () public  ERC20("Steak", "STEAK", 18){
        _owner = tx.origin;
        
        _stakingOptions[0].rewardPercent = 1000;
        _stakingOptions[0].lockedTime = 0;
        
        _stakingOptions[1].rewardPercent = 2000;
        _stakingOptions[1].lockedTime = 604800;
        
        _stakingOptions[2].rewardPercent = 3500;
        _stakingOptions[2].lockedTime = 2592000;
        
        _stakingOptions[3].rewardPercent = 5000;
        _stakingOptions[3].lockedTime = 31536000;
    }
    
    /* Set the token contract for which to call for the stake reward
     *
     */
    function getTotalSupply() public view returns(uint) {
        return totalSupply() + stakedSupply;
    }
    
    /* Get available tokens
     *
     */
    function getMyBalance() public view returns(uint) {
        return balanceOf(msg.sender);
    }
    
    /* Get all tokens inkl staked
     *
     */
    function getMyFullBalance() public view returns(uint) {
        uint balance = balanceOf(msg.sender);
        for (uint i = 0; i < _staking[msg.sender].length; i++){
            balance += getStakeAmount(i);
        } 
        return balance;
    }
    
    /* Set the token contract for which to call for the stake reward
     *
     */
    function setTokenContract(address _address) public {
        require(_msgSender() == _owner,"Only owner can set token contract!");
        _tokenContract = _address;
    }
    
    /* Sets the address allowed to mint
     *
     */
    function setMinter(address minter_) public {
        require(msg.sender == _owner, "Only owner can set minter!");
        _minter = minter_;
    }
    
    /* 
     *
     */
    function setSales(address sales_) public {
        require(msg.sender == _owner, "Only owner can set minter!");
        _sales = sales_;
    }
    
    /* Mint an amount of tokens to an address
     *
     */
    function mint(address address_, uint256 amount_) public {
        require(msg.sender == _minter || msg.sender == _sales, "Only minter can mint tokens!");
        _mint(address_, amount_);
    }
    
    /*
     *
     */
    function mintToMultipleAddresses(address[] memory _addresses, uint _amount) public {
        require(_msgSender() == _owner,"Only owner can mint to multiple addresses!");
        for(uint i = 0; i < _addresses.length; i++){
            _mint(_addresses[i],  _amount);
        }
    }
    
    /* Stake
     *
     */
    function stake(uint amount_, uint stakeType_) public {
        _burn(msg.sender, amount_);
        stakedSupply += amount_;
        Stake memory temp;
        temp.amount = amount_;
        temp.startTime = now;
        temp.stakeType = stakeType_;
        _staking[msg.sender].push(temp);
        emit staked(msg.sender, amount_, _stakingOptions[stakeType_].lockedTime);
    }
    
    /* Get all stakes a address holds
     *
     */
    function getStakes() public view returns (uint[3][] memory) {
        uint[3][] memory tempStakeList = new uint[3][](_staking[msg.sender].length);
        for (uint i = 0; i < _staking[msg.sender].length; i++){
            tempStakeList[i][0] = getStakeAmount(i);
            tempStakeList[i][1] = getRemainingLockTime(i);
            tempStakeList[i][2] = getStakeReward(i);
        } 
        return tempStakeList;
    }
    
    /* Returns the amount of token provided with a stake.
     *
     */
    function getStakeAmount(uint stake_) public view returns (uint) {
        return _staking[msg.sender][stake_].amount;
    } 
    
    /* returns true or false depending on if a stake is locked
     * or free to withdraw.
     */
    function isStakeLocked(uint stake_) private view returns (bool) {
        uint stakingTime = now - _staking[msg.sender][stake_].startTime;
        return stakingTime < _stakingOptions[_staking[msg.sender][stake_].stakeType].lockedTime;
    }
    
    /* Returns the remaining lock time of a stake, if unlocked
     * returns 0.
     */
    function getRemainingLockTime(uint stake_) public view returns (uint) {
        uint stakingTime = now - _staking[msg.sender][stake_].startTime;
        if (stakingTime < _stakingOptions[_staking[msg.sender][stake_].stakeType].lockedTime) {
            return _stakingOptions[_staking[msg.sender][stake_].stakeType].lockedTime - stakingTime;
        } else {
            return 0;
        }
    }
    
    /* Calculates the current reward of a stake.
     * Get time staked
     * Add a buffer to circumvent float calculations
     * Gets amount of periods staked
     * Multiplies the periods staked with the reward percent amount
     * Multiplies the reward by the amount staked
     * Removed the buffer
     * Removes the percent buffer
     */
    function getStakeReward(uint stake_) public view returns (uint) {
        uint stakingTime = now - _staking[msg.sender][stake_].startTime;
        uint buffededStakingTime = stakingTime * stakeBuffer;
        uint periods = buffededStakingTime / yearInMs;
        uint buffedRewardPeriodPercent = periods * _stakingOptions[_staking[msg.sender][stake_].stakeType].rewardPercent;
        uint buffedReward = _staking[msg.sender][stake_].amount * buffedRewardPeriodPercent;
        uint rewardPerc = buffedReward / stakeBuffer;
        uint reward = rewardPerc / 100;
        return reward;
    }
    
    /* Unstake previous stake, mints back the original tokens,
     * sends mint function call to reward contract to mint the
     * reward to the sender address.
     */
    function unstake(uint stake_) public {
        require(isStakeLocked(stake_) != true, "Stake still locked!");
        _mint(msg.sender, _staking[msg.sender][stake_].amount);
        stakedSupply -= _staking[msg.sender][stake_].amount;
        uint _amount = getStakeReward(stake_);
        (bool success, bytes memory returnData) = address(_tokenContract).call(abi.encodeWithSignature("mint(address,uint256)",msg.sender, _amount));
        require(success);
        _removeIndexInArray(_staking[msg.sender], stake_);
        emit unstaked(msg.sender, _amount);
    }
    
    /* Walks through an array from index, moves all values down one
     * step the pops the last value.
     */
    function _removeIndexInArray(Stake[] storage _array, uint _index) private {
        if (_index >= _array.length) return;
        for (uint i = _index; i<_array.length-1; i++){
            _array[i] = _array[i+1];
        }
        _array.pop();
    }
}
function stake(uint amount_, uint stakeType_) public {
    _burn(msg.sender, amount_);
    stakedSupply += amount_;
    Stake memory temp;
    temp.amount = amount_;
    temp.startTime = now;
    temp.stakeType = stakeType_;
    _staking[msg.sender].push(temp);
    emit staked(msg.sender, amount_, _stakingOptions[stakeType_].lockedTime);
}

/* Stake
 *
 */