import SwiftUI

struct OpenSourceLicensesView: View {
    @Environment(\.dismiss) private var dismiss

    let sourceURL: URL

    var body: some View {
        NavigationStack {
            List {
                Section {
                    Text(L10n.text(
                        "license.n3n_summary",
                        fallback: "The HT Portal packet-tunnel extension contains n3n 3.4.6 and the Hive Technology packet bridge. This separate executable is distributed under GNU GPL version 3 only."
                    ))
                    Text(L10n.text(
                        "license.no_warranty",
                        fallback: "This software is provided without warranty. You may redistribute and modify the GPL-covered packet-tunnel components under the GNU GPL version 3."
                    ))
                } header: {
                    Text("n3n / PacketTunnel")
                }

                Section {
                    Link(destination: sourceURL) {
                        Label(
                            L10n.text("license.source", fallback: "Download corresponding source"),
                            systemImage: "arrow.down.doc"
                        )
                    }
                    NavigationLink {
                        LicenseTextView(
                            resourceName: "GPL-3.0-only",
                            title: "GNU GPL v3",
                            fallbackURL: "https://www.gnu.org/licenses/gpl-3.0.html"
                        )
                    } label: {
                        Label(
                            L10n.text("license.gpl", fallback: "GNU GPL version 3"),
                            systemImage: "doc.text"
                        )
                    }
                    NavigationLink {
                        LicenseTextView(
                            resourceName: "LGPL-2.1-only",
                            title: "GNU LGPL v2.1",
                            fallbackURL: "https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
                        )
                    } label: {
                        Label(
                            L10n.text("license.lgpl", fallback: "GNU LGPL version 2.1"),
                            systemImage: "doc.text"
                        )
                    }
                    Link(destination: URL(string: "https://github.com/n42n/n3n")!) {
                        Label(
                            L10n.text("license.project", fallback: "n3n project"),
                            systemImage: "link"
                        )
                    }
                }

                Section {
                    Text("n3n: Copyright (C) 2007-2022 ntop.org and contributors; Copyright (C) 2023-2024 Hamish Coleman.")
                        .font(.footnote)
                        .foregroundStyle(.secondary)
                }
            }
            .navigationTitle(L10n.text("license.title", fallback: "Open-source licenses"))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(L10n.text("action.done", fallback: "Done")) { dismiss() }
                }
            }
        }
    }
}

private struct LicenseTextView: View {
    let resourceName: String
    let title: String
    let fallbackURL: String

    private var licenseText: String {
        guard let url = Bundle.main.url(forResource: resourceName, withExtension: "txt"),
              let text = try? String(contentsOf: url, encoding: .utf8) else {
            return fallbackURL
        }
        return text
    }

    var body: some View {
        ScrollView {
            Text(licenseText)
                .font(.caption.monospaced())
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding()
                .textSelection(.enabled)
        }
        .navigationTitle(title)
        .navigationBarTitleDisplayMode(.inline)
    }
}
