import SwiftUI
import SFSafeSymbols

struct UploadSheet: View {

    @EnvironmentObject
    private var content: Content

    @EnvironmentObject
    private var upload: RemotePush

    @Environment(\.dismiss)
    private var dismiss

    private let lineLimit = 4

    @State
    private var output: [String]

    init(output: [String] = ["Ready to upload", "", "", ""]) {
        self.output = output
    }

    private var uploadSymbol: SFSymbol {
        if upload.isTransmittingToRemote {
            return .squareAndArrowUpBadgeClock
        }
        if !upload.lastPushWasSuccessful {
            return .squareAndArrowUpTrianglebadgeExclamationmark
        }
        return .squareAndArrowUp
    }

    var header: String {
        let user = content.settings.general.remoteUserForUpload
        let port = content.settings.general.remotePortForUpload
        let url = content.settings.general.url.withHttpPrefixRemoved
        let path = content.settings.general.remotePathForUpload.withLeadingSlashRemoved
        return "\(user)@\(url):\(port)/\(path)"
    }

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Button("Upload", action: startUpload)
                    .disabled(upload.isTransmittingToRemote)
                Text(header)
                Spacer()
                Button("Close", action: { dismiss() })
            }
            VStack(alignment: .leading) {
                Text(output[0])
                Text(output[1])
                Text(output[2])
                Text(output[3])
            }
            .font(.body.monospaced())
            .lineLimit(1)
//            TextField("", text: .constant(output.joined(separator: "\n")))
//                .font(.body.monospaced())
//                .textFieldStyle(.plain)
//                .lineLimit(lineLimit)
//                .disabled(true)
//                .frame(minHeight: 150)
//            ScrollView {
//                Text(output.joined(separator: "\n"))
//                    .font(.body.monospaced())
//                    .foregroundStyle(.primary)
//                    .frame(maxWidth: .infinity, alignment: .leading)
//            }
        }
        .padding()
        .frame(minWidth: 500, idealWidth: 600)
    }

    private func startUpload() {
        guard let folder = content.storage.outputScope?.url.path() else {
            output = ["No output folder to start upload", "", "", ""]
            return
        }
        output = ["Starting upload...", "", "", ""]

        upload.transmitToRemote(
            settings: content.settings.general,
            outputFolder: folder) { newContent in
                DispatchQueue.main.async {
                    let newLines = newContent.components(separatedBy: "\n").suffix(lineLimit)
                    if newLines.count >= lineLimit {
                        self.output = newLines.suffix(lineLimit)
                    } else {
                        self.output = (self.output + newLines).suffix(lineLimit)
                    }
                }
            }
    }
}

#Preview {
    UploadSheet(output: [
        "Some very long text that should cause the view to scroll", "More", "Some", "Yes"
    ])
}
