import MarkdownUI
import SwiftUI

@MainActor
extension Theme {
    static let sybilReadable: Theme = {
        SybilFontRegistry.registerIfNeeded()

        return Theme()
            .text {
                FontFamily(.custom("Inter"))
                FontSize(15)
                ForegroundColor(SybilTheme.text)
            }
            .code {
                FontFamilyVariant(.monospaced)
                FontSize(.em(0.88))
                BackgroundColor(SybilTheme.surfaceStrong.opacity(0.78))
            }
            .strong {
                FontWeight(.semibold)
            }
            .link {
                ForegroundColor(SybilTheme.accent)
            }
            .heading1 { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.18))
                    .markdownMargin(top: .em(1.1), bottom: .em(0.5))
                    .markdownTextStyle {
                        FontWeight(.semibold)
                        FontSize(.em(1.45))
                    }
            }
            .heading2 { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.18))
                    .markdownMargin(top: .em(1.0), bottom: .em(0.45))
                    .markdownTextStyle {
                        FontWeight(.semibold)
                        FontSize(.em(1.25))
                    }
            }
            .heading3 { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.18))
                    .markdownMargin(top: .em(0.9), bottom: .em(0.4))
                    .markdownTextStyle {
                        FontWeight(.semibold)
                        FontSize(.em(1.12))
                    }
            }
            .heading4 { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.18))
                    .markdownMargin(top: .em(0.85), bottom: .em(0.35))
                    .markdownTextStyle {
                        FontWeight(.semibold)
                    }
            }
            .heading5 { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.18))
                    .markdownMargin(top: .em(0.8), bottom: .em(0.35))
                    .markdownTextStyle {
                        FontWeight(.semibold)
                        FontSize(.em(0.92))
                    }
            }
            .heading6 { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.18))
                    .markdownMargin(top: .em(0.8), bottom: .em(0.35))
                    .markdownTextStyle {
                        FontWeight(.semibold)
                        FontSize(.em(0.86))
                    }
            }
            .paragraph { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.46))
                    .markdownMargin(top: .zero, bottom: .em(0.82))
            }
            .blockquote { configuration in
                HStack(alignment: .top, spacing: 10) {
                    RoundedRectangle(cornerRadius: 2)
                        .fill(SybilTheme.primary.opacity(0.55))
                        .frame(width: 3)
                    configuration.label
                        .markdownTextStyle {
                            ForegroundColor(SybilTheme.textMuted)
                        }
                }
                .fixedSize(horizontal: false, vertical: true)
                .markdownMargin(top: .em(0.25), bottom: .em(0.9))
            }
            .codeBlock { configuration in
                ScrollView(.horizontal) {
                    configuration.label
                        .fixedSize(horizontal: false, vertical: true)
                        .relativeLineSpacing(.em(0.28))
                        .markdownTextStyle {
                            FontFamilyVariant(.monospaced)
                            FontSize(.em(0.88))
                        }
                        .padding(12)
                }
                .background(
                    RoundedRectangle(cornerRadius: 10)
                        .fill(SybilTheme.surfaceStrong.opacity(0.82))
                )
                .overlay(
                    RoundedRectangle(cornerRadius: 10)
                        .stroke(SybilTheme.border.opacity(0.72), lineWidth: 1)
                )
                .markdownMargin(top: .em(0.2), bottom: .em(1))
            }
            .list { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .markdownMargin(top: .em(0.08), bottom: .em(0.78))
            }
            .listItem { configuration in
                configuration.label
                    .markdownMargin(top: .em(0.28))
            }
            .table { configuration in
                configuration.label
                    .fixedSize(horizontal: false, vertical: true)
                    .markdownTableBorderStyle(.init(color: SybilTheme.border.opacity(0.85)))
                    .markdownTableBackgroundStyle(
                        .alternatingRows(
                            SybilTheme.surface.opacity(0.72),
                            SybilTheme.surfaceStrong.opacity(0.62)
                        )
                    )
                    .markdownMargin(top: .em(0.2), bottom: .em(1))
            }
            .tableCell { configuration in
                configuration.label
                    .markdownTextStyle {
                        if configuration.row == 0 {
                            FontWeight(.semibold)
                        }
                        BackgroundColor(nil)
                    }
                    .fixedSize(horizontal: false, vertical: true)
                    .relativeLineSpacing(.em(0.30))
                    .padding(.vertical, 7)
                    .padding(.horizontal, 11)
            }
            .thematicBreak {
                Divider()
                    .overlay(SybilTheme.border.opacity(0.82))
                    .markdownMargin(top: .em(1.2), bottom: .em(1.2))
            }
    }()
}
