// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

// File: @openzeppelin/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 tokenId) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

// File: @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



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

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

// File: contracts/SFAStaking.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";

contract SFAStaking is IERC721Receiver {
    
    struct StakingData {
        uint256 tokenId;
        uint256 timestamp;
    }

    uint256 public maxSolarPass;
    uint256 public maxSolarHead;
    address public solarPass;
    uint8 public solarPassPayout;
    address public solarHead;
    uint8 public solarHeadPayout;
    address public sfaToken;
    uint32 public payoutPeriod;
    mapping(address => uint256) public solarPassStaked;
    mapping(address => uint256) public solarHeadStaked;
    mapping(address => StakingData[5]) public solarPassStakedData;
    mapping(address => StakingData[25]) public solarHeadStakedData;

    event SolarHeadStaked(address user, uint256 amount);
    event SolarPassStaked(address user, uint256 amount);
    event RewardsClaimed(address user, uint256 amount);
    event WithdrawalAll(address user);
    event WithdrawalSolarHead(address user);
    event WithdrawalSolarPass(address user);

    constructor(
        uint256 _maxSolarPass,
        uint256 _maxSolarHead,
        address _solarPass,
        uint8 _solarPassPayout,
        address _solarHead,
        uint8 _solarHeadPayout,
        address _sfaToken,
        uint32 _payoutPeriod
    ) {
        maxSolarPass = _maxSolarPass;
        maxSolarHead = _maxSolarHead;
        solarPass = _solarPass;
        solarPassPayout = _solarPassPayout;
        solarHead = _solarHead;
        solarHeadPayout = _solarHeadPayout;
        sfaToken = _sfaToken;
        payoutPeriod = _payoutPeriod;
    }


    /**
     * @notice views the staking state of a user
     * @param _user address of user whose state gets returned
     * @return uint256 amount of solar passes staked
     * @return uint256 amount of solar heads staked
     */
    function getStakedAmounts(address _user) external view returns(uint256, uint256) {
        uint256 userSolarPassesStaked = solarPassStaked[_user];
        uint256 userSolarHeadsStaked = solarHeadStaked[_user];
        return (userSolarPassesStaked, userSolarHeadsStaked);
    }


    /**
     * @notice stakes one solar head for msg.sender
     * @param _tokenId tokenId of solar head to be staked
     */
    function stakeSolarHead(uint256 _tokenId) external {
        uint256 amountStaked = solarHeadStaked[msg.sender];
        require(amountStaked + 1 <= maxSolarHead, "SFA Staking: staked amount exceeds limit");
        IERC721(solarHead).safeTransferFrom(msg.sender, address(this), _tokenId);
        solarHeadStaked[msg.sender] += 1;
        solarHeadStakedData[msg.sender][amountStaked] = StakingData(_tokenId, block.timestamp);
        emit SolarHeadStaked(msg.sender, 1);
    }

    /**
     * @notice stakes multiple solar heads at one time for msg.sender
     * @param _amount the amount of solar heads to stake
     * @param _tokenIds the tokenIds of the solar heads being staked
     */
    function batchStakeSolarHead(uint256 _amount, uint256[] calldata _tokenIds) external {
        uint256 amountStaked = solarHeadStaked[msg.sender];
        require(amountStaked + _amount <= maxSolarHead, "SFA Staking: staked amount exceeds limit");
        
        address _solarHead = solarHead;

        uint256 i = 0;
        while(i < _amount) {
            IERC721(_solarHead).safeTransferFrom(msg.sender, address(this), _tokenIds[i]);
            solarHeadStaked[msg.sender] += 1;
            solarHeadStakedData[msg.sender][amountStaked + i] = StakingData(_tokenIds[i], block.timestamp);
            unchecked {
                ++i;
            }
        }
        emit SolarHeadStaked(msg.sender, _amount);
    }


    /**
     * @notice stakes one solar pass for msg.sender
     * @param _tokenId tokenId of solar pass to be staked
     */
    function stakeSolarPass(uint256 _tokenId) external {
        uint256 amountStaked = solarPassStaked[msg.sender];
        require(amountStaked + 1 <= maxSolarPass, "SFA Staking: staked amount exceeds limit");
        IERC721(solarPass).safeTransferFrom(msg.sender, address(this), _tokenId);
        solarPassStaked[msg.sender] += 1;
        solarPassStakedData[msg.sender][amountStaked] = StakingData(_tokenId, block.timestamp);
        emit SolarPassStaked(msg.sender, 1);
    }

     /**
     * @notice stakes multiple solar passes at one time for msg.sender
     * @param _amount the amount of solar passes to stake
     * @param _tokenIds the tokenIds of the solar passes being staked
     */
    function batchStakeSolarPass(uint256 _amount, uint256[] calldata _tokenIds) external {
        uint256 amountStaked = solarPassStaked[msg.sender];
        require(amountStaked + _amount <= maxSolarPass, "SFA Staking: staked amount exceeds limit");
        
        address _solarPass = solarPass;

        uint256 i = 0;
        while(i < _amount) {
            IERC721(_solarPass).safeTransferFrom(msg.sender, address(this), _tokenIds[i]);
            solarPassStaked[msg.sender] += 1;
            solarPassStakedData[msg.sender][amountStaked + i] = StakingData(_tokenIds[i], block.timestamp);
            unchecked {
                ++i;
            }
        }
        emit SolarPassStaked(msg.sender, _amount);
    }

    /**
     * @notice allows msg.sender to claim SFA Token rewards, 
     * without having to withdraw their stakes NFTs
     */
    function claimRewards() external {
        uint256 solarHeadsStaked = solarHeadStaked[msg.sender];
        uint256 solarPassesStaked = solarPassStaked[msg.sender];

        uint256 tokensEarned;
        uint256 currTime = block.timestamp;
        uint32 _payoutPeriod = payoutPeriod;
        uint256 stakedTime;
        uint256 duration;

        uint256 i;
        while(i < solarHeadsStaked) {
            stakedTime = solarHeadStakedData[msg.sender][i].timestamp;
            duration = currTime - stakedTime;
            tokensEarned += (duration / _payoutPeriod) * 1 ether;
            solarHeadStakedData[msg.sender][i].timestamp = block.timestamp;
            unchecked {
                ++i;
            }
        }

        i = 0;
        while(i < solarPassesStaked) {
            stakedTime = solarPassStakedData[msg.sender][i].timestamp;
            duration = currTime - stakedTime;
            tokensEarned += (duration / _payoutPeriod) * 10 ether;
            solarPassStakedData[msg.sender][i].timestamp = block.timestamp;
            unchecked {
                ++i;
            }
        }

        IERC20(sfaToken).transfer(msg.sender, tokensEarned);
        emit RewardsClaimed(msg.sender, tokensEarned);
    }


     /**
     * @notice withdraws users staked NFTs. Additonally, claims
     * all SFA Token rewards for the user.
     */
    function withdraw() external {

        address _solarHead = solarHead;
        address _solarPass = solarPass;

        uint256 solarHeadsStaked = solarHeadStaked[msg.sender];
        uint256 solarPassesStaked = solarPassStaked[msg.sender];

        solarHeadStaked[msg.sender] = 0;
        solarPassStaked[msg.sender] = 0;

        uint256 tokensEarned;
        uint256 currTime = block.timestamp;
        uint32 _payoutPeriod = payoutPeriod;
        uint256 stakedTime;
        uint256 duration;

        uint256 i = 0;

        while(i < solarHeadsStaked) {
            stakedTime = solarHeadStakedData[msg.sender][i].timestamp;
            duration = currTime - stakedTime;
            tokensEarned += (duration / _payoutPeriod) * 1 ether;
            solarHeadStakedData[msg.sender][i].timestamp = 0;
            IERC721(_solarHead).safeTransferFrom(address(this), msg.sender, solarHeadStakedData[msg.sender][i].tokenId);
            unchecked {
                ++i;
            }
        }

        i = 0;
        while(i < solarPassesStaked) {
            stakedTime = solarPassStakedData[msg.sender][i].timestamp;
            duration = currTime - stakedTime;
            tokensEarned += (duration / _payoutPeriod) * 10 ether;
            solarPassStakedData[msg.sender][i].timestamp = 0;
            IERC721(_solarPass).safeTransferFrom(address(this), msg.sender, solarPassStakedData[msg.sender][i].tokenId);
            unchecked {
                ++i;
            }
        }

        IERC20(sfaToken).transfer(msg.sender, tokensEarned);
        emit WithdrawalAll(msg.sender);
    }

     /**
     * @notice withdraws users staked SolarHead. Additonally, claims
     * all SFA Token rewards for the user.
     */
    function withdrawSolarHead() external {

        address _solarHead = solarHead;

        uint256 solarHeadsStaked = solarHeadStaked[msg.sender];

        solarHeadStaked[msg.sender] = 0;

        uint256 tokensEarned;
        uint256 currTime = block.timestamp;
        uint32 _payoutPeriod = payoutPeriod;
        uint256 stakedTime;
        uint256 duration;

        uint256 i = 0;

        while(i < solarHeadsStaked) {
            stakedTime = solarHeadStakedData[msg.sender][i].timestamp;
            duration = currTime - stakedTime;
            tokensEarned += (duration / _payoutPeriod) * 1 ether;
            solarHeadStakedData[msg.sender][i].timestamp = 0;
            IERC721(_solarHead).safeTransferFrom(address(this), msg.sender, solarHeadStakedData[msg.sender][i].tokenId);
            unchecked {
                ++i;
            }
        }

        IERC20(sfaToken).transfer(msg.sender, tokensEarned);
        emit WithdrawalSolarHead(msg.sender);
    }

     /**
     * @notice withdraws users staked Solar Passes. Additonally, claims
     * all SFA Token rewards for the user.
     */
    function withdrawSolarPass() external {

        address _solarPass = solarPass;

        uint256 solarPassesStaked = solarPassStaked[msg.sender];

        solarPassStaked[msg.sender] = 0;

        uint256 tokensEarned;
        uint256 currTime = block.timestamp;
        uint32 _payoutPeriod = payoutPeriod;
        uint256 stakedTime;
        uint256 duration;

        uint256 i = 0;

        while(i < solarPassesStaked) {
            stakedTime = solarPassStakedData[msg.sender][i].timestamp;
            duration = currTime - stakedTime;
            tokensEarned += (duration / _payoutPeriod) * 10 ether;
            solarPassStakedData[msg.sender][i].timestamp = 0;
            IERC721(_solarPass).safeTransferFrom(address(this), msg.sender, solarPassStakedData[msg.sender][i].tokenId);
            unchecked {
                ++i;
            }
        }

        IERC20(sfaToken).transfer(msg.sender, tokensEarned);
        emit WithdrawalSolarPass(msg.sender);
    }

     /**
     * @notice required to allow this contract to recieve NFTs
     * in accordance of ERC721 token standard 
     */
    function onERC721Received(
        address,
        address,
        uint256,
        bytes memory
    ) public virtual override returns (bytes4) {
        return this.onERC721Received.selector;
    }

}