pragma solidity ^0.6.2;

abstract contract Context {
    function _msgSender() internal view virtual returns (address payable) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes memory) {
        this; 
        return msg.data;
    }
}

contract Ownable is Context {
    address _owner;

    event OwnershipTransferred(
        address indexed previousOwner,
        address indexed newOwner
    );

    
    constructor() internal {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    
    function owner() public view returns (address) {
        return _owner;
    }

    
    modifier onlyOwner() {
        require(_owner == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(
            newOwner != address(0),
            "Ownable: new owner is the zero address"
        );
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}


interface IERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function allowance(address owner, address spender)
        external
        view
        returns (uint256);

    function transfer(address recipient, uint256 amount)
        external
        returns (bool);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );
}

library SafeMath {
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        assert(b <= a);
        return a - b;
    }

    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        assert(c >= a);
        return c;
    }

    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        
        
        
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        
        require(b > 0, errorMessage);
        uint256 c = a / b;
        

        return c;
    }
}

interface IPresale {
    function presale() external returns (bool);

    function claimable(address _user) external returns (uint256);
}

contract PresaleDistributor is Ownable {
    using SafeMath for uint256;

    IPresale public presaleContract;
    IERC20 public token;

    mapping(address => bool) public isClaimed;

    event ClaimTokenEvent(address user, uint256 amount);

    constructor(address _presaleContract, address _token) public {
        presaleContract = IPresale(_presaleContract);
        token = IERC20(_token);
    }

    modifier isPresaleOver() {
        require(presaleContract.presale() == true, "The presale is not over");
        _;
    }

    function claimToken() external isPresaleOver {
        
        require(
            presaleContract.claimable(msg.sender) > 0,
            "No tokens to be claimed"
        );
        require(isClaimed[msg.sender] == false, "Tokens already claimed");
        uint256 amount = presaleContract.claimable(msg.sender);
        isClaimed[msg.sender] = true;
        token.transfer(msg.sender, amount);
        emit ClaimTokenEvent(msg.sender, 0);
    }

    function getEthBalance() public view returns (uint256) {
        return address(this).balance;
    }

    function getTokenBalance() public view returns (uint256) {
        return token.balanceOf(address(this));
    }

    function adminTokenTrans() external onlyOwner {
        require(getTokenBalance() > 0, "the contract has no pry tokens");
        token.transfer(msg.sender, token.balanceOf(address(this)));
    }

    function adminTransferFund(uint256 value) external onlyOwner {
        msg.sender.call{value: value}("");
    }
}