namespace Graphics2DText {

    /*
        This class is not exposed to the user.

        Wraps a native font font instance.
        Almost all platforms have some sort of notion of a font object.
        What properties of a font constitutes a unique font object varies, though.
        This class encompasses whatever that notion is.

        nativeData[0] -> native font instance. This will be:
            - JS -> a simple struct with the attributes in it
            - PyGame -> a pygame.font.Font instance
            - Java -> a java.awt.Font instance
            - C# -> a font object

    */
    @localized('en', 'NativeFont')
    @localized('es', 'ESNativeFont')
    @localized('jp', 'JPNativeFont')
    class NativeFont {

        field key;

        static field _nativeFontInstances = {};

        @private
        constructor(type, id, size, style, r, g, b) {
            // Native code must generate the key because some of these attributes will be dropped.
            // For example, PyGame has a single font instance no matter what color, but a font instance changes
            // depending on the size and style. Therefore r/g/b will be dropped, but the others must be part of
            // the unique key.
            this.key = NativeFont.getUniqueKey(type, id, size, r, g, b, style);

            $$$('allocNativeData', this, 1);
            output = [null];
            // assigns native font instance to native data
            status = $lib_graphics2dtext_createNativeFont(this, type, id, size, r, g, b, style);
            if ($$$('isJavaScript') && type == 1) {
                i = 0;
                while (!$lib_graphics2dtext_isDynamicFontLoaded()) {
                    if (++i > 10000) throw new FontNotFoundException("Error loading font resource.");
                    Core.sleep(.001);
                }
            }
            switch (status) {
                case 0: break; // all is fine
                case 1: throw new FontNotFoundException("Font not installed: '" + id + "'");
                case 2: throw new FontNotFoundException("Font resource not found: '" + id + "'");
                default: throw new Exception(); // this won't happen
            }
        }

        @localized('en', 'get')
        @localized('es', 'ESget')
        @localized('jp', 'JPget')
        static function get(type, id, size, style, r, g, b) {
            key = NativeFont.getUniqueKey(type, id, size, r, g, b, style);
            nativeFont = NativeFont._nativeFontInstances.get(key);
            if (nativeFont == null) {
                nativeFont = new NativeFont(type, id, size, style, r, g, b);
                NativeFont._nativeFontInstances[key] = nativeFont;
            }
            return nativeFont;
        }

        static function getUniqueKey(type, id, size, r, g, b, style) {
            output = [];
            $lib_graphics2dtext_getNativeFontUniqueKey(
                // convert everything to strings
                type + '', id + '', size + '', r + '', g + '', b + '', style + '',
                output);
            return output.join('|');
        }
    }
}
