// File: contracts/1_SVG_DarkBLONKS.sol



// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.7;

import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/// @title darkBLONKS SVG Contract v1.1
/// @author Matto
/// @notice This contract builds the SVG text.
/// @custom:security-contact monkmatto@protonmail.com

interface iBLONKSsvg {
    function assembleSVG(
        uint256 _ownerEntropy,
        uint256 _tokenEntropy,
        uint8[11] memory _traitsArray,
        uint16[110] memory _locatsArray
    ) external view returns (string memory);
}

contract darkBLONKSsvg is Ownable {
    using Strings for string;
    address private OGcontract = 0xf82aB780a635d577743Bc34AEec1873ea5d37ed5; // Mainnet

    function updateOGcontract(address _newOGcontract) external onlyOwner {
        OGcontract = _newOGcontract;
    }

    function rA(
        uint16 _x,
        uint16 _y,
        uint16 _w,
        uint16 _h,
        uint16 _r,
        uint16 _g,
        uint16 _b,
        uint16 _sw,
        uint16 _sc
    ) internal pure returns (string memory) {
        return
            string(
                abi.encodePacked(
                    '<rect x="',
                    Strings.toString(_x),
                    '" y="',
                    Strings.toString(_y),
                    '" width="',
                    Strings.toString(_w),
                    '" height="',
                    Strings.toString(_h),
                    '" style="fill: rgb(',
                    Strings.toString(_r),
                    ", ",
                    Strings.toString(_g),
                    ", ",
                    Strings.toString(_b),
                    "); stroke-width: ",
                    Strings.toString(_sw),
                    "px; stroke: rgb(",
                    Strings.toString(_sc),
                    ", ",
                    Strings.toString(_sc),
                    ", ",
                    Strings.toString(_sc),
                    ');"/>'
                )
            );
    }

    function rS(
        uint16 _x,
        uint16 _y,
        uint16 _w,
        uint16 _h,
        string memory _style
    ) internal pure returns (string memory) {
        return
            string(
                abi.encodePacked(
                    '<rect x="',
                    Strings.toString(_x),
                    '" y="',
                    Strings.toString(_y),
                    '" width="',
                    Strings.toString(_w),
                    '" height="',
                    Strings.toString(_h),
                    '" style="fill: rgb',
                    _style
                )
            );
    }


    function _substring(string memory str) internal pure returns (string memory) {
        bytes memory strBytes = bytes(str);
        require(strBytes.length > 38, "String is too short");

        bytes memory result = new bytes(strBytes.length - 38);

        for (uint i = 38; i < strBytes.length; i++) {
            result[i - 38] = strBytes[i];
        }

        return string(result);
    }


    function assembleSVG(
        uint256 eO,
        uint256 eT,
        uint8[11] memory tA,
        uint16[110] memory loc
    ) external view returns (string memory) {
        // Variables
        string memory OGsvg = iBLONKSsvg(OGcontract).assembleSVG(
            eO,
            eT,
            tA,
            loc
        );
        OGsvg = _substring(OGsvg);
        string memory b = string(
            abi.encodePacked(
                '<?xml version="1.0" encoding="utf-8"?>',
                '<svg viewBox="0 0 1000 1000" xmlns="http://www.w3.org/2000/svg">',
                OGsvg,
                '<svg viewBox="0 0 1000 1000" xmlns="http://www.w3.org/2000/svg">',
                rS(0, 0, 1000, 1000, '(0,0,0); fill-opacity: 0.7;"/>')
            )
        );
        string memory s;
        // Ear rings
        loc[49] = loc[32] + loc[30] - 35;
        s = '(242,242,255); stroke-width: 2px; stroke: rgb(233,233,242);"/>';
        if (tA[7] == 4) {
            s = '(22,22,22); fill-opacity: 0.5; stroke-width: 8px; stroke: rgb(12,12,12);"/>';
            loc[48] = 20;
        }
        if (tA[7] == 1 || tA[7] == 3 || tA[7] == 4) {
            b = string(
                abi.encodePacked(
                    b,
                    rS(
                        loc[28] + (loc[31] - loc[48]) / 2,
                        loc[49],
                        loc[48],
                        loc[48],
                        s
                    )
                )
            );
        }
        if (tA[7] == 2 || tA[7] == 3 || tA[7] == 4) {
            b = string(
                abi.encodePacked(
                    b,
                    rS(
                        loc[29] + (loc[31] - loc[48]) / 2,
                        loc[49],
                        loc[48],
                        loc[48],
                        s
                    )
                )
            );
        }
        loc[49] = loc[30] + 20;
        s = '(252,214,18);"/>';
        if (tA[8] == 1 || tA[8] == 3) {
            b = string(
                abi.encodePacked(b, rS(loc[28] - 15, loc[49], 30, 15, s))
            );
        }
        if (tA[8] == 2 || tA[8] == 3) {
            b = string(
                abi.encodePacked(
                    b,
                    rS(loc[29] + loc[31] - 15, loc[49], 30, 15, s)
                )
            );
        }
        // Extra Detail
        if (tA[3] == 1) {
            s = '(50,50,255);"/>';
        } else if (tA[3] == 2) {
            s = '(222,22,22);"/>';
        } else if (tA[3] == 3) {
            s = '(150,220,255);"/>';
        } else if (tA[3] == 4) {
            s = '(124,124,124); fill-opacity: 0.8;"/>';
        }
        if (tA[3] > 0) {
            b = string(
                abi.encodePacked(b, rS(loc[54], loc[55], loc[56], loc[57], s))
            );
            b = string(
                abi.encodePacked(b, rS(loc[58], loc[59], loc[60], loc[61], s))
            );
        }
        // Pupils
        if (tA[1] == 1) {
            s = '(220,52,52); stroke-width: 6px; stroke: rgb(210,42,42);"/>';
        } else if (tA[1] == 2) {
            s = '(47,201,20); stroke-width: 6px; stroke: rgb(70,219,44);"/>';
        } else {
            if (tA[4] == 1) {
                s = '(67, 191, 240); stroke-width: 6px; stroke: rgb(77, 201, 250);"/>';
            } else if (tA[4] == 2) {
                s = '(243,104,203); stroke-width: 6px; stroke: rgb(255,116,225);"/>';
            } else if (tA[4] == 3) {
                s = '(22,122,255); stroke-width: 8px; stroke: rgb(255,22,22);"/>';
            } else if (tA[4] == 4) {
                s = '(252,214,18); stroke-width: 6px; stroke: rgb(242,204,8);"/>';
            } else {
                s = '(212,212,212); stroke-width: 6px; stroke: rgb(222,222,222);"/>';
            }
        }
        b = string(
            abi.encodePacked(b, rS(loc[16], loc[17], loc[18], loc[19], s))
        );
        b = string(
            abi.encodePacked(b, rS(loc[20], loc[21], loc[22], loc[23], s))
        );

        if (tA[6] > 0) {
            b = string(
                abi.encodePacked(
                    b,
                    rA(loc[87], loc[88], loc[89], loc[90], 252, 214, 18, 0, 0)
                )
            );
        }
        if (tA[6] == 2) {
            b = string(
                abi.encodePacked(
                    b,
                    rA(loc[87], loc[91], loc[89], loc[92], 0, 120, 90, 0, 0)
                )
            );
        }
        // Other
        if (tA[9] == 3) {
            b = string(
                abi.encodePacked(
                    b,
                    rS(loc[98], loc[99], loc[100], loc[101], '(252,214,18);"/>')
                )
            );
        }
        b = string(abi.encodePacked(b, "</svg></svg>"));
        return b;
    }
}

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



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (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 Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        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/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);
    }
}

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