#ifndef GRAPHICS_UI_ELEMENTS_LABEL_HPP_
#define GRAPHICS_UI_ELEMENTS_LABEL_HPP_

#include "UINode.hpp"

class Font;

namespace gui {
    struct LineScheme {
        size_t offset;
        bool fake;
    };

    struct LabelCache {
        Font* font = nullptr;
        std::vector<LineScheme> lines;
        /// @brief Reset cache flag
        bool resetFlag = true;
        size_t wrapWidth = -1;
    
        void prepare(Font* font, size_t wrapWidth);
        void update(const std::wstring& text, bool multiline, bool wrap);
    };

    class Label : public UINode {
        LabelCache cache;

        glm::vec2 calcSize();
    protected:
        std::wstring text;
        std::string fontName;
        wstringsupplier supplier = nullptr;
        
        /// @brief Lines interval multiplier
        float lineInterval = 1.5f;

        /// @brief Vertical alignment (only when multiline is set to false)
        Align valign = Align::center;

        /// @brief Line separators and wrapping will be ignored if set to false
        bool multiline = false;

        /// @brief Text wrapping (works only if multiline is enabled)
        bool textWrap = true;
        
        /// @brief Text Y offset relative to label position
        /// (last calculated alignment)
        int textYOffset = 0;

        /// @brief Text line height multiplied by line interval
        int totalLineHeight = 1;

        /// @brief Auto resize label to fit text
        bool autoresize = false;
    public:
        Label(const std::string& text, std::string fontName="normal");
        Label(const std::wstring& text, std::string fontName="normal");

        virtual void setText(const std::wstring& text);
        const std::wstring& getText() const;

        virtual void setFontName(std::string name);
        virtual const std::string& getFontName() const;

        /// @brief Set text vertical alignment (default value: center)
        /// @param align Align::top / Align::center / Align::bottom
        virtual void setVerticalAlign(Align align);
        virtual Align getVerticalAlign() const;

        /// @brief Get line height multiplier used for multiline labels 
        /// (default value: 1.5)
        virtual float getLineInterval() const;

        /// @brief Set line height multiplier used for multiline labels
        virtual void setLineInterval(float interval);

        /// @brief Get Y position of the text relative to label position
        /// @return Y offset
        virtual int getTextYOffset() const;

        /// @brief Get Y position of the line relative to label position
        /// @param line target line index
        /// @return Y offset
        virtual int getLineYOffset(uint line) const;

        /// @brief Get position of line start in the text
        /// @param line target line index
        /// @return position in the text [0..length]
        virtual size_t getTextLineOffset(size_t line) const;

        /// @brief Get line index by its Y offset relative to label position
        /// @param offset target Y offset
        /// @return line index [0..+]
        virtual uint getLineByYOffset(int offset) const;
        virtual uint getLineByTextIndex(size_t index) const;
        virtual uint getLinesNumber() const;
        virtual bool isFakeLine(size_t line) const;

        virtual void draw(const DrawContext* pctx, Assets* assets) override;

        virtual void textSupplier(wstringsupplier supplier);

        virtual void setAutoResize(bool flag);
        virtual bool isAutoResize() const;

        virtual void setMultiline(bool multiline);
        virtual bool isMultiline() const;

        virtual void setTextWrapping(bool flag);
        virtual bool isTextWrapping() const;
    };
}

#endif // GRAPHICS_UI_ELEMENTS_LABEL_HPP_
