// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";


contract ImagePicker {
    event accountHexEvent(bytes accountHex);
    event shortAddressProgressEvent(bytes shortAddress);
    event shortAddressEvent(string shortAddress);
    event shortAddressBytesEvent(bytes32 shortAddressBytes);
    event numberEvent(uint256 number);
    event imageIndexEvent(uint256 imageIndex);

    // TODO: del events, make pure
    // function getImageByAccountHex(bytes memory accountHex) public returns (string memory) {
    function getImageByAccountHex(bytes memory accountHex) public pure returns (string memory) {

        // call example:  '\t"image": getImageByAccountHex(accountHex),\n',
        require(accountHex.length == 42, "accountHex length should be 42 characters"); // Validate address length
    
        // Extract the relevant parts of the address
        bytes memory _shortAddressBytes = new bytes(12);

        for (uint256 i = 0; i < 12; i++) {
            if (i <= 7) {
                // 7 first bytes
                _shortAddressBytes[i] = accountHex[i];
            } else {
                // 5 last except one: 36 37 38 39 40
                _shortAddressBytes[i] = accountHex[28 + i];
            }
            // emit shortAddressProgressEvent(_shortAddressBytes);
        }

        // Convert the extracted bytes to a string
        string memory shortAddress = string(_shortAddressBytes);
        // emit shortAddressEvent(shortAddress);

        // Convert the string to a number
        bytes32 shortAddressBytes = bytes32(keccak256(abi.encodePacked(shortAddress)));
        // emit shortAddressBytesEvent(shortAddressBytes);
       
        uint256 number = uint256(shortAddressBytes);
        // emit numberEvent(number);
 
        string[10] memory images = [
            "blue 001",
            "pink 002",
            "green 003",
            "red 004",
            "orange 005",
            "green 006",
            "ping 007",
            "red 008",
            "metal 009",
            "light metal 010"
        ];

        // Calculate the color index
        uint256 imageIndex = number % images.length;
        // emit imageIndexEvent(imageIndex);

        return images[imageIndex];
    }

    // TODO: del events, make pure
    // function getImageByTokenID(uint256 tokenId) external returns(string memory) {
    function getImageByTokenID(uint256 tokenId) external pure returns(string memory) {

        bytes memory accountHex = bytes(Strings.toHexString(tokenId, 20));

        string[10] memory images = [
            "blue 001",
            "pink 002",
            "green 003",
            "red 004",
            "orange 005",
            "green 006",
            "ping 007",
            "red 008",
            "metal 009",
            "light metal 010"
        ];
        string memory image = images[tokenId % images.length];

        // emit accountHexEvent(accountHex);

        return getImageByAccountHex(accountHex);
    }
}