contract SimpleBreeding is CKProxy {
  address payable public breeder;
  uint256 public breederReward;
  uint256 public originalBreederReward;
  uint256 public maxBreedingFee;

  event Breed(address breeder, uint256 matronId, uint256 sireId, uint256 reward);
  event MaxBreedingFeeChange(uint256 oldBreedingFee, uint256 newBreedingFee);
  event BreederRewardChange(uint256 oldBreederReward, uint256 newBreederReward);

  constructor(address payable _owner, address payable _breeder, address _kittyCoreAddress, uint256 _breederReward)
    CKProxy(_owner, _kittyCoreAddress) public {
    require(_breeder != address(0), 'Invalid breeder address');
    breeder = _breeder;
    maxBreedingFee = kittyCore.autoBirthFee();
    breederReward = _breederReward;
    originalBreederReward = _breederReward;
  }

  /**
   * Contract funds are used to cover breeding fees and pay callee
   */
  function () external payable {}

  /**
   * Owner can withdraw funds from contract
   */
  function withdraw(uint256 amount) external onlyOwner {
    owner.transfer(amount);
  }

  /**
   * Owner can adjust breedering fee if needed
   */
  function setMaxBreedingFee(
    uint256 _maxBreedingFee
  ) external onlyOwner {
    emit MaxBreedingFeeChange(maxBreedingFee, _maxBreedingFee);
    maxBreedingFee = _maxBreedingFee;
  }

   /**
   * Owner or breeder can change breeder's reward if needed.
   * Breeder can lower reward to make more attractive offer, it cannot set more than was originally agreed.
   * Owner can increase reward to motivate breeder to breed during high gas price, it cannot set less than was set by breeder.
   */
  function setBreederReward(
    uint256 _breederReward
  ) external {
    require(msg.sender == breeder || msg.sender == owner, 'Invalid sender');

    if(msg.sender == owner) {
      require(_breederReward >= originalBreederReward || _breederReward > breederReward, 'Reward value is less than required');
    } else if(msg.sender == breeder) {
      require(_breederReward <= originalBreederReward, 'Reward value is more than original');
    }

    emit BreederRewardChange(breederReward, _breederReward);
    breederReward = _breederReward;
  }

  /**
   * Breeder can call this function to breed kitties on behalf of owner
   * Owner can breed as well
   */
  function breed(uint256 _matronId, uint256 _sireId) external whenNotPaused {
    require(msg.sender == breeder || msg.sender == owner, 'Invalid sender');
    uint256 fee = kittyCore.autoBirthFee();
    require(fee <= maxBreedingFee, 'Breeding fee is too high');
    kittyCore.breedWithAuto.value(fee)(_matronId, _sireId);

    uint256 reward = 0;
    // breeder can reenter but that's OK, since breeder is payed per successful breed
    if(msg.sender == breeder) {
      reward = breederReward;
      breeder.transfer(reward);
    }

    emit Breed(msg.sender, _matronId, _sireId, reward);
  }

  function destroy() public onlyOwner {
    require(kittyCore.balanceOf(address(this)) == 0, 'Contract has tokens');
    selfdestruct(owner);
  }

  function destroyAndSend(address payable _recipient) public onlyOwner {
    require(kittyCore.balanceOf(address(this)) == 0, 'Contract has tokens');
    selfdestruct(_recipient);
  }
}
/**
 * @title SimpleBreeding
 * @dev Simple breeding contract allows dedicated breeder to breed kitties on behalf of owner, while owner retains control over funds and kitties.
 * Breeder gets reward per each successful breed. Breeder can breed when contract is not paused.
 * Owner should transfer kitties and funds to contact to breeding starts and withdraw afterwards.
 * Breeder can only breed kitties owned by contract and cannot transfer funds or kitties itself.
 */
function withdraw(uint256 amount) external onlyOwner {
  owner.transfer(amount);
}

/**
 * Owner can withdraw funds from contract
 */