contract SURGEStaking is SafeMath {
    uint256 public constant DECIMAL_NOMINATOR = 10**18;
    uint256 public constant rewardBreakingPoint = 30;
    uint256 public constant beforeBreakPoint = 100;    // used in 100 mulitipliction
    uint256 public constant aterBreakPoint = 150;    // used in 100 mulitipliction
    uint256 private totalSurgeBalance;
    uint256 public rewardsAllocated;
    bool public enabled = false;
    IERC20 public constant SurgeToken = IERC20(0x38B27df57d2C1b92bd88B582BbE88816354a7f62);//UPATE WITH ADDRESS FROM MAINNET
    IERC20 public constant StakingToken = IERC20(0x38B27df57d2C1b92bd88B582BbE88816354a7f62); //UPATE WITH ADDRESS FROM MAINNET
    address public owner;

    mapping(address => uint256) public stakedAmount;
    mapping(address => uint256) public lastStack;

    event Staked(address indexed user, uint256 amount);
    event Unstaked(address indexed user, uint256 amount);

    constructor() {
    owner = msg.sender;
    SurgeToken.approve(address(this),115792089237316195423570985008687907853269984665640564039457584007913129639935);
    StakingToken.approve(address(this),115792089237316195423570985008687907853269984665640564039457584007913129639935);
    }

    // To stake token user will call this method
    // user can stake only once while
    function stake(uint256 amount) external returns (bool) {
        require(enabled == true);
        require(amount <= 115792089237316195423570985008687907853269984665640564039457584007913129639935, "Overflow");
        if (stakedAmount[msg.sender] == 0) {
                bool isOk = IERC20(StakingToken).transferFrom(msg.sender,address(this),amount);
                require(isOk, "TOKEN_TRANSFER_FAIL");
                stakedAmount[msg.sender] = safeSub(amount, SurgeToken.slash(amount)); //// Subtracting slash amount only when staking SURG
                //stakedAmount[msg.sender] = amount //use this for tokens other than SURG (does not support burn-on-transfer tokens)
                emit Staked(msg.sender, amount);
                lastStack[msg.sender] = block.timestamp;
                return true;
        }
            else {
                bool isOk = IERC20(StakingToken).transferFrom(msg.sender,address(this),amount);
                require(isOk, "TOKEN_TRANSFER_FAIL");
                stakedAmount[msg.sender] = safeSub(safeAdd(stakedAmount[msg.sender], amount), SurgeToken.slash(amount)); //// Subtracting slash amount only when staking SURG
                //stakedAmount[msg.sender] = safeAdd(stakedAmount[msg.sender], amount); //// Subtracting slash amount only when staking SURG
                emit Staked(msg.sender, amount);
                lastStack[msg.sender] = block.timestamp;
                return true;
        }
    }

    // To unstake token user will call this method
    // user get daily rewards according to calulation
    //  for first 30 days we give 1.0% rewards per day
    //  after 31 day reward is 1.5% per day
    function unStake() external returns (bool) {
        require(stakedAmount[msg.sender] != 0, "ERR_NOT_STACKED");
        uint256 lastStackTime = lastStack[msg.sender];
        uint256 amount = stakedAmount[msg.sender];
        uint256 _days = safeDiv(safeSub(block.timestamp, lastStackTime), 86400);
        uint256 totalReward = 0;

        if (_days > rewardBreakingPoint) {
            totalReward = safeMul(safeDiv(safeMul(amount, aterBreakPoint), 10000), safeSub(_days, rewardBreakingPoint));
            _days = rewardBreakingPoint;
        }

        totalReward = safeAdd(totalReward, safeMul(safeDiv(safeMul(amount, beforeBreakPoint), 10000), _days));
        totalReward = safeAdd(totalReward, 1);

        SurgeToken.mint(msg.sender, totalReward);
        StakingToken.transfer(msg.sender, amount);

        emit Unstaked(msg.sender, amount);

        stakedAmount[msg.sender] = 0;
        lastStack[msg.sender] = 0;

        return true;
    }

    // user can check how many day passed untill they stake
    function checkDays(address _whom) external view returns (uint256) {
        uint256 lastStackTime = lastStack[_whom];
        uint256 _days = safeDiv(safeSub(block.timestamp, lastStackTime), 86400);
        return _days;
    }

    // user can check balance if they unstake now
    function balanceOf(address _whom) external view returns (uint256) {
        uint256 lastStackTime = lastStack[_whom];
        uint256 amount = stakedAmount[_whom];
        uint256 _days = safeDiv(safeSub(block.timestamp, lastStackTime), 86400);

        uint256 totalReward = 0;

        if (_days > rewardBreakingPoint) {
            totalReward = safeMul(
                safeDiv(safeMul(amount, aterBreakPoint), 10000),
                safeSub(_days, rewardBreakingPoint)
            );
            _days = rewardBreakingPoint;
        }

        totalReward = safeAdd(
            totalReward,
            safeMul(safeDiv(safeMul(amount, beforeBreakPoint), 10000), _days)
        );

        uint256 recivedAmount = safeAdd(amount, totalReward);
        return recivedAmount;
    }

    function enable() public {
        require(msg.sender == owner);
        enabled = true;
    }

    function disable() public {
        require(msg.sender == owner);
        enabled = false;
    }

}
function checkDays(address _whom) external view returns (uint256) {
    uint256 lastStackTime = lastStack[_whom];
    uint256 _days = safeDiv(safeSub(block.timestamp, lastStackTime), 86400);
    return _days;
}

// user can check how many day passed untill they stake