import SwiftUI

struct GeneralSettingsDetailView: View {

    @ObservedObject
    var generalSettings: GeneralSettings

    @State
    private var showFileSelectionSheet = false

    private var requiredFilesText: String {
        let count = generalSettings.requiredFiles.count
        switch count {
        case 0: return "No files"
        case 1: return "1 file"
        default: return "\(count) files"
        }
    }

    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                StringPropertyView(
                    title: "Website URL",
                    text: $generalSettings.url,
                    footer: "The base path where the website is deployed, to generate absolute links")
                
                IntegerPropertyView(
                    title: "Link Preview Image Width",
                    value: $generalSettings.linkPreviewImageWidth,
                    footer: "The maximum width of a link preview image")
                
                IntegerPropertyView(
                    title: "Linfk Preview Image Height",
                    value: $generalSettings.linkPreviewImageHeight,
                    footer: "The maximum height of a link preview image")

                StringPropertyView(
                    title: "Upload User",
                    text: $generalSettings.remoteUserForUpload,
                    footer: "The user on the server to connect via ssh for upload")

                IntegerPropertyView(
                    title: "Upload Port",
                    value: $generalSettings.remotePortForUpload,
                    footer: "The port on the server to rsync the generated website")

                StringPropertyView(
                    title: "Upload Folder",
                    text: $generalSettings.remotePathForUpload,
                    footer: "The path to the folder on the server where the files should be uploaded to")

                OptionalStringPropertyView(
                    title: "Push Notification URL",
                    text: $generalSettings.urlForPushNotification,
                    footer: "The url to send push notifications to")

                OptionalStringPropertyView(
                    title: "Push Notification Access Token",
                    text: $generalSettings.pushNotificationAccessToken,
                    footer: "The access token to use for sending push notifications")

                GenericPropertyView(
                    title: "Required files",
                    footer: "The additional files required by the page") {
                        HStack {
                            Image(systemSymbol: .squareAndPencilCircleFill)
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(height: 20)
                            Text(requiredFilesText)
                            Spacer()
                        }
                        .padding(.vertical, 8)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showFileSelectionSheet = true
                        }
                }
            }
            .padding()
        }
        .sheet(isPresented: $showFileSelectionSheet) {
            MultiFileSelectionView(selectedFiles: $generalSettings.requiredFiles, insertSorted: true)
        }
    }
}
