//
//  SQText.swift
//  Allovoisins
//
//  Created by Victor on 13/06/2024.
//  Copyright © 2024 AlloVoisins. All rights reserved.
//

import SwiftUI

// MARK: - SQTextFont

enum SQTextFont: String {
    case medium = "TTChocolates-Medium"
    case mediumItalic = "TTChocolates-MediumIt"
    case semiBold = "TTChocolates-DemiBold"
    case bold = "TTChocolates-Bold"
    case boldItalic = "TTChocolates-Bold-Italic"
}

// MARK: - SQText

/// Text component with custom Sequoia typography.
///
/// Use View Modifiers to configure the text:
/// ```swift
/// SQText("Hello world!")
///     .sqSize(18)
///     .sqFont(.bold)
///     .sqColor(.sqNeutral(100))
/// ```
struct SQText: View {
    private let text: String
    private let attributedText: AttributedString?

    @Environment(\.sqTextConfiguration) private var config

    /// Creates a text view with plain string.
    ///
    /// - Parameter text: The string to display
    init(_ text: String) {
        self.text = text
        self.attributedText = nil
    }

    /// Creates a text view with attributed string.
    ///
    /// - Parameter attributedText: The attributed string to display
    init(_ attributedText: AttributedString) {
        self.text = ""
        self.attributedText = attributedText
    }

    var body: some View {
        if let attributedText = attributedText {
            Text(attributedText)
                .font(.custom(config.font.rawValue, fixedSize: config.size))
                .foregroundStyle(config.textColor)
        } else {
            Text(text)
                .font(.custom(config.font.rawValue, fixedSize: config.size))
                .foregroundStyle(config.textColor)
        }
    }
}

// MARK: - Preview

#Preview {
    VStack(spacing: 10) {
        SQText("Hello world!")
            .sqFont(.medium)

        SQText("Hello world!")
            .sqFont(.mediumItalic)

        SQText("Hello world!")
            .sqSize(18)
            .sqFont(.semiBold)

        SQText("Hello world!")
            .sqFont(.bold)

        SQText("Hello world!")
            .sqSize(18)
            .sqFont(.boldItalic)
    }
}
