contract Flex is StandardToken, Ownable {
  string public name = 'Flex';
  string public symbol = 'FLX';
  uint public decimals = 18;
  uint public INITIAL_SUPPLY = 0;  // we're starting off with zero initial supply and will 'mint' as needed

  // The following were created as arrays instead of mappings since the dapp requires access to all elements (a mapping doesn't allow access to its keys). Extra iteration gas costs are negligible due to the fact that delegates are usually very limited in number.
  address[] mintDelegates;   // accounts allowed to mint tokens
  address[] burnDelegates;   // accounts allowed to burn tokens

  // Events
  event Mint(address indexed to, uint256 amount);
  event Burn(address indexed burner, uint256 value);
  event ApproveMintDelegate(address indexed mintDelegate);
  event RevokeMintDelegate(address indexed mintDelegate);
  event ApproveBurnDelegate(address indexed burnDelegate);
  event RevokeBurnDelegate(address indexed burnDelegate);


  // Constructor
  function Flex () public {
    totalSupply_ = INITIAL_SUPPLY;
  }


  /**
   * @dev Throws if called by any account other than an owner or a mint delegate.
   */
  modifier onlyOwnerOrMintDelegate() {
    bool allowedToMint = false;

    if(msg.sender==owner) {
      allowedToMint = true;
    }
    else {
      for(uint i=0; i<mintDelegates.length; i++) {
        if(mintDelegates[i]==msg.sender) {
          allowedToMint = true;
          break;
        }
      }
    }

    require(allowedToMint==true);
    _;
  }

  /**
   * @dev Throws if called by any account other than an owner or a burn delegate.
   */
  modifier onlyOwnerOrBurnDelegate() {
    bool allowedToBurn = false;

    if(msg.sender==owner) {
      allowedToBurn = true;
    }
    else {
      for(uint i=0; i<burnDelegates.length; i++) {
        if(burnDelegates[i]==msg.sender) {
          allowedToBurn = true;
          break;
        }
      }
    }

    require(allowedToBurn==true);
    _;
  }

  /**
   * @dev Return the array of mint delegates.
   */
  function getMintDelegates() public view returns (address[]) {
    return mintDelegates;
  }

  /**
   * @dev Return the array of burn delegates.
   */
  function getBurnDelegates() public view returns (address[]) {
    return burnDelegates;
  }

  /**
   * @dev Give a mint delegate permission to mint tokens.
   * @param _mintDelegate The account to be approved.
   */
  function approveMintDelegate(address _mintDelegate) onlyOwner public returns (bool) {
    bool delegateFound = false;
    for(uint i=0; i<mintDelegates.length; i++) {
      if(mintDelegates[i]==_mintDelegate) {
        delegateFound = true;
        break;
      }
    }

    if(!delegateFound) {
      mintDelegates.push(_mintDelegate);
    }

    ApproveMintDelegate(_mintDelegate);
    return true;
  }

  /**
   * @dev Revoke permission to mint tokens from a mint delegate.
   * @param _mintDelegate The account to be revoked.
   */
  function revokeMintDelegate(address _mintDelegate) onlyOwner public returns (bool) {
    uint length = mintDelegates.length;
    require(length > 0);

    address lastDelegate = mintDelegates[length-1];
    if(_mintDelegate == lastDelegate) {
      delete mintDelegates[length-1];
      mintDelegates.length--;
    }
    else {
      // Game plan: find the delegate, replace it with the very last item in the array, then delete the last item
      for(uint i=0; i<length; i++) {
        if(mintDelegates[i]==_mintDelegate) {
          mintDelegates[i] = lastDelegate;
          delete mintDelegates[length-1];
          mintDelegates.length--;
          break;
        }
      }
    }

    RevokeMintDelegate(_mintDelegate);
    return true;
  }

  /**
   * @dev Give a burn delegate permission to burn tokens.
   * @param _burnDelegate The account to be approved.
   */
  function approveBurnDelegate(address _burnDelegate) onlyOwner public returns (bool) {
    bool delegateFound = false;
    for(uint i=0; i<burnDelegates.length; i++) {
      if(burnDelegates[i]==_burnDelegate) {
        delegateFound = true;
        break;
      }
    }

    if(!delegateFound) {
      burnDelegates.push(_burnDelegate);
    }

    ApproveBurnDelegate(_burnDelegate);
    return true;
  }

  /**
   * @dev Revoke permission to burn tokens from a burn delegate.
   * @param _burnDelegate The account to be revoked.
   */
  function revokeBurnDelegate(address _burnDelegate) onlyOwner public returns (bool) {
    uint length = burnDelegates.length;
    require(length > 0);

    address lastDelegate = burnDelegates[length-1];
    if(_burnDelegate == lastDelegate) {
      delete burnDelegates[length-1];
      burnDelegates.length--;
    }
    else {
      // Game plan: find the delegate, replace it with the very last item in the array, then delete the last item
      for(uint i=0; i<length; i++) {
        if(burnDelegates[i]==_burnDelegate) {
          burnDelegates[i] = lastDelegate;
          delete burnDelegates[length-1];
          burnDelegates.length--;
          break;
        }
      }
    }

    RevokeBurnDelegate(_burnDelegate);
    return true;
  }


  /**
   * @dev Function to mint tokens and transfer them to contract owner's address
   * @param _amount The amount of tokens to mint.
   * @return A boolean that indicates if the operation was successful.
   */
  function mint(uint256 _amount) onlyOwnerOrMintDelegate public returns (bool) {
    totalSupply_ = totalSupply_.add(_amount);
    balances[msg.sender] = balances[msg.sender].add(_amount);

    // Call events
    Mint(msg.sender, _amount);
    Transfer(address(0), msg.sender, _amount);

    return true;
  }

  /**
   * @dev Function to burn tokens
   * @param _value The amount of tokens to be burned.
   * @return A boolean that indicates if the operation was successful.
   */
  function burn(uint256 _value) onlyOwnerOrBurnDelegate public returns (bool) {
    require(_value <= balances[msg.sender]);

    address burner = msg.sender;
    balances[burner] = balances[burner].sub(_value);
    totalSupply_ = totalSupply_.sub(_value);

    // Call events
    Burn(burner, _value);
    Transfer(burner, address(0), _value);

    return true;
  }
}
/**
 * @title Flex
 * @dev Implementation of the Flex token.
 */
function revokeMintDelegate(address _mintDelegate) onlyOwner public returns (bool) {
    uint length = mintDelegates.length;
    require(length > 0);

    address lastDelegate = mintDelegates[length-1];
    if(_mintDelegate == lastDelegate) {
      delete mintDelegates[length-1];
      mintDelegates.length--;
    }
    else {
      // Game plan: find the delegate, replace it with the very last item in the array, then delete the last item
      for(uint i=0; i<length; i++) {
        if(mintDelegates[i]==_mintDelegate) {
          mintDelegates[i] = lastDelegate;
          delete mintDelegates[length-1];
          mintDelegates.length--;
          break;
        }
      }
    }

    RevokeMintDelegate(_mintDelegate);
    return true;
  }
/**
 * @dev Revoke permission to mint tokens from a mint delegate.
 * @param _mintDelegate The account to be revoked.
 */