contract AuctionPotato {
    using SafeMath for uint256; 
    // static
    address public owner;
    uint public startTime;
    
    string name;
    
    // start auction manually at given time
    bool started;

    // pototo
    uint public potato;
    uint oldPotato;
    uint oldHighestBindingBid;
    
    // transfer ownership
    address creatureOwner;
    
    event CreatureOwnershipTransferred(address indexed _from, address indexed _to);
    
    
   
    
    uint public highestBindingBid;
    address public highestBidder;
    
    // used to immidiately block placeBids
    bool blockerPay;
    bool blockerWithdraw;
    
    mapping(address => uint256) public fundsByBidder;
  

    event LogBid(address bidder, address highestBidder, uint oldHighestBindingBid, uint highestBindingBid);
    event LogWithdrawal(address withdrawer, address withdrawalAccount, uint amount);
    
    
    
    // initial settings on contract creation
    constructor() public {
    
        
        blockerWithdraw = false;
        blockerPay = false;
        
        owner = msg.sender;
        creatureOwner = owner;
        
        // 1 ETH starting price
        highestBindingBid = 1000000000000000000;
        potato = 0;
        
        started = false;
        
        name = "Aetherian";
        
    }

    function getHighestBid() internal
        constant
        returns (uint)
    {
        return fundsByBidder[highestBidder];
    }
    
    
    
    function auctionName() public view returns (string _name) {
        return name;
    }
    
    // calculates the next bid amount so that you can have a one-click buy button
    function nextBid() public view returns (uint _nextBid) {
        return highestBindingBid.add(potato);
    }
    
    
    // command to start the auction
    function startAuction() public onlyOwner returns (bool success){
        require(started == false);
        
        started = true;
        startTime = now;
        
        
        return true;
        
    }
    
    function isStarted() public view returns (bool success) {
        return started;
    }

    function placeBid() public
        payable
        onlyAfterStart
        onlyNotOwner
        returns (bool success)
    {   
        // we are only allowing to increase in bidIncrements to make for true hot potato style
        // while still allowing overbid to happen in case some parties are trying to 
        require(msg.value >= highestBindingBid.add(potato));
        require(msg.sender != highestBidder);
        require(started == true);
        require(blockerPay == false);
        blockerPay = true;

        // if someone overbids, return their
        if (msg.value > highestBindingBid.add(potato))
        {
            uint overbid = msg.value - highestBindingBid.add(potato);
            msg.sender.transfer(overbid);
        }
        
        // calculate the user's total bid based on the current amount they've sent to the contract
        // plus whatever has been sent with this transaction

        
        
        oldHighestBindingBid = highestBindingBid;
        
        // set new highest bidder
        highestBidder = msg.sender;
        highestBindingBid = highestBindingBid.add(potato);
        
        fundsByBidder[msg.sender] = fundsByBidder[msg.sender].add(highestBindingBid);
        
        
        oldPotato = potato;
        
        uint potatoShare;
        
        potatoShare = potato.div(2);
        potato = highestBindingBid.mul(5).div(10);
            
        // special case at start of auction
        if (creatureOwner == owner) {
            fundsByBidder[owner] = fundsByBidder[owner].add(highestBindingBid);
        }
        else {
            fundsByBidder[owner] = fundsByBidder[owner].add(potatoShare);
            
            fundsByBidder[creatureOwner] = fundsByBidder[creatureOwner].add(potatoShare);
        }
        
        
        
        
        emit LogBid(msg.sender, highestBidder, oldHighestBindingBid, highestBindingBid);
        
        
        emit CreatureOwnershipTransferred(creatureOwner, msg.sender);
        creatureOwner = msg.sender;
        
        
        blockerPay = false;
        return true;
    }

    

    function withdraw() public
    // can withdraw once overbid
        returns (bool success)
    {
        require(blockerWithdraw == false);
        blockerWithdraw = true;
        
        address withdrawalAccount;
        uint withdrawalAmount;
        
        if (msg.sender == owner) {
            withdrawalAccount = owner;
            withdrawalAmount = fundsByBidder[withdrawalAccount];
            
            
            // set funds to 0
            fundsByBidder[withdrawalAccount] = 0;
        }
       
        // overbid people can withdraw their bid + profit
        // exclude owner because he is set above
        if (msg.sender != highestBidder && msg.sender != owner) {
            withdrawalAccount = msg.sender;
            withdrawalAmount = fundsByBidder[withdrawalAccount];
            fundsByBidder[withdrawalAccount] = 0;
        }
        
        if (withdrawalAmount == 0) revert();
    
        // send the funds
        msg.sender.transfer(withdrawalAmount);

        emit LogWithdrawal(msg.sender, withdrawalAccount, withdrawalAmount);
        blockerWithdraw = false;
        return true;
    }
    
    // amount owner can withdraw
    // that way you can easily compare the contract balance with your amount
    // if there is more in the contract than your balance someone didn't withdraw
    // let them know that :)
    function ownerCanWithdraw() public view returns (uint amount) {
        return fundsByBidder[owner];
    }
    
    // just in case the contract is bust and can't pay
    // should never be needed but who knows
    function fuelContract() public onlyOwner payable {
        
    }
    
    function balance() public view returns (uint _balance) {
        return address(this).balance;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    modifier onlyNotOwner {
        require(msg.sender != owner);
        _;
    }

    modifier onlyAfterStart {
        if (now < startTime) revert();
        _;
    }

    
    
    
    // who owns the creature (not necessarily auction winner)
    function queryCreatureOwner() public view returns (address _creatureOwner) {
        return creatureOwner;
    }
    
    
    
   
    
}
function fuelContract() public onlyOwner payable {
        
    }
// just in case the contract is bust and can't pay
// should never be needed but who knows