import SwiftUI
import SFSafeSymbols

struct InsertableImage: View, InsertableCommandView {

    final class Model: ObservableObject, InsertableCommandModel {

        @Published
        var caption: String?

        @Published
        var selectedImage: FileResource?

        var isReady: Bool {
            selectedImage != nil
        }

        init() {

        }

        var command: String? {
            guard let selectedImage else {
                return nil
            }
            guard let caption else {
                return "![image](\(selectedImage.identifier))"
            }
            return "![image](\(selectedImage.identifier);\(caption))"
        }
    }

    static let title = "Image"

    static let sheetTitle = "Insert image"

    static let icon: SFSymbol = .photo

    @ObservedObject
    private var model: Model

    init(model: Model) {
        self.model = model
    }

    var body: some View {
        VStack {
            FilePropertyView(
                title: "Image",
                footer: "Select the image to insert",
                selectedFile: $model.selectedImage,
                allowedType: .image)
            OptionalStringPropertyView(
                title: "Caption",
                text: $model.caption,
                prompt: "Image Caption",
                footer: "The caption to show on the fullscreen image")
        }
    }
}
