/*
 * Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
 * Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/CSSFontFaceDescriptors.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/ParsedFontFace.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontSourceStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontStyleStyleValue.h>
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/CSS/StyleValues/UnicodeRangeStyleValue.h>
#include <LibWeb/DOM/Document.h>

namespace Web::CSS {

Vector<ParsedFontFace::Source> ParsedFontFace::sources_from_style_value(StyleValue const& style_value)
{
    Vector<Source> sources;
    auto add_source = [&sources](FontSourceStyleValue const& font_source) {
        font_source.source().visit(
            [&](FontSourceStyleValue::Local const& local) {
                sources.empend(string_from_style_value(local.name), OptionalNone {}, Vector<FontTech> {});
            },
            [&](URL const& url) {
                sources.empend(url, font_source.format(), font_source.tech());
            });
    };

    if (style_value.is_font_source()) {
        add_source(style_value.as_font_source());
    } else if (style_value.is_value_list()) {
        for (auto const& source : style_value.as_value_list().values())
            add_source(source->as_font_source());
    }
    return sources;
}

ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& descriptors)
{
    auto extract_percentage_or_normal = [](StyleValue const& value) -> Optional<Percentage> {
        if (value.is_percentage())
            return value.as_percentage().percentage();
        if (value.is_calculated()) {
            // FIXME: These should probably be simplified already?
            return value.as_calculated().resolve_percentage({});
        }
        if (value.to_keyword() == Keyword::Normal)
            return {};

        return {};
    };

    FlyString font_family;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontFamily)))
        font_family = string_from_style_value(*value);

    ComputationContext computation_context {
        .length_resolution_context = Length::ResolutionContext::for_document(*descriptors.parent_rule()->parent_style_sheet()->owning_document())
    };

    Optional<FontWeightRange> weight;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontWeight))) {
        // https://drafts.csswg.org/css-fonts-4/#font-prop-desc
        // The auto values for these three descriptors have the following effects:
        //  - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
        //  - FIXME: For variation axis clamping, clamping does not occur
        if (value->to_keyword() == Keyword::Auto) {
            weight = { 400, 400 };
        } else {
            auto absolutized = value->absolutized(computation_context);
            auto& weight_values = absolutized->as_value_list().values();
            if (weight_values.size() == 1) {
                auto one_weight = static_cast<int>(StyleComputer::compute_font_weight(weight_values[0], {})->as_number().number());
                weight = { one_weight, one_weight };
            } else if (weight_values.size() == 2) {
                // https://w3c.github.io/csswg-drafts/css-fonts/#font-prop-desc
                // User agents must swap the computed value of the startpoint and endpoint of the range in order to forbid decreasing ranges.
                auto first_weight = static_cast<int>(StyleComputer::compute_font_weight(weight_values[0], {})->as_number().number());
                auto second_weight = static_cast<int>(StyleComputer::compute_font_weight(weight_values[1], {})->as_number().number());
                weight = {
                    min(first_weight, second_weight),
                    max(first_weight, second_weight)
                };
            } else {
                VERIFY_NOT_REACHED();
            }
        }
    }

    Optional<int> slope;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontStyle))) {
        // https://drafts.csswg.org/css-fonts-4/#font-prop-desc
        // The auto values for these three descriptors have the following effects:
        //  - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
        //  - FIXME: For variation axis clamping, clamping does not occur
        if (value->to_keyword() == Keyword::Auto) {
            slope = 0;
        } else {
            slope = StyleComputer::compute_font_style(value->absolutized(computation_context))->as_font_style().to_font_slope();
        }
    }

    Optional<int> width;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontWidth))) {
        // https://drafts.csswg.org/css-fonts-4/#font-prop-desc
        // The auto values for these three descriptors have the following effects:
        //  - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
        //  - FIXME: For variation axis clamping, clamping does not occur
        if (value->to_keyword() == Keyword::Auto) {
            width = 100;
        } else {
            width = StyleComputer::compute_font_width(value->absolutized(computation_context))->as_percentage().raw_value();
        }
    }

    Vector<Source> sources;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::Src)))
        sources = sources_from_style_value(*value);

    Vector<Gfx::UnicodeRange> unicode_ranges;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::UnicodeRange))) {
        for (auto const& range : value->as_value_list().values())
            unicode_ranges.append(range->as_unicode_range().unicode_range());
    }

    Optional<Percentage> ascent_override;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::AscentOverride)))
        ascent_override = extract_percentage_or_normal(*value);

    Optional<Percentage> descent_override;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::DescentOverride)))
        descent_override = extract_percentage_or_normal(*value);

    Optional<Percentage> line_gap_override;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::LineGapOverride)))
        line_gap_override = extract_percentage_or_normal(*value);

    FontDisplay font_display;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontDisplay)))
        font_display = keyword_to_font_display(value->to_keyword()).value_or(FontDisplay::Auto);

    Optional<FlyString> font_named_instance;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontNamedInstance))) {
        if (value->is_string())
            font_named_instance = value->as_string().string_value();
    }

    Optional<FlyString> font_language_override;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontLanguageOverride))) {
        if (value->is_string())
            font_language_override = value->as_string().string_value();
    }

    Optional<OrderedHashMap<FlyString, i32>> font_feature_settings;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontFeatureSettings))) {
        if (value->to_keyword() == Keyword::Normal) {
            font_feature_settings.clear();
        } else if (value->is_value_list()) {
            auto const& feature_tags = value->as_value_list().values();
            OrderedHashMap<FlyString, i32> settings;
            settings.ensure_capacity(feature_tags.size());
            for (auto const& feature_tag_style_value : feature_tags) {
                auto const& feature_tag = feature_tag_style_value->as_open_type_tagged();

                // FIXME: We should absolutize feature_tag.value() in case there are relative lengths
                settings.set(feature_tag.tag(), int_from_style_value(feature_tag.value()));
            }
            font_feature_settings = move(settings);
        }
    }

    Optional<OrderedHashMap<FlyString, double>> font_variation_settings;
    if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontVariationSettings))) {
        if (value->to_keyword() == Keyword::Normal) {
            font_variation_settings.clear();
        } else if (value->is_value_list()) {
            auto const& variation_tags = value->as_value_list().values();
            OrderedHashMap<FlyString, double> settings;
            settings.ensure_capacity(variation_tags.size());

            // FIXME: Absolutize these values to handle relative lengths within calcs
            for (auto const& variation_tag : variation_tags)
                settings.set(variation_tag->as_open_type_tagged().tag(), number_from_style_value(variation_tag->as_open_type_tagged().value(), {}));

            font_variation_settings = move(settings);
        }
    }

    return ParsedFontFace {
        *descriptors.parent_rule(),
        move(font_family),
        move(weight),
        move(slope),
        move(width),
        move(sources),
        move(unicode_ranges),
        move(ascent_override),
        move(descent_override),
        move(line_gap_override),
        move(font_display),
        move(font_named_instance),
        move(font_language_override),
        move(font_feature_settings),
        move(font_variation_settings)
    };
}

ParsedFontFace::ParsedFontFace(GC::Ref<CSSRule> parent_rule, FlyString font_family, Optional<FontWeightRange> weight, Optional<int> slope, Optional<int> width, Vector<Source> sources, Vector<Gfx::UnicodeRange> unicode_ranges, Optional<Percentage> ascent_override, Optional<Percentage> descent_override, Optional<Percentage> line_gap_override, FontDisplay font_display, Optional<FlyString> font_named_instance, Optional<FlyString> font_language_override, Optional<OrderedHashMap<FlyString, i32>> font_feature_settings, Optional<OrderedHashMap<FlyString, double>> font_variation_settings)
    : m_parent_rule(parent_rule)
    , m_font_family(move(font_family))
    , m_font_named_instance(move(font_named_instance))
    , m_weight(move(weight))
    , m_slope(slope)
    , m_width(width)
    , m_sources(move(sources))
    , m_unicode_ranges(move(unicode_ranges))
    , m_ascent_override(move(ascent_override))
    , m_descent_override(move(descent_override))
    , m_line_gap_override(move(line_gap_override))
    , m_font_display(font_display)
    , m_font_language_override(font_language_override)
    , m_font_feature_settings(move(font_feature_settings))
    , m_font_variation_settings(move(font_variation_settings))
{
}

}
