/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once

#include "iv/markdown/iv_markdown_article.h"

#include "ui/click_handler.h"

#include <memory>
#include <optional>

namespace style {
struct TextStyle;
} // namespace style

namespace Iv::Markdown {

class InlineFormulaObjectCache;

class InlineIvImageRepaintScope {
public:
	InlineIvImageRepaintScope(
		Fn<void()> repaint,
		Fn<void(QRect)> repaintRect);
	~InlineIvImageRepaintScope();

	InlineIvImageRepaintScope(const InlineIvImageRepaintScope &) = delete;
	InlineIvImageRepaintScope &operator=(
		const InlineIvImageRepaintScope &) = delete;

private:
	bool _active = false;
};

[[nodiscard]] ClickHandlerPtr CreatePreparedLinkHandler(PreparedLink link);
[[nodiscard]] std::optional<PreparedLink> ExtractPreparedLink(
	const ClickHandlerPtr &link);
void BindLinks(
	Ui::Text::String *leaf,
	const std::vector<PreparedLink> &links);

[[nodiscard]] std::shared_ptr<InlineFormulaObjectCache>
CreateInlineFormulaObjectCache(std::shared_ptr<MathRenderer> renderer);
void SetInlineFormulaObjectCacheRenderer(
	const std::shared_ptr<InlineFormulaObjectCache> &cache,
	std::shared_ptr<MathRenderer> renderer);
void ClearInlineFormulaObjectCache(
	const std::shared_ptr<InlineFormulaObjectCache> &cache);
void InvalidateInlineFormulaPaletteCache(
	const std::shared_ptr<InlineFormulaObjectCache> &cache);
void InvalidateInlineFormulaRasterCache(
	const std::shared_ptr<InlineFormulaObjectCache> &cache);

[[nodiscard]] const PreparedFormulaSlot *PreparedFormulaFor(
	const std::vector<PreparedFormulaSlot> &formulas,
	int formulaIndex);
[[nodiscard]] PreparedFormulaSlot *PreparedFormulaFor(
	std::vector<PreparedFormulaSlot> *formulas,
	int formulaIndex);
[[nodiscard]] RenderedFormula *FormulaRasterSlot(
	std::vector<RenderedFormula> *rendered,
	int formulaIndex);
[[nodiscard]] RenderedFormula EnsureFormulaRendered(
	const PreparedFormulaSlot *slot,
	RenderedFormula *rendered,
	MathRenderer *renderer,
	int devicePixelRatio);

void SetTextLeaf(
	Ui::Text::String *leaf,
	const style::TextStyle &textStyle,
	const TextWithEntities &text,
	const std::vector<PreparedFormulaSlot> *formulas,
	InlineFormulaObjectCache *inlineFormulaObjects,
	const std::shared_ptr<MediaRuntime> &mediaRuntime,
	int minResizeWidth);

} // namespace Iv::Markdown
