contract NatminToken is ERC20Standard, ERC223Standard, Ownable, NatminVesting, BurnToken {
    using SafeMath for uint256;

    string _name = "Natmin";
    string _symbol = "NAT";
    string _standard = "ERC20 / ERC223";
    uint256 _decimals = 18; // same value as wei
    uint256 _totalSupply;

    mapping(address => uint256) balances;
    mapping(address => mapping(address => uint256)) allowed;

    constructor(uint256 _supply) public {
        require(_supply != 0);
        _totalSupply = _supply * (10 ** 18);
        balances[contractOwner] = _totalSupply;
    }

    // Returns the _name of the token
    function name() public view returns (string) {
        return _name;        
    }

    // Returns the _symbol of the token
    function symbol() public view returns (string) {
        return _symbol;
    }

    // Returns the _standard of the token
    function standard() public view returns (string) {
        return _standard;
    }

    // Returns the _decimals of the token
    function decimals() public view returns (uint256) {
        return _decimals;
    }

    // Function to return the total supply of the token
    function totalSupply() public view returns (uint256) {
        return _totalSupply;
    }

    // Function to return the balance of a specified address
    function balanceOf(address _user) public view returns (uint256 balance){
        return balances[_user];
    }   

    // Transfer function to be compatable with ERC20 Standard
    function transfer(address _to, uint256 _value) public returns (bool success){
        bytes memory _empty;
        if(isContract(_to)){
            return transferToContract(_to, _value, _empty);
        }else{
            return transferToAddress(_to, _value, _empty);
        }
    }

    // Transfer function to be compatable with ERC223 Standard
    function transfer(address _to, uint256 _value, bytes _data) public returns (bool success) {
        if(isContract(_to)){
            return transferToContract(_to, _value, _data);
        }else{
            return transferToAddress(_to, _value, _data);
        }
    }

    // This function checks if the address is a contract or wallet
    // If the codeLength is greater than 0, it is a contract
    function isContract(address _to) internal view returns (bool) {
        uint256 _codeLength;

        assembly {
            _codeLength := extcodesize(_to)
        }

        return _codeLength > 0;
    }

    // This function to be used if the target is a contract address
    function transferToContract(address _to, uint256 _value, bytes _data) internal returns (bool) {
        require(balances[msg.sender] >= _value);
        require(vestingEnded(msg.sender));
        
        // This will override settings and allow contract owner to send to contract
        if(msg.sender != contractOwner){
            ERC223ReceivingContract _tokenReceiver = ERC223ReceivingContract(_to);
            _tokenReceiver.tokenFallback(msg.sender, _value, _data);
        }

        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);

        emit Transfer(msg.sender, _to, _value);
        emit Transfer(msg.sender, _to, _value, _data);
        return true;
    }

    // This function to be used if the target is a normal eth/wallet address 
    function transferToAddress(address _to, uint256 _value, bytes _data) internal returns (bool) {
        require(balances[msg.sender] >= _value);
        require(vestingEnded(msg.sender));

        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);

        emit Transfer(msg.sender, _to, _value);
        emit Transfer(msg.sender, _to, _value, _data);
        return true;
    }

    // ERC20 standard function
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
        require(_value <= allowed[_from][msg.sender]);
        require(_value <= balances[_from]);
        require(vestingEnded(_from));

        balances[_from] = balances[_from].sub(_value);
        balances[_to] = balances[_to].add(_value);
        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);

        emit Transfer(_from, _to, _value);

        return true;
    }

    // ERC20 standard function
    function approve(address _spender, uint256 _value) public returns (bool success){
        allowed[msg.sender][_spender] = 0;
        allowed[msg.sender][_spender] = _value;

        emit Approval(msg.sender, _spender, _value);

        return true;
    }

    // ERC20 standard function
    function allowance(address _owner, address _spender) public view returns (uint256 remaining){
        return allowed[_owner][_spender];
    }

    // Stops any attempt from sending Ether to this contract
    function () public {
        revert();
    }

    // public function to call the _burn function 
    function burn(uint256 _value) public ownerOnly {
        _burn(msg.sender, _value);
    }

    // Burn the specified amount of tokens by the owner
    function _burn(address _user, uint256 _value) internal ownerOnly {
        require(balances[_user] >= _value);

        balances[_user] = balances[_user].sub(_value);
        _totalSupply = _totalSupply.sub(_value);
        
        emit Burn(_user, _value);
        emit Transfer(_user, address(0), _value);

        bytes memory _empty;
        emit Transfer(_user, address(0), _value, _empty);
    }

    // Create a vesting entry for the specified user
    function addVesting(address _user, uint256 _amount) public ownerOnly {
        vestings[_user].amount = _amount;
        vestings[_user].endTime = now + 180 days;
    }

    // Returns the vested amount for a specified user
    function getVestedAmount(address _user) public view returns (uint256 _amount) {
        _amount = vestings[_user].amount;
        return _amount;
    }

    // Returns the vested end time for a specified user
    function getVestingEndTime(address _user) public view returns (uint256 _endTime) {
        _endTime = vestings[_user].endTime;
        return _endTime;
    }

    // Checks if the venting period is over for a specified user
    function vestingEnded(address _user) public view returns (bool) {
        if(vestings[_user].endTime <= now) {
            return true;
        }
        else {
            return false;
        }
    }

    // Manual end vested time 
    function endVesting(address _user) public ownerOnly {
        vestings[_user].endTime = now;
    }
}
//NatminToken implements the ERC20, ERC223 standard methods
function transfer(address _to, uint256 _value) public returns (bool success){
        bytes memory _empty;
        if(isContract(_to)){
            return transferToContract(_to, _value, _empty);
        }else{
            return transferToAddress(_to, _value, _empty);
        }
    }
// Transfer function to be compatable with ERC20 Standard