#pragma once

#include <QColor>
#include <QImage>
#include <QObject>
#include <QString>
#include <QUrl>
#include <QtQml>

#include <memory>

#include <jkqtmathtext/jkqtmathtext.h>

namespace ZShell::llm {

class LlmMathText : public QObject {
	Q_OBJECT
	QML_ELEMENT

	Q_PROPERTY(QString latex READ latex WRITE setLatex NOTIFY changed)
	Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed)
	Q_PROPERTY(
		double fontPointSize READ fontPointSize WRITE setFontPointSize NOTIFY
			changed)
	Q_PROPERTY(
		qreal devicePixelRatio READ devicePixelRatio WRITE setDevicePixelRatio
			NOTIFY changed)
	Q_PROPERTY(QImage image READ image NOTIFY changed)
	Q_PROPERTY(QUrl imageUrl READ imageUrl NOTIFY changed)
	Q_PROPERTY(qreal width READ width NOTIFY changed)
	Q_PROPERTY(qreal height READ height NOTIFY changed)
	Q_PROPERTY(bool ok READ ok NOTIFY changed)

	public:
	explicit LlmMathText(QObject* parent = nullptr);

	[[nodiscard]] QString latex() const { return m_latex; }
	void setLatex(const QString& value);
	[[nodiscard]] QColor color() const { return m_color; }
	void setColor(const QColor& value);
	[[nodiscard]] double fontPointSize() const { return m_fontPointSize; }
	void setFontPointSize(double value);
	[[nodiscard]] qreal devicePixelRatio() const { return m_devicePixelRatio; }
	void setDevicePixelRatio(qreal value);
	[[nodiscard]] QImage image() const { return m_image; }
	[[nodiscard]] QUrl imageUrl() const { return m_imageUrl; }
	[[nodiscard]] qreal width() const { return m_width; }
	[[nodiscard]] qreal height() const { return m_height; }
	[[nodiscard]] bool ok() const { return m_ok; }

	Q_SIGNALS:
	void changed();

	private:
	void reRender();

	std::shared_ptr<JKQTMathText> m_renderer;
	QString m_latex;
	QColor m_color;
	double m_fontPointSize = 12.0;
	qreal m_devicePixelRatio = 1.0;
	QImage m_image;
	QUrl m_imageUrl;
	qreal m_width = 0;
	qreal m_height = 0;
	bool m_ok = false;
	int m_requestId = 0;
	bool m_inFlight = false;
};

} // namespace ZShell::llm
