#ifndef Font_H
#define Font_H

#include <functional>

#include "Critical.h"
#include "Point.h"
#include "ReferenceCounter.h"

namespace Framework
{
    class Image;      //! Image.h
    class Text;      //! Text.h
    class Character; //! from this file
    class Alphabet;  //! from this file
    class Font;   //! from this file

    //! Stores the alpha values of a character of a specific font.
    //! The other color values are determined by the font color. Hence only
    //! the alpha values.
    class Character : public virtual ReferenceCounter
    {
    private:
        Point size;
        unsigned char* alpha;
        int fontSize;

    public:
        //! Creates a new empty object
        DLLEXPORT Character();
        //! Deletes the object
        DLLEXPORT ~Character();
        //! Creates a new character with a specific size
        //! \param size The size of the character in pixels
        DLLEXPORT void NewCharacter(Point& size);
        //! Sets the alpha value of a specific pixel
        //! \param pos The position of the pixel
        //! \param alpha The value of the pixel
        DLLEXPORT void setPixel(Point& pos, unsigned char alpha);
        //! Sets the alpha value of a specific pixel
        //! \param x The x position of the pixel
        //! \param y The y position of the pixel
        //! \param alpha The value of the pixel
        DLLEXPORT void setPixel(int x, int y, unsigned char alpha);
        //! Sets the alpha value of a specific pixel
        //! \param i The index of the pixel. Calculated as x + y *
        //! width and ranges from 0 to width * height - 1 \param alpha The value
        //! of the pixel
        DLLEXPORT void setPixel(int i, unsigned char alpha);
        //! Sets the font size this character belongs to
        //! \param sg The font size of the character.
        DLLEXPORT void setFontSize(int sg);
        //! Returns the font size this character belongs to
        DLLEXPORT int getFontSize() const;
        //! Returns the alpha values of the character as an array where the
        //! values are stored row by row
        DLLEXPORT unsigned char* getBuff() const;
        //! Returns the unscaled size of the character in pixels.
        DLLEXPORT const Point& getSize() const;
        //! Returns the width of the character in pixels
        DLLEXPORT int getWidth() const;
        //! Returns the height of the character in pixels
        DLLEXPORT int getHeight() const;
    };

    //! Stores all characters of the same font size.
    //! Used by the Font class
    class Alphabet : public virtual ReferenceCounter
    {
    private:
        Character** zeichen;
        int fontSize;

    public:
        //! Creates an empty object
        DLLEXPORT Alphabet();
        //! Deletes the object
        DLLEXPORT ~Alphabet();
        //! Deletes all stored characters
        DLLEXPORT void NeuAlphabet();
        //! Adds a character to the alphabet.
        //! If the added character already exists, it will be overwritten
        //! \param i The ASCII code of the character to add
        //! \param buchstabe The character to add
        DLLEXPORT void setCharacter(unsigned char i, Character* buchstabe);
        //! Sets the font size of the alphabet and its stored characters
        //! \param gr The font size of the alphabet
        DLLEXPORT void setFontSize(int gr);
        //! Returns the stored character for a specific ASCII code
        //! \param i The ASCII code of the character \return A pointer to
        //! the character with increased reference counter
        DLLEXPORT Character* getCharacter(unsigned char i) const;
        //! Returns the stored character for a specific ASCII code
        //! \param i The ASCII code of the character \return A pointer to
        //! the character without increased reference counter
        DLLEXPORT Character* zCharacter(unsigned char i) const;
        //! Checks whether a character exists for a specific ASCII code
        //! \param b The ASCII code to check
        //! \return (true) if a character was found for the code. (false)
        //! otherwise
        DLLEXPORT bool hasCharacter(unsigned char b) const;
        //! Returns the font size whose characters are stored in this alphabet
        DLLEXPORT int getFontSize() const;
    };

    //! Stores a list of alphabets with different font sizes.
    //! Used by the Font class to store all characters sorted by font size.
    class AlphabetArray
    {
    private:
        Alphabet* alphabets[256];

    public:
        //! Creates a new list
        DLLEXPORT AlphabetArray();

        //! Adds an alphabet to the list.
        //! If an alphabet with the same font size already exists,
        //! the alphabet is not added \param alphabet The alphabet
        //! to add \return (true) if the alphabet was added. (false) otherwise.
        DLLEXPORT bool addAlphabet(Alphabet* alphabet);
        //! Removes an alphabet of a specific font size from the list
        //! \param sg The font size of the alphabet to remove
        //! \return (true) if an alphabet was removed. (false) otherwise
        DLLEXPORT bool removeAlphabet(unsigned char sg);

        //! Returns a specific alphabet with increased reference counter
        //! \param sg The font size whose alphabet is sought
        //! \return (0) if no matching alphabet was found
        DLLEXPORT Alphabet* getAlphabet(unsigned char sg) const;
        //! Returns a specific alphabet without increased reference counter
        //! \param sg The font size whose alphabet is sought
        //! \return (0) if no matching alphabet was found
        DLLEXPORT Alphabet* zAlphabet(unsigned char sg) const;
    };

    //! Stores all characters of a font in different font sizes
    class Font : public virtual ReferenceCounter
    {
    private:
        unsigned char alphabetCount;
        AlphabetArray* alphabet;

    public:
        //! Creates an empty font
        DLLEXPORT Font();
        //! Deletes the object
        DLLEXPORT ~Font();
        //! Adds an alphabet to the font. If an alphabet of the same font
        //! size already exists, the alphabet is not added
        //! \param alphabet The alphabet to add
        //! \return (true) if the alphabet was added. (false) otherwise
        DLLEXPORT bool addAlphabet(Alphabet* alphabet);
        //! Removes a specific alphabet from the font
        //! \param sg The font size whose alphabet should be removed
        DLLEXPORT void removeAlphabet(unsigned char sg);
        //! Returns a specific alphabet with increased reference counter
        //! \param sg The font size whose alphabet is sought
        //! \return (0) if no matching alphabet was found
        DLLEXPORT Alphabet* getAlphabet(unsigned char sg) const;
        //! Returns a specific alphabet without increased reference counter
        //! \param sg The font size whose alphabet is sought
        //! \return (0) if no matching alphabet was found
        DLLEXPORT Alphabet* zAlphabet(unsigned char sg) const;
        //! Returns how many alphabets (and thus font sizes) the font contains
        DLLEXPORT unsigned char getAlphabetCount() const;
    };

    class TextRenderer : public virtual ReferenceCounter
    {
    protected:
        Font* s;
        int fontSize;
        int lineSpacing;
        int charSpacing;
        int charWidths[256];
        int charHeights[256];

    public:
        DLLEXPORT TextRenderer();
        DLLEXPORT TextRenderer(Font* font);
        DLLEXPORT virtual ~TextRenderer();
        DLLEXPORT void setFontZ(Font* font);
        DLLEXPORT Font* getFont();
        DLLEXPORT Font* zFont();
        //! Sets the font size to draw with. The font automatically
        //! selects the appropriate alphabet for drawing \param sg The font size
        DLLEXPORT void setFontSize(int sg);
        //! Sets the line spacing to use for drawing
        //! \param za The line spacing to the bottom of the line above in pixels
        DLLEXPORT void setLineSpacing(int za);
        //! Sets the character spacing to use for drawing
        //! \param za The character spacing in pixels
        DLLEXPORT void setCharSpacing(int za);
        //! Inserts line breaks into the text so it is fully displayed
        //! at a given width \param zText The text to insert line breaks into
        //! \param maxWidth The width in pixels at which the text should be
        //! fully displayed
        DLLEXPORT virtual void formatText(Text* zText, int maxWidth);
        //! Draws a specific text with cursor and coloring onto an image.
        //! Use (setPosition) and (setDrawFontGroesse) to change position
        //! and size \param x x position of the first character
        //! \param y y position of the first character
        //! \param txt The text to draw
        //! \param zRObj The image to draw on
        //! \param cpos The position of the cursor in the text
        //! \param cf The color of the cursor
        //! \param fbeg The position of the character in the text where coloring
        //! should begin. The text is colored from there to the cursor position
        //! \param ff The background color of the colored text
        //! \param f A function called for each character that returns its color
        DLLEXPORT virtual void renderText(int x,
            int y,
            const char* txt,
            Image& zRObj,
            std::function<int(int, int, int)> f,
            int cpos = -1,
            int cf = 0,
            int fbeg = -1,
            int ff = 0);
        //! Draws a specific text with cursor and coloring onto an image.
        //! Use (setPosition) and (setDrawFontGroesse) to change position
        //! and size \param x x position of the first character
        //! \param y y position of the first character
        //! \param txt The text to draw
        //! \param zRObj The image to draw on
        //! \param cpos The position of the cursor in the text
        //! \param cf The color of the cursor
        //! \param fbeg The position of the character in the text where coloring
        //! should begin. The text is colored from there to the cursor position
        //! \param ff The background color of the colored text
        //! \param f The color in which the text should be drawn
        DLLEXPORT virtual void renderText(int x,
            int y,
            const char* txt,
            Image& zRObj,
            int f,
            int cpos = -1,
            int cf = 0,
            int fbeg = -1,
            int ff = 0);
        //! Draws a specific character with coloring onto an image.
        //! Use (setPosition) and (setDrawFontGroesse) to change position
        //! and size \param x x position of the first character
        //! \param y y position of the first character
        //! \param txt The text to draw
        //! \param zRObj The image to draw on
        //! \param color The color in which the text should be drawn
        //! \param underlined 1 if the text should be underlined
        //! \param selected 1 if the character should be colored
        //! \param selectedBackgroundColor The background color of the
        //! colored text
        DLLEXPORT virtual void renderChar(int& x,
            int y,
            char c,
            Image& zRObj,
            int color,
            bool underlined = 0,
            bool selected = 0,
            int selectedBackgroundColor = 0);
        //! Returns the font size used for drawing
        DLLEXPORT int getFontSize() const;
        //! Determines how many pixels are needed to fully display a specific text
        //! \param txt The text whose width in pixels should be determined
        DLLEXPORT virtual int getTextWidth(const char* txt) const;
        //! Determines how many pixels are needed to fully display a specific text
        //! \param txt The text whose height in pixels should be determined
        DLLEXPORT virtual int getTextHeight(const char* txt) const;
        //! Determines how many pixels are needed to fully display a specific
        //! character \param c The character whose width in pixels should be
        //! determined
        DLLEXPORT virtual int getCharWidth(const char c) const;
        //! Determines the maximum pixels needed to fully display a character
        DLLEXPORT virtual int getMaxCharWidth() const;
        //! Determines how many pixels are needed to fully display a specific
        //! character \param c The character whose height in pixels should be
        //! determined
        DLLEXPORT virtual int getCharHeight(const char c) const;
        //! Returns the line spacing in pixels to the bottom of the line above
        DLLEXPORT int getLineSpacing() const;
        //! Returns the character spacing in pixels on the x axis
        DLLEXPORT int getCharSpacing() const;
        //! Returns the scaled height needed by a drawn line in pixels
        DLLEXPORT int getRowHeight() const;
        //! Determines the character in the text that the mouse points to
        //! \param zTxt The text the mouse points to
        //! \param mouseX The X position of the mouse in pixels relative to the
        //! position of the first character \param mouseY The Y position of the
        //! mouse in pixels relative to the position of the first character
        DLLEXPORT virtual int textPos(
            const char* txt, int mouseX, int mouseY) const;
    };

    class EngravedTextRenderer : public TextRenderer
    {
    public:
        DLLEXPORT EngravedTextRenderer();
        DLLEXPORT EngravedTextRenderer(Font* font);
        DLLEXPORT virtual ~EngravedTextRenderer();
        //! Draws a specific character with coloring onto an image.
        //! Use (setPosition) and (setDrawFontGroesse) to change position
        //! and size \param x x position of the first character
        //! \param y y position of the first character
        //! \param txt The text to draw
        //! \param zRObj The image to draw on
        //! \param color The color in which the text should be drawn
        //! \param underlined 1 if the text should be underlined
        //! \param selected 1 if the character should be colored
        //! \param selectedBackgroundColor The background color of the
        //! colored text
        DLLEXPORT virtual void renderChar(int& x,
            int y,
            char c,
            Image& zRObj,
            int color,
            bool underlined = 0,
            bool selected = 0,
            int selectedBackgroundColor = 0) override;
        //! Determines how many pixels are needed to fully display a specific
        //! character \param c The character whose width in pixels should be
        //! determined
        DLLEXPORT virtual int getCharWidth(const char c) const override;
        //! Determines how many pixels are needed to fully display a specific
        //! character \param c The character whose height in pixels should be
        //! determined
        DLLEXPORT virtual int getCharHeight(const char c) const override;
    };

    class ItalicTextRenderer : public TextRenderer
    {
    public:
        DLLEXPORT ItalicTextRenderer();
        DLLEXPORT ItalicTextRenderer(Font* font);
        DLLEXPORT virtual ~ItalicTextRenderer();
        //! Draws a specific character with coloring onto an image.
        //! Use (setPosition) and (setDrawFontGroesse) to change position
        //! and size \param x x position of the first character
        //! \param y y position of the first character
        //! \param txt The text to draw
        //! \param zRObj The image to draw on
        //! \param color The color in which the text should be drawn
        //! \param underlined 1 if the text should be underlined
        //! \param selected 1 if the character should be colored
        //! \param selectedBackgroundColor The background color of the
        //! colored text
        DLLEXPORT virtual void renderChar(int& x,
            int y,
            char c,
            Image& zRObj,
            int color,
            bool underlined = 0,
            bool selected = 0,
            int selectedBackgroundColor = 0) override;
        //! Determines how many pixels are needed to fully display a specific
        //! character \param c The character whose width in pixels should be
        //! determined
        DLLEXPORT virtual int getCharWidth(const char c) const override;
    };
} // namespace Framework

#endif