contract ShareTokenSaleData is BaseContractData, IShareTokenSaleData{
    using SafeMath for uint;
    modifier canTransfer{
        require(_shareholderCanTransfer,"Can not transfer BNU now");
        _;
    }

    uint internal _tokenDecimalValue = 1000000000000000000;

    /**
    * @dev Shareholder list
    */
    address[] internal _shareholders;

    /**
    * @dev Stores shareholders' balances for each round
    * Mapping: Round => (shareholder address => shareholder balance)
    */
    mapping(uint => mapping(address => uint)) internal _shareholderBalances;

    /**
    * @dev Stores end times of private and seed round 
    */
    mapping(uint => uint) internal _tokenSaleEndTimes;

    /**
    @dev Variable to check whether sahreholders can transfer BNU to others
     */
    bool internal _shareholderCanTransfer;

    /**
     * @dev Stores mapping start time of each token sale round
     */ 
    mapping(uint => uint) internal _tokenSaleStartTimes;

    /**
    * @dev Release percentage configuration 
    * Mapping: round => array of percent
    */
    mapping(uint => uint[]) internal _releaseDurations;

    /**
    * @dev Release percentage configuration 
    * Mapping: round => array of percent
    */
    mapping(uint => uint[]) internal _releasePercents;

    mapping(uint => uint) internal _releasedCounts;

    mapping(uint => mapping(address => uint)) internal _releaseHolderCounts;

    /**
    * @dev Constructor
    * Implementations
    *   1. Set current round and _currentPurchase property
    *   2. Set tokenRemains for each round, initially is total token to sell
    *   3. Set token sale round durations
    */
    constructor(){
        _releaseDurations[0] = [180 days, 270 days, 360 days, 450 days, 540 days, 630 days, 720 days];
        _releasePercents[0] = [250, 125, 125, 125, 125, 125, 125];

        _releaseDurations[1] = [180 days, 270 days, 360 days, 450 days, 540 days, 630 days, 720 days];
        _releasePercents[1] = [250, 125, 125, 125, 125, 125, 125];

        _shareholderCanTransfer = true;
    }

    /**
    @dev Update all related data for purchasing
    *
    * Implementations:
    *   1. Create purchase history
    *   2. Decrease remained token for `round`
    *   3. Increase shareholder's balance
     */
    function updatePurchaseData(address account, uint round, uint tokenAmount) external override onlyTokenSaleContract returns(bool){
        require(round == 0 || round == 1, "Round is invalid");

        //Add share holder balance for round
        _increaseShareholderBalance(account, tokenAmount, round);

        //Save new shareholder if not existed
        require(_saveShareholder(account),"ShareTokenSaleData.updatePurchaseData: Can not create new shareholder");

        return true;
    }

    /**
    * @dev Set contract properties to end current round
    * Implementations
    *   1. Update current round
    *   2. Update canPurchase = true
    *   3. Save startTime for new round
     */
    function end(uint round, uint time) external override onlyTokenSaleContract contractActive returns(bool){
        _setTokenSaleEndTime(round, time);
        return true;
    }

    /**
    * @dev Update released data when releasing
    */
    function updateReleasedData(uint round) external override onlyTokenSaleContract contractActive returns(bool){
        //Increase times of release by 1
        _releasedCounts[round] = _releasedCounts[round].add(1);
        
        return true;
    }

    /**
    * @dev Update released data when releasing
    */
    function updateWithdrawData(address account, uint round) external override onlyTokenSaleContract contractActive returns(bool){
        //Increase times for holders for this round by 1
        _releaseHolderCounts[round][account] = _releaseHolderCounts[round][account].add(1);
        
        return true;
    }

    /**
    * @dev Get the next time can be released for this round
    */
    function getNextReleaseTime(uint round) external override view returns(uint){
        uint tokenSaleEndTime = _tokenSaleEndTimes[round];
        require(tokenSaleEndTime > 0,"Round is not ended");

        return tokenSaleEndTime.add(_releaseDurations[round][_getReleasedCountByRound(round)]);
    }

    /**
    * @dev Get release count by round
    */
    function getReleasedCountByRound(uint round) external override view returns(uint){
        return _getReleasedCountByRound(round);
    }

    /**
    * @dev Get release count by round and holder
    */
    function getReleasedCountByRoundAndHolder(uint round, address account) external override view returns(uint){
        return _releaseHolderCounts[round][account];
    }

    /**
    * @dev Get release count by round
    */
    function getReleasedPercentByRound(uint round) external override view returns(uint){
        return _releasePercents[round][_getReleasedCountByRound(round)];
    }

    /**
    * @dev Get total count can be released by round `round`
    */
    function getTotalCanReleaseCountByRound(uint round) external override view returns(uint){
        return _releaseDurations[round].length;
    }

    /**
    * @dev Get share holder's token balance
    */
    function getShareholderBalance(address account, uint round) external view override returns(uint){
        return _shareholderBalances[round][account];
    }

    /**
    * @dev Get shareholder list 
    */
    function getShareholders() external view override returns(address[] memory){
        return _shareholders;
    }

    /**TRANSFER FUNCTION */

    /**
    * @dev Get the state to check whether shareholders can transfer BNU or not
    */
    function getShareholderCanTransfer() external override view returns(bool){
        return _shareholderCanTransfer;
    }

    /**
    * @dev Set the state to check whether shareholders can transfer BNU or not
    */
    function setShareholderCanTransfer(bool value) external override onlyTokenSaleContract contractActive{
        _shareholderCanTransfer =  value;
    }

    /**
    @dev Update all transfer data when a transfer request is maked
    *
    * Implementations
    *   1. Add transfer history
    *   2. Update transfer balance
    *   3. Update share holder balance
     */
    function updateShareholderTransferData(address from, address to, uint amount, uint round) external override onlyTokenSaleContract canTransfer returns(bool){
        require(round == 0 || round == 1, "ShareTokenSaleData.addTransferHistoryAndBalance: Round is invalid");

        //Update shareholder balance
        _decreaseShareholderBalance(from, amount, round);
        _increaseShareholderBalance(to, amount, round);

        //Save new shareholder if not existed
        require(_saveShareholder(to),"ShareTokenSaleData.updateShareholderTransferData: Can not save new shareholder");

        return true;
    }

    /**
    * @dev Get end time of `round` 
    */
    function getTokenSaleEndTime(uint round) external view override returns(uint){
        return _tokenSaleEndTimes[round];
    }

    /**
    * @dev Increase shareholder balance with `amount` of BNU for round `round`
    */
    function _increaseShareholderBalance(address account, uint amount, uint round) internal{
        _shareholderBalances[round][account] = _shareholderBalances[round][account].add(amount);
    }

    /**
    * @dev Decrease shareholder balance with `amount` of BNU for round `round`
    */
    function _decreaseShareholderBalance(address account, uint amount, uint round) internal{
        _shareholderBalances[round][account] = _shareholderBalances[round][account].sub(amount);
    }

    function _getReleasedCountByRound(uint round) internal view returns(uint){
        return _releasedCounts[round];
    }

    /**
    * @dev Save new shareholder to _shareholders
    *
    * Requirements: `account` should be not address zero
    */
    function _saveShareholder(address account) internal returns(bool){
        //Requirements
        require(account != address(0),"Shareholder is address zero");
        for(uint index = 0; index < _shareholders.length; index++){
            if(_shareholders[index] == account)
                return true;
        }
        _shareholders.push(account);

        return true;
    }

    /**
    * @dev Set end time of `round` 
    */
    function _setTokenSaleEndTime(uint round, uint time) internal returns(bool){
        _tokenSaleEndTimes[round] = time;
        return true;
    }
}
function getReleasedPercentByRound(uint round) external override view returns(uint){
        return _releasePercents[round][_getReleasedCountByRound(round)];
    }
/**
 * @dev Get release count by round
 */