contract ArtworkBase is ArtworkAccessControl {
    /*** EVENTS ***/

    /// @dev The Birth event is fired whenever a new artwork comes into existence. This obviously
    event Birth(address owner,bytes artworkContent);
     /// @dev The CoupledArt  event is fired when two artworks successfully couple a
    event CoupledArt(address owner, uint256 topId, uint256 bottomId);
    event CoupledArtRightpiece(address owner, uint256 topId, uint256 bottomId);

    /// @dev The Grant events are fired when a control token assigns a token to the left/right/control slots
    event Grant(uint256 _tokenToGrantControlOf, uint256 _tokenToGrantControlTo);
    event GrantControl(uint256 _tokenToGrantControlOf, uint256 _tokenToGrantControlTo);


    /// @dev Transfer event as defined in current draft of ERC721. Emitted every time a artwork
    ///  ownership is assigned, including births.
    event Transfer(address from, address to, uint256 tokenId);

    /*** DATA TYPES ***/

    /// @dev The main Artwork struct. Every artwork in Pixelicu is represented by a copy
    ///  of this structure, so great care was taken to ensure that it fits neatly into
    ///  exactly two 256-bit words. Note that the order of the members in this structure
    ///  is important because of the byte-packing rules used by Ethereum.
    ///  Ref: http://solidity.readthedocs.io/en/develop/miscellaneous.html
    struct Artwork {
        address creatorId;
        uint64 birthTime;
        bytes artworkContent;
    }

    /*** CONSTANTS ***/


    // An approximation of currently how many seconds are in between blocks.
    uint256 public secondsPerBlock = 15;

    /*** STORAGE ***/

    /// @dev An array containing the Artwork struct for all Artworks in existence. The ID
    ///  of each artwork is actually an index into this array. Note that ID 0 is a negaartwork,
    ///  the unArtwork, the mythical beast that is the parent of all gen0 artworks. A bizarre
    ///  creature that is both creator and license... to itself! Has an invalid genetic code.
    ///  In other words, artwork ID 0 is invalid... ;-)
    // Artwork[] artworks; 
    Artwork[] artworks;
    mapping (bytes => bool) isTaken;


    /// @dev A mapping from artwork IDs to the address that owns them. All artworks have
    ///  some valid owner address, even gen0 artworks are created with a non-zero owner.
    mapping (uint256 => address) public artworkIndexToOwner;
    mapping (uint256 => bool) public artworkIndexToPublic;
    mapping (address=> bytes32) public addressToUsername;

    // @dev A mapping from owner address to count of tokens that address owns.
    //  Used internally inside balanceOf() to resolve ownership count.
    mapping (address => uint256) ownershipTokenCount;

    /// @dev A mapping from ArtworkIDs to an address that has been approved to call
    ///  transferFrom(). Each Artwork can only have one approved address for transfer
    ///  at any time. A zero value means no approval is outstanding.
    mapping (uint256 => address) public artworkIndexToApproved;
    
    mapping (uint256 => uint256) public artworkIndexToLeftpiece;
    mapping (uint256 => uint256) public artworkIndexToRightpiece;
    mapping (uint256 => uint256) public artworkIndexToControlPiece;


    
        /// @dev A mapping from artworkIDs to an address that has been approved to use
    ///  this Kitty for coupling . Each Artwork can only have one approved
    ///  address for coupling at any time. A zero value means no approval is outstanding.
    mapping (uint256 => address) public coupleAllowedToAddress;
    /// @dev The address of the ClockAuction contract that handles sales of Artworks. This
    ///  same contract handles both peer-to-peer sales as well as the gen0 sales which are
    ///  initiated every 15 minutes.
    SaleClockAuction public saleAuction;
    CouplingClockAuction public couplingAuction;


      function _getBytes(uint256 _artworkId) internal returns (bytes artworkContent){
           return artworks[_artworkId].artworkContent;
    }

    /// @dev Assigns ownership of a specific Artwork to an address.
    function _transfer(address _from, address _to, uint256 _tokenId) internal {
        // Since the number of artworks is capped to 2^32 we can't overflow this
        ownershipTokenCount[_to]++;
        // transfer ownership
        artworkIndexToOwner[_tokenId] = _to;
        // When creating new artworks _from is 0x0, but we can't account that address.
        if (_from != address(0)) {
            ownershipTokenCount[_from]--;
            // clear any previously approved ownership exchange
            delete artworkIndexToApproved[_tokenId];
        }
        // Emit the transfer event.
        Transfer(_from, _to, _tokenId);
    }

    /// @dev An internal method that creates a new artwork and stores it. This
    ///  method doesn't do any checking and should only be called when the
    ///  input data is known to be valid. Will generate both a Birth event
    ///  and a Transfer event.
    /// @param _creatorId The artwork ID of the creator of this artwork (zero for gen0)
    /// @param _artworkContent The artwork's genetic code.
    /// @param _owner The inital owner of this artwork, must be non-zero (except for the unArtwork, ID 0)
    function _createArtwork(
        bytes _artworkContent,
        address _owner,
        address _creatorId
    )
        internal
        returns (uint256)
    {
        // These requires are not strictly necessary, our calling code should make
        // sure that these conditions are never broken. However! _createArtwork() is already
        // an expensive call (for storage), and it doesn't hurt to be especially careful
        // to ensure our data structures are always valid.
        require(isTaken[_artworkContent] == false);
        Artwork memory _artwork = Artwork({
            birthTime: uint64(now),
            creatorId: _creatorId,
            artworkContent: _artworkContent
        });
        
        uint256 newArtId = artworks.push(_artwork) - 1;
        isTaken[_artworkContent] = true;

        // It's probably never going to happen, 4 ga-billion artworks is A LOT, but
        // let's just be 100% sure we never let this happen.
        require(newArtId == uint256(newArtId));

        // emit the birth event
        Birth(
            _artwork.creatorId,
            _artworkContent
        );
        artworkIndexToLeftpiece[newArtId] = 0;
        artworkIndexToRightpiece[newArtId] = 0;

        // This will assign ownership, and also emit the Transfer event as
        // per ERC721 draft
        _transfer(0, _owner, newArtId);

        return newArtId;
    }
    
     function _assignLeftpiece(uint256 _topId, uint256 _leftId) internal {


        // Mark the topPiece as having a tailpiece, keeping track of which one
        
        artworkIndexToLeftpiece[_topId] = _leftId;
        // Emit the coupling event.
        CoupledArt(artworkIndexToOwner[_topId], _topId, _leftId);
    }

     function _assignControlPiece(uint256 _tokenId, uint256 _controlId) internal {
    
        artworkIndexToControlPiece[_tokenId] = _controlId;
    }

    function _assignRightpiece(uint256 _topId, uint256 _rightId) internal {
         Artwork storage topArtwork = artworks[_topId];

        // Mark the topPiece as having a tailpiece, keeping track of which one
        //topArtwork.rightpieceId = _rightId;
        
        artworkIndexToRightpiece[_topId] = _rightId;
        // Emit the coupling event.
        CoupledArtRightpiece(artworkIndexToOwner[_topId], _topId, _rightId);
    }
    
     function _owns(address _claimant, uint256 _tokenId) internal view returns (bool) {
        return artworkIndexToOwner[_tokenId] == _claimant;
    }
      // External function intended for subcontracts to be able to attach rewards to tokens
     /// @dev Checks if a given address is the current owner of a particular Artwork.
    /// @param _claimant the owned token of the msg sender they are using to validate their claim.
    /// @param _tokenId the token that the caller is indicating as being controlled
    /// @param _controlToken tthe token that is being used to control the _tokenId;
      function _ownsOrControls(address _claimant, uint256 _tokenId, uint256 _controlToken) internal view returns (bool) {
        return artworkIndexToOwner[_tokenId] == _claimant || artworkIndexToControlPiece[_tokenId] == _controlToken;
    }

    function _shareControl(uint256 _masterToken, uint256 _childToken) internal view returns (bool){
        return (artworkIndexToControlPiece[_childToken] == _masterToken || artworkIndexToControlPiece[_childToken] == artworkIndexToControlPiece[_masterToken]);
    }
    
     function _isCouplingPermitted(uint256 _topId, uint256 _partnerId) internal view returns (bool) {
        address topOwner = artworkIndexToOwner[_topId];
        address bottomOwner = artworkIndexToOwner[_partnerId];

        // Coupling is okay if they have same owner, or if the top's owner was given
        // permission to pair with this artwork.
        return (topOwner == bottomOwner || coupleAllowedToAddress[_partnerId] == topOwner || artworkIndexToPublic[_partnerId] == true);
    }
    
        function approveCoupling(address _addr, uint256 _partnerId)
        external payable
        whenNotPaused
    {
        require(_owns(msg.sender, _partnerId));
        coupleAllowedToAddress[_partnerId] = _addr;
    }

        function setUsername(bytes32 _username)
        external payable
        whenNotPaused
    {
        addressToUsername[msg.sender] = _username;
    }


      function setTokenAsPublic(uint256 _tokenId)
        external payable
        whenNotPaused
    {
        require(_owns(msg.sender, _tokenId));
        artworkIndexToPublic[_tokenId] = true;
    }

      function setTokenAsPrivate(uint256 _tokenId)
        external payable
        whenNotPaused
    {
        require(_owns(msg.sender, _tokenId));
        artworkIndexToPublic[_tokenId] = false;
    }
    
           function removeCouplingPermission(uint256 _partnerId)
        external payable
        whenNotPaused
    {
        require(_owns(msg.sender, _partnerId));
        coupleAllowedToAddress[_partnerId] = msg.sender;
    }

}
/// @title Base contract for Pixelicu. Holds all common structs, events and base variables.
/// @author Axiom Zen (https://www.axiomzen.co)
/// @dev See the ArtworkCore contract documentation to understand how the various contract facets are arranged.
function _ownsOrControls(address _claimant, uint256 _tokenId, uint256 _controlToken) internal view returns (bool) {
  return artworkIndexToOwner[_tokenId] == _claimant || artworkIndexToControlPiece[_tokenId] == _controlToken;


/// @dev Checks if a given address is the current owner of a particular Artwork.
/// @param _claimant the owned token of the msg sender they are using to validate their claim.
/// @param _tokenId the token that the caller is indicating as being controlled
/// @param _controlToken tthe token that is being used to control the _tokenId;