contract MiningSupplier {
    using SafeMath for uint256;

    uint256 public constant secondsPerYear = 1 years * 1 seconds;
    uint256 public constant secondsPerDay = 1 days * 1 seconds;

    // @dev Number of blocks per year
    function _getBlocksPerYear(
        uint256 _secondsPerBlock
    ) public pure returns(uint256) {
        return secondsPerYear.div(_secondsPerBlock);
    }

    // @dev 0-based block number index
    //      First block number index of every years is 0
    function _getBlockIndexAtYear(
        uint256 _initialBlockNumber,
        uint256 _currentBlockNumber,
        uint256 _secondsPerBlock
    ) public pure returns(uint256) {
        //require(_currentBlockNumber >= _initialBlockNumber, "current is large than or equal to initial");
        require(_currentBlockNumber >= _initialBlockNumber);
        uint256 _blockIndex = _currentBlockNumber.sub(_initialBlockNumber);
        uint256 _blocksPerYear = _getBlocksPerYear(_secondsPerBlock);
        return _blockIndex.sub(_blockIndex.div(_blocksPerYear).mul(_blocksPerYear));
    }

    // @dev Map block number to block index.
    //      First block is number 0.
    function _getBlockIndex(
        uint256 _initialBlockNumber,
        uint256 _currentBlockNumber
    ) public pure returns(uint256) {
        //require(_currentBlockNumber >= _initialBlockNumber, "current is large than or equal to initial");
        require(_currentBlockNumber >= _initialBlockNumber);
        return _currentBlockNumber.sub(_initialBlockNumber);
    }

    // @dev Map block number to year index.
    //      First (blocksPerYear - 1) blocks are number 0.
    function _getYearIndex(
        uint256 _secondsPerBlock,
        uint256 _initialBlockNumber,
        uint256 _currentBlockNumber
    ) public pure returns(uint256) {
        uint256 _blockIndex =  _getBlockIndex(_initialBlockNumber, _currentBlockNumber);
        uint256 _blocksPerYear = _getBlocksPerYear(_secondsPerBlock);
        return _blockIndex.div(_blocksPerYear);
    }

    // @dev
    function _getWaitingBlocks(
        uint256 _secondsPerBlock
    ) public pure returns(uint256) {
        return secondsPerDay.div(_secondsPerBlock);
    }

    function _getWeightUntil(
        uint256 _totalWeight,
        uint256 _yearIndex
    ) public pure returns(uint256) {
        uint256 _sum = 0;
        for(uint256 i = 0; i < _yearIndex; i++) {
            _sum = _sum.add(_totalWeight / (2 ** (i + 1)));
        }
        return _sum;
    }

    function _estimateSupply(
        uint256 _secondsPerBlock,
        uint256 _initialBlockNumber,
        uint256 _currentBlockNumber,
        uint256 _totalWeight
    ) public pure returns(uint256){
        uint256 _yearIndex = _getYearIndex(_secondsPerBlock, _initialBlockNumber, _currentBlockNumber); // 0-based
        uint256 _blockIndex = _getBlockIndexAtYear(_initialBlockNumber, _currentBlockNumber, _secondsPerBlock) + 1;
        uint256 _numerator = _totalWeight.mul(_secondsPerBlock).mul(_blockIndex);
        uint256 _yearFactor = 2 ** (_yearIndex + 1);
        uint256 _denominator =  _yearFactor.mul(secondsPerYear);
        uint256 _supply = _numerator.div(_denominator).add(_getWeightUntil(_totalWeight, _yearIndex));
        return _supply; // mg
    }

    function _estimateWeight(
        uint256 _secondsPerBlock,
        uint256 _initialBlockNumber,
        uint256 _currentBlockNumber,
        uint256 _totalWeight,
        uint256 _currentWeight
    ) public pure returns(uint256) {
        uint256 _supply = _estimateSupply(
            _secondsPerBlock,
            _initialBlockNumber,
            _currentBlockNumber,
            _totalWeight
        );
        uint256 _yearIndex = _getYearIndex(
            _secondsPerBlock,
            _initialBlockNumber,
            _currentBlockNumber
        ); // 0-based
        uint256 _yearFactor = 2 ** _yearIndex;
        uint256 _defaultWeight = 10000; // mg

        if(_currentWeight > _supply) {
            // (_supply / _currentWeight) * _defaultWeight / _yearFactor
            return _supply.mul(_defaultWeight).div(_currentWeight).div(_yearFactor);
        } else {
            // _defaultWeight / _yearFactor
            return _defaultWeight.div(_yearFactor);
        }
    }

    function _updateNeeded(
        uint256 _secondsPerBlock,
        uint256 _currentBlockNumber,
        uint256 _blockNumberUpdated
    ) public pure returns(bool) {
        if (_blockNumberUpdated == 0) {
            return true;
        }
        uint256 _waitingBlocks = _getWaitingBlocks(_secondsPerBlock);
        return _currentBlockNumber >= _blockNumberUpdated + _waitingBlocks;
    }
}
// @title MiningSupplier
// @author Takayuki Jimba
function _getBlocksPerYear(
    uint256 _secondsPerBlock
) public pure returns(uint256) {
    return secondsPerYear.div(_secondsPerBlock);
}

// @dev Number of blocks per year