// File: 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;
    }
}

// File: MultipleProposalVoting.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.12;
import "./Strings.sol";
import "./Context.sol";

contract MultipleProposalVoting is Context {
    mapping(string => uint256)[] public votes;
    string[][] public possibleAnswers;
    uint256 public totalVotes;
    uint256 public maxVotes;
    mapping(address => bool) public voters;

    event VoteOccurred(address voter, uint256 voteCount);

    /**
     * @dev Creates a survey by specifying possible answers for each question. Each element in the
     * outer array corresponds to a question. The inner array includes possible answers to that question.
     * While answers are stored on-chain, the questions are not.
     */
    constructor(string[][] memory possibleAnswers_, uint256 maxVotes_) {
        possibleAnswers = possibleAnswers_;
        for (uint64 i = 0; i < possibleAnswers_.length; ++i) {
            votes.push();
        }
        totalVotes = 0;
        maxVotes = maxVotes_;
    }

    /**
     * @dev Votes for answers to all the questions posed function for buys on a whitelist. The price can be different from
     *  the regular minting function. In addition,
     */
    function vote(uint64[] memory answers) public {
        require(
            answers.length == possibleAnswers.length,
            string.concat(
                "Number of answers (",
                Strings.toString(answers.length),
                ") doesn't match number of questions (",
                Strings.toString(possibleAnswers.length),
                ")."
            )
        );
        totalVotes++;
        require(totalVotes <= maxVotes, "Too many votes have been cast.");
        require(!voters[_msgSender()], "Address has already voted.");
        voters[_msgSender()] = true;
        for (uint64 i = 0; i < answers.length; ++i) {
            require(
                answers[i] < possibleAnswers[i].length,
                string.concat(
                    "Answer number ",
                    Strings.toString(i),
                    " is ",
                    Strings.toString(answers[i]),
                    " which is out of bounds - [0, ",
                    Strings.toString(possibleAnswers[i].length),
                    ")."
                )
            );
            votes[i][possibleAnswers[i][answers[i]]]++;
        }

        emit VoteOccurred(_msgSender(), totalVotes);
    }
}

// File: Strings.sol



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

pragma solidity ^0.8.0;

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0x00";
        }
        uint256 temp = value;
        uint256 length = 0;
        while (temp != 0) {
            length++;
            temp >>= 8;
        }
        return toHexString(value, length);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = _HEX_SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }
}