// File: contracts/Deathrow.sol



//MMMMMMMMMMMMMMMMMMMMMMMMMMMWKOdlc,... ..''..   ...,:ldkKWMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMWXOdc,.         :KXNX:           .,cokXWMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMW0d:.              oMMMWc           ..   .;oONMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMWKd,.                 oMMMWc           .:oo:.   ,o0WMMMMMMMMMMMM
//MMMMMMMMMMMMMMNk:. .'.                oMMMWc             .:l'     .;xNMMMMMMMMMM
//MMMMMMMMMMMMNk;  'xXK;                oMMMWc                 .oxc.   ,xNMMMMMMMM
//MMMMMMMMMMW0:   .xMMX;                oMMMWc                 :XMWo     ,kWMMMMMM
//MMMMMMMMMNd.    .xMMX;                oMMMWc                 ;XMMd  .,  .lXMMMMM
//MMMMMMMMK:      .xMMX;                oMMMWc                 ;XMMd. ;0x.  ,0WMMM
//MMMMMMM0,       .xMMX;                oMMMWc                 ;XMMx. ,KM0;  'OWMM
//MMMMMM0,        .xMMX;                oMMMWc                 ;XMMx. ,0MMX:  .kWM
//MMMMMK;         .xMMX;                oMMMWc                 ;XMMx. .OMMMX:  'OM
//MMMMNl          .xMMX;                oMMMWc                 ;XMMx.  cNMMMK;  :X
//MMMMk.          .xMMX;                oMMMWc                 ;XMMx.  .kMMMMk. .d
//MMMWl           .xMMX;                oMMMWc                 ;XMMx.   cNMMMN:  ;
//MMMK,           .xMMX;                oMMMWc                 ;XMMx.   '0MMMMd  .
//MMMO'           .xMMX;                oMMMWc                 ;XMMx.   .OMMMMk. .
//MMMk.           .xMMX;                oMMMWc                 ;XMMx.   .xMMMM0'
//MMMk.           .xMMX;                oMMMWc                 ;XMMx.   .xMMMMK,
//MMMx.           .xMMX;                oMMMWc                 ;XMMx.   .xMMMMK,
//MMMk.           .xMMX;                oMMMWc                 ;XMMx.   .xMMMMK,
//MMMk.           .xMMX;                oMMMWc                 ;XMMx.   .xMMMMK,
//MMMk.           .xMMX;                oMMMWc                 ;XMMx.   .kMMMMK,
//MMMO.           .xMMX;                oMMMWc                 ;XMMx.   .kMMMMK,
//MMMO.           .xMMX;                oMMMWc                 ;XMMx.   .kMMMMK,
//MMMO.           .xMMX;                oMMMWc                 ;XMMx.   .kMMMMK,
//MMMO.           .xMMX;                oMMMWc                 ;XMMx.   .kMMMMK, .
//MMMO.           .xMMX;                oMMMWc                 ;XMMx.   .OMMMMK, .
//MMMO.           .xMMX;                oMMMWl                 ;XMMx.   .kMMMMK,
//MMMO.           .xMMX;                oWMMWc                 ;XMMx.   .kMMMMX;
//MMMO.           .oKKO,                cKKKK:                 ,OKKo.   .oKKKK0,
//MMMK,             ...                  ....                   ...       .....  .
//MMMWl                                                                         .x

//   CELMATES - DEATHROW

pragma solidity ^0.8.13;

import "@openzeppelin/contracts/access/Ownable.sol";

interface ICelmates {
    function ownerOf(uint256 tokenId) external view returns (address);

    function tokenOfOwnerByIndex(address owner, uint256 index)
        external
        view
        returns (uint256);

    function balanceOf(address owner) external view returns (uint256);
}

struct Infos {
    bool status;
    address owner;
    uint256 timestamp;
}

contract Deathrow is Ownable {
    ICelmates private CELMATES;
    mapping(uint256 => Infos) public deathRowInfos;
    mapping(uint256 => uint256) public points;
    mapping(uint256 => uint256) public celmateToType;
    bool private opened;

    // ------------------ External ------------------ //

    function deathRow(uint256 _celId, bool _status) external {
        require(opened, "Deathrow is closed.");
        require(
            CELMATES.ownerOf(_celId) == msg.sender,
            "You don't own this Celmate"
        );
        Infos memory currDeathRow = deathRowInfos[_celId];
        if (_status) {
            Infos memory newDeathRow = Infos(true, msg.sender, block.timestamp);
            deathRowInfos[_celId] = newDeathRow;
        } else {
            require(deathRowInfos[_celId].status, "Not on DeathRow");
            currDeathRow.status = false;
            deathRowInfos[_celId] = currDeathRow;
        }
    }

    // ------------------ Public ------------------ //

    function getDeathrow(uint256 _celId)
        public
        view
        returns (Infos memory infos)
    {
        return deathRowInfos[_celId];
    }

    function getPoints(address _owner)
        public
        view
        returns (uint256 pointsToReturn)
    {
        uint256 totalPoints;
        uint256 balance = CELMATES.balanceOf(_owner);
        for (uint256 i = 0; i < balance; i++) {
            uint256 tokenId = CELMATES.tokenOfOwnerByIndex(_owner, i);
            Infos memory infos = deathRowInfos[tokenId];
            if (infos.owner == _owner && infos.status) {
                uint256 daysStaked = (block.timestamp - infos.timestamp) /
                    60 /
                    60 /
                    24;
                totalPoints += points[celmateToType[tokenId]] * daysStaked;
            }
        }
        return totalPoints;
    }

    // ------------------ Owner ------------------ //

    function setPoints(uint256[] memory _points) external onlyOwner {
        for (uint256 i = 0; i < _points.length; i++) {
            points[i] = _points[i];
        }
    }

    function setTypes(uint256[] memory _types) external onlyOwner {
        for (uint256 i = 0; i < _types.length; i++) {
            celmateToType[i] = _types[i];
        }
    }

    function editStake(
        uint256 _stakeId,
        address _owner,
        uint256 _timestamp,
        bool _status
    ) external onlyOwner {
        Infos memory infoToEdit = deathRowInfos[_stakeId];
        infoToEdit.owner = _owner;
        infoToEdit.timestamp = _timestamp;
        infoToEdit.status = _status;
        deathRowInfos[_stakeId] = infoToEdit;
    }

    function editTypes(uint256[] memory _ids, uint256[] memory _types)
        external
        onlyOwner
    {
        for (uint256 i = 0; i < _ids.length; i++) {
            celmateToType[_ids[i]] = _types[i];
        }
    }

    function setOpened(bool _flag) external onlyOwner {
        opened = _flag;
    }

    function setCelmates(address _celmates) external onlyOwner {
        CELMATES = ICelmates(_celmates);
    }
}

// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}