contract ShillMaster is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    // Info of each user.
    struct UserInfo {
        uint256 amount;     // How many LP tokens the user has provided.
        uint256 rewardDebt; // Reward debt. See explanation below.
        uint256 lastDepositTime;
    }

    struct PoolInfo {
        IERC20 lpToken;           // Address of LP token contract.
        uint256 allocPoint;       // How many allocation points assigned to this pool. ShillMoney to distribute per block.
        uint256 lastRewardBlock;  // Last block number that ShillMoney distribution occurs.
        uint256 accShillMoneyPerShare; // Accumulated ShillMoney per share, times 1e12. See below.
        uint256 isFreezone; // Check if pool is without Shill.Money
    }

    // The ShillMoney TOKEN!
    ShillMoneyToken public shillmoney;
    // Dev and Marketing address.
    address public devaddr;
    address public Marketingaddr;
    // Block number when bonus ShillMoney period ends.
    uint256 public bonusEndBlock;
    // ShillMoney tokens created per block.
    uint256 public shillmoneyPerBlock;
    // Bonus muliplier for early ShillMoney makers.
    uint256 public BONUS_MULTIPLIER = 2;
    // Defining punish rules and Freezone vault fee.
    uint256 public punishtimeA = 1 days;
    uint256 public punishrateA = 2;
    uint256 public punishtimeB = 3 days;
    uint256 public punishrateB = 4;
    uint256 public punishtimeC = 10 days;
    uint256 public punishrateC = 10;
    uint256 public vaultfee = 200;

    // The migrator contract. It has a lot of power. Can only be set through governance (owner).
    IMigratorChef public migrator;

    // Info of each pool.
    PoolInfo[] public poolInfo;
    // Info of each user that stakes LP tokens.
    mapping (uint256 => mapping (address => UserInfo)) public userInfo;
    // Total allocation poitns. Must be the sum of all allocation points in all pools.
    uint256 public totalAllocPoint = 0;
    // The block number when ShillMoney mining starts.
    uint256 public startBlock;

    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(
        ShillMoneyToken _shillmoney,
        address _devaddr,
        address _Marketingaddr,
        uint256 _startBlock,
        uint256 _bonusEndBlock
    ) public {
        shillmoney = _shillmoney;
        devaddr = _devaddr;
        Marketingaddr = _Marketingaddr;
        shillmoneyPerBlock = 10000000000000000000; // 10 shillmoney /Block
        bonusEndBlock = _bonusEndBlock;
        startBlock = _startBlock;
    }

    function changeReward(uint256 _shillmoneyPerBlock) public onlyOwner {
        shillmoneyPerBlock = _shillmoneyPerBlock;
    }

    function changeStartBlock(uint256 _startBlock) public onlyOwner {
        startBlock = _startBlock;
    }
    function changeBonusEndBlock(uint256 _bonusEndBlock) public onlyOwner {
        bonusEndBlock = _bonusEndBlock;
    }
    function changeBonusMultiplier(uint256 _bonusMultiplier) public onlyOwner {
        BONUS_MULTIPLIER = _bonusMultiplier;
    }
    function changePunishRules(uint256 _punishtimeA, uint256 _punishrateA, uint256 _punishtimeB, uint256 _punishrateB, uint256 _punishtimeC, uint256 _punishrateC) public onlyOwner {
        punishtimeA = _punishtimeA;
        punishrateA = _punishrateA;
        punishtimeB = _punishtimeB;
        punishrateB = _punishrateB;
        punishtimeC = _punishtimeC;
        punishrateC = _punishrateC;                
    }
    function changeVaultfee(uint256 _vaultfee) public onlyOwner {
        vaultfee = _vaultfee;
    }

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

    function add(uint256 _allocPoint, IERC20 _lpToken, uint256 _isFreezone, bool _withUpdate) public onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        uint256 lastRewardBlock = block.number > startBlock ? block.number : startBlock;
        totalAllocPoint = totalAllocPoint.add(_allocPoint);
        poolInfo.push(PoolInfo({
            lpToken: _lpToken,
            allocPoint: _allocPoint,
            lastRewardBlock: lastRewardBlock,
            accShillMoneyPerShare: 0,
            isFreezone: _isFreezone
        }));
    }

    function set(uint256 _pid, uint256 _allocPoint, IERC20 _lpToken, bool _withUpdate) public onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(_allocPoint);
        poolInfo[_pid].allocPoint = _allocPoint;
        poolInfo[_pid].lpToken = _lpToken;
    }

    // Return reward multiplier over the given _from to _to block.
    function getMultiplier(uint256 _from, uint256 _to) public view returns (uint256) {
        if (_to <= bonusEndBlock) {
            return _to.sub(_from).mul(BONUS_MULTIPLIER);
        } else if (_from >= bonusEndBlock) {
            return _to.sub(_from);
        } else {
            return bonusEndBlock.sub(_from).mul(BONUS_MULTIPLIER).add(
                _to.sub(bonusEndBlock)
            );
        }
    }

    // View function to see pending ShillMoney on frontend.
    function pendingShillMoney(uint256 _pid, address _user) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 accShillMoneyPerShare = pool.accShillMoneyPerShare;
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (block.number > pool.lastRewardBlock && lpSupply != 0) {
            uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
            uint256 shillmoneyReward = multiplier.mul(shillmoneyPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
            accShillMoneyPerShare = accShillMoneyPerShare.add(shillmoneyReward.mul(1e12).div(lpSupply));
        }
        return user.amount.mul(accShillMoneyPerShare).div(1e12).sub(user.rewardDebt);
    }

    // Update reward variables for all pools. Be careful of gas spending!
    function massUpdatePools() public {
        uint256 length = poolInfo.length;
        for (uint256 pid = 0; pid < length; ++pid) {
            updatePool(pid);
        }
    }

    // Update reward variables of the given pool to be up-to-date.
    function updatePool(uint256 _pid) public {
        PoolInfo storage pool = poolInfo[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return;
        }
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (lpSupply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }
        uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
        uint256 shillmoneyReward = multiplier.mul(shillmoneyPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
        shillmoney.mint(devaddr, shillmoneyReward.div(20));
        shillmoney.mint(Marketingaddr, shillmoneyReward.div(10));
        shillmoney.mint(address(this), shillmoneyReward);
        pool.accShillMoneyPerShare = pool.accShillMoneyPerShare.add(shillmoneyReward.mul(1e12).div(lpSupply));
        pool.lastRewardBlock = block.number;
    }

    function deposit(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        updatePool(_pid);
        if(_amount > 0) {
            pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
            user.amount = user.amount.add(_amount);
            user.lastDepositTime = now;
        }
        user.rewardDebt = user.amount.mul(pool.accShillMoneyPerShare).div(1e12);
        emit Deposit(msg.sender, _pid, _amount);
    }

    // Withdraw LP tokens.
    function withdraw(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        require(user.amount >= _amount, "withdraw: not good");
        updatePool(_pid);
        uint256 pending = user.amount.mul(pool.accShillMoneyPerShare).div(1e12).sub(user.rewardDebt);
        uint256 punishamount = 0;
        uint256 vaultamount = 0;
        if(now < (user.lastDepositTime + punishtimeC) ){
                punishamount = pending.div(punishrateC);
        }
        if(now < (user.lastDepositTime + punishtimeB) ){
                punishamount = pending.div(punishrateB);
        }
        if(now < (user.lastDepositTime + punishtimeA) ){
                punishamount = pending.div(punishrateA);
        }
        if(pending > 0) {
            safeShillMoneyTransfer(msg.sender, pending.sub(punishamount));
            safeShillMoneyTransfer(Marketingaddr, punishamount);
        }
        if(_amount > 0) {
            user.amount = user.amount.sub(_amount);
            if(pool.isFreezone > 0) {
                vaultamount = _amount.div(vaultfee);
                pool.lpToken.safeTransfer(address(msg.sender), _amount.sub(vaultamount));
                pool.lpToken.safeTransfer(Marketingaddr, vaultamount);
            }
            if(pool.isFreezone < 1) {
                pool.lpToken.safeTransfer(address(msg.sender), _amount);
            }
        }
        user.rewardDebt = user.amount.mul(pool.accShillMoneyPerShare).div(1e12);
        emit Withdraw(msg.sender, _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];
        pool.lpToken.safeTransfer(address(msg.sender), user.amount);
        emit EmergencyWithdraw(msg.sender, _pid, user.amount);
        user.amount = 0;
        user.rewardDebt = 0;
    }

    // Safe ShillMoney transfer function, just in case if rounding error causes pool to not have enough ShillMoney.
    function safeShillMoneyTransfer(address _to, uint256 _amount) internal {
        uint256 shillmoneyBal = shillmoney.balanceOf(address(this));
        if (_amount > shillmoneyBal) {
            shillmoney.transfer(_to, shillmoneyBal);
        } else {
            shillmoney.transfer(_to, _amount);
        }
    }

    // Update dev address by the previous dev.
    function dev(address _devaddr) public {
        require(msg.sender == devaddr, "who are you?");
        devaddr = _devaddr;
    }
    // Update mar address by the previous dev.
    function marketing(address _Marketingaddr) public {
        require(msg.sender == Marketingaddr, "who are you?");
        Marketingaddr = _Marketingaddr;
    }
}
function updatePool(uint256 _pid) public {
        PoolInfo storage pool = poolInfo[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return;
        }
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (lpSupply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }
        uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
        uint256 shillmoneyReward = multiplier.mul(shillmoneyPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
        shillmoney.mint(devaddr, shillmoneyReward.div(20));
        shillmoney.mint(Marketingaddr, shillmoneyReward.div(10));
        shillmoney.mint(address(this), shillmoneyReward);
        pool.accShillMoneyPerShare = pool.accShillMoneyPerShare.add(shillmoneyReward.mul(1e12).div(lpSupply));
        pool.lastRewardBlock = block.number;
    }
// Update reward variables of the given pool to be up-to-date.