import SwiftUI
import HighlightedTextEditor

struct PageTitleView: View {

    @ObservedObject
    var page: LocalizedPage

    var body: some View {
        TextField("", text: $page.title)
            .font(.title)
            .textFieldStyle(.plain)
    }
}

struct PageContentView: View {

    @ObservedObject
    var page: Page

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    init(page: Page) {
        self.page = page
    }

    var body: some View {
        if page.isExternalUrl {
            VStack {
                PageTitleView(page: page.localized(in: language))
                    .id(page.identifier + language.rawValue)
                Spacer()
                Text("No content available for external page")
                    .font(.title)
                    .foregroundStyle(.secondary)
                Spacer()
            }.padding()
        } else {
            VStack(alignment: .leading) {
                PageTitleView(page: page.localized(in: language))
                    .id(page.identifier + language.rawValue)
                TagDisplayView(tags: $page.tags)
                LocalizedPageContentView(page: page, language: language)
                    .id(page.identifier + language.rawValue)
            }
            .padding()
        }
    }
}

extension PageContentView: MainContentView {

    init(item: Page) {
        self.page = item
    }

    static let itemDescription = "a page"
}

#Preview {
    PageContentView(page: Page.Mock.empty)
}
