contract SmolTingPot is Ownable {
    using SafeMath for uint256;

    // info of each user.
    struct UserInfo {
        uint256 amount; // how many tokens the user has provided.
        uint256 rewardDebt; // Reward debt. See explanation below.
        //
        // we do some fancy math here. basically, any point in time, the amount of TINGs
        // entitled to a user but is pending to be distributed is:
        //
        //   pending reward = (user.amount * pool.accTingPerShare) - user.rewardDebt
        //
        // whenever a user deposits or withdraws tokens to a pool. Here's what happens:
        //   1. user's pending reward is minted to his/her address.
        //   2. user's `amount` gets updated.
        //   3. user's `lastUpdate` gets updated.
    }

    // info of each pool.
    struct PoolInfo {
        IERC20 token; // address of token contract.
        uint256 tingsPerDay; // the amount of TINGs per day generated for each token staked
        uint256 maxStake; // the maximum amount of tokens which can be staked in this pool
        uint256 lastUpdateTime; // last timestamp that TINGs distribution occurs.
        uint256 accTingPerShare; // accumulated TINGs per share, times 1e12. See below.
    }

    // treasury address.
    address public treasuryAddr;
    // info of each pool.
    PoolInfo[] public poolInfo;
    // info of each user that stakes LP tokens.
    mapping(uint256 => mapping(address => UserInfo)) public userInfo;
    // record whether the pair has been added.
    mapping(address => uint256) public tokenPID;

    SmolTing public Ting;
    SmolMuseum public Museum;

    event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
    event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
    event EmergencyWithdraw(
        address indexed user,
        uint256 indexed pid,
        uint256 amount
    );

    constructor(SmolTing _tingAddress, SmolMuseum _smolMuseumAddress, address _treasuryAddr) public {
        Ting = _tingAddress;
        Museum = _smolMuseumAddress;
        treasuryAddr = _treasuryAddr;
    }

    function poolLength() external view returns (uint256) {
        return poolInfo.length;
    }

    // add a new token to the pool. can only be called by the owner.
    // XXX DO NOT add the same token more than once. rewards will be messed up if you do.
    function add(IERC20 _token, uint256 _tingsPerDay, uint256 _maxStake) public onlyOwner {
        require(tokenPID[address(_token)] == 0, "smoltingpot:duplicate add.");
        require(address(_token) != address(Ting), "cannot add ting as a pool" );
        poolInfo.push(
            PoolInfo({
                token: _token,
                maxStake: _maxStake,
                tingsPerDay: _tingsPerDay,
                lastUpdateTime: block.timestamp,
                accTingPerShare: 0
            })
        );
        tokenPID[address(_token)] = poolInfo.length;
    }

    // set a new max stake. value must be greater than previous one,
    // to not give an unfair advantage to people who already staked > new max
    function setMaxStake(uint256 pid, uint256 amount) public onlyOwner {
        require(amount >= 0, "max stake cannot be negative");
        poolInfo[pid].maxStake = amount;
    }

    // set the amount of TINGs generated per day for each token staked
    function setTingsPerDay(uint256 pid, uint256 amount) public onlyOwner {
        require(amount >= 0, "hey smol tings per day cannot be negative");
        PoolInfo storage pool = poolInfo[pid];
        uint256 blockTime = block.timestamp;
        uint256 tingReward = blockTime.sub(pool.lastUpdateTime).mul(pool.tingsPerDay);

        pool.accTingPerShare = pool.accTingPerShare.add(tingReward.mul(1e12).div(86400));
        pool.lastUpdateTime = block.timestamp;
        pool.tingsPerDay = amount;
    }

    function _userPoolState(uint256 _pid, address _user) internal view returns (uint256[2] memory) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 blockTime = block.timestamp;
        uint256 accTing = pool.accTingPerShare;

        uint256 tingReward = blockTime.sub(pool.lastUpdateTime).mul(pool.tingsPerDay);
        accTing = accTing.add(tingReward.mul(1e12).div(86400));                    
    
        uint256 pending = user.amount.mul(accTing).div(1e12).sub(user.rewardDebt);
        return [pending, accTing];
    }

    // view function to see pending TINGs on frontend.
    function pendingTing(uint256 _pid, address _user) public view returns (uint256) {
        uint256 pending = _userPoolState(_pid, _user)[0];
		uint256 booster = Museum.getBoosterForUser(_user, _pid);		
        if ( booster > 0) 
		{
			pending = pending.mul(booster.add(1e5));
			pending = pending.div(1e5);
		}
        return pending;
    }


    // view function to calculate the total pending TINGs of address across all pools
    function totalPendingTing(address _user) public view returns (uint256) {
        uint256 total = 0;
        uint256 length = poolInfo.length;
        for (uint256 pid = 0; pid < length; ++pid) {
            total = total.add(pendingTing(pid, _user));
        }

        return total;
    }

    // harvest pending TINGs of a list of pools.
    // be careful of beeg gas spending if you try to harvest a big number of pools
    // might be worth it checking in the frontend for the pool IDs with pending ting for this address and only harvest those
    function rugPull(uint256[] memory _pids) public {
        for (uint i=0; i < _pids.length; i++) {
            withdraw(_pids[i], 0, msg.sender);
        }
    }


    // deposit LP tokens to pool for TING allocation.
    function deposit(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        require(_amount.add(user.amount) <= pool.maxStake, "cannot stake beyond max stake value");

        uint256 pending = _userPoolState(_pid, msg.sender)[0];
        uint256 accTing = _userPoolState(_pid, msg.sender)[1];
        user.amount = user.amount.add(_amount);
        user.rewardDebt = user.amount.mul(accTing).div(1e12);
		
		uint256 booster = Museum.getBoosterForUser(msg.sender, _pid).add(1e5);
		uint256 pendingWithBooster = pending.mul(booster).div(1e5);
        if (pendingWithBooster > 0) {
            Ting.mint(treasuryAddr, pendingWithBooster.div(40)); // 2.5% TING for the treasury 
            Ting.mint(msg.sender, pendingWithBooster);
            Ting.addClaimed(pendingWithBooster);
        }
		
        pool.token.transferFrom(address(msg.sender), address(this), _amount);
        emit Deposit(msg.sender, _pid, _amount);
    }

    // withdraw tokens from pool.
    function withdraw(uint256 _pid, uint256 _amount, address _staker) public {
        address staker = _staker;
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][staker];
        require(user.amount >= _amount, "withdraw: not good");
        require(msg.sender == staker || _amount == 0);
		
		uint256 pending = _userPoolState(_pid, staker)[0];
        uint256 accTing = _userPoolState(_pid, staker)[1];

        // in case the maxstake has been lowered and address is above maxstake, we force it to withdraw what is above current maxstake
        // user can delay his/her withdraw/harvest to take advantage of a reducing of maxstake,
        // if he/she entered the pool at maxstake before the maxstake reducing occured
        uint256 leftAfterWithdraw = user.amount.sub(_amount);
        if (leftAfterWithdraw > pool.maxStake) {
            _amount = _amount.add(leftAfterWithdraw - pool.maxStake);
        }
	
        user.amount = user.amount.sub(_amount);
        user.rewardDebt = user.amount.mul(accTing).div(1e12);
		
		uint256 booster = Museum.getBoosterForUser(staker, _pid).add(1e5);
		uint256 pendingWithBooster = pending.mul(booster).div(1e5);
        if(pendingWithBooster > 0)
        {
            Ting.mint(treasuryAddr, pendingWithBooster.div(40));
            Ting.mint(staker, pendingWithBooster);
            Ting.addClaimed(pendingWithBooster);
        }

        pool.token.transfer(address(staker), _amount);
        emit Withdraw(staker, _pid, _amount);
    }

    // withdraw without caring about rewards. EMERGENCY ONLY.
    function emergencyWithdraw(uint256 _pid) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        require(user.amount > 0, "emergncy withdrawal not good");
        uint256 _amount = user.amount;
        user.amount = 0;
        user.rewardDebt = 0;
        pool.token.transfer(address(msg.sender), _amount);
        emit EmergencyWithdraw(msg.sender, _pid, _amount);
    }


    // update dev address by the previous dev.
    function treasury(address _treasuryAddr) public {
        require(msg.sender == treasuryAddr, "must be called from current treasury address");
        treasuryAddr = _treasuryAddr;
    }

    // update Museum address if the booster logic changed.
    function updateSmolMuseumAddress(SmolMuseum _smolMuseumAddress) public onlyOwner{
        Museum = _smolMuseumAddress;
    }
}
function pendingTing(uint256 _pid, address _user) public view returns (uint256) {
    uint256 pending = _userPoolState(_pid, _user)[0];
nt256 booster = Museum.getBoosterForUser(_user, _pid);		
    if ( booster > 0) 

ending = pending.mul(booster.add(1e5));
ending = pending.div(1e5);

    return pending;
}

// view function to see pending TINGs on frontend.