import SwiftUI
import SFSafeSymbols

struct InsertableVideo: View, InsertableCommandView {

    final class Model: ObservableObject, InsertableCommandModel {

        @Published
        var posterImage: FileResource?

        @Published
        var videoH265: FileResource?

        @Published
        var videoH264: FileResource?

        @Published
        var videoWebm: FileResource?

        @Published
        var controls: Bool = false

        @Published
        var autoplay: Bool = false

        @Published
        var loop: Bool = false

        @Published
        var muted: Bool = false

        @Published
        var playsinline = false

        @Published
        var preload = false

        @Published
        var preloadType: VideoBlock.Option.Preload = .metadata

        var isReady: Bool {
            videoH265 != nil || videoH264 != nil
        }

        init() {

        }

        var command: String? {
            var lines: [String] = []
            lines.append("```video")
            if let posterImage {
                lines.append("\(VideoBlock.Key.poster): \(posterImage.identifier)")
            }
            if let videoH265 {
                lines.append("\(VideoBlock.Key.h265): \(videoH265.identifier)")
            }
            if let videoH264 {
                lines.append("\(VideoBlock.Key.h264): \(videoH264.identifier)")
            }
            if let videoWebm {
                lines.append("\(VideoBlock.Key.webm): \(videoWebm.identifier)")
            }
            if controls { lines.append(VideoBlock.Key.controls.rawValue) }
            if autoplay { lines.append(VideoBlock.Key.autoplay.rawValue) }
            if loop { lines.append(VideoBlock.Key.loop.rawValue) }
            if muted { lines.append(VideoBlock.Key.muted.rawValue) }
            if playsinline { lines.append(VideoBlock.Key.playsinline.rawValue) }
            if preload {
                lines.append("\(VideoBlock.Key.preload.rawValue):\(preloadType.rawValue)")
            }

            lines.append("```")
            return lines.joined(separator: "\n")
        }
    }

    static let title = "Video"

    static let sheetTitle = "Insert video"

    static let icon: SFSymbol = .movieclapper

    @ObservedObject
    private var model: Model

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

    var body: some View {
        VStack {
            FilePropertyView(
                title: "Video (h265)",
                footer: "Select the video encoded using h265",
                selectedFile: $model.videoH265,
                allowedType: .video)
            FilePropertyView(
                title: "Video (h264)",
                footer: "Select the video encoded using h264",
                selectedFile: $model.videoH264,
                allowedType: .video)
            FilePropertyView(
                title: "Poster image",
                footer: "Select the image to show as the poster",
                selectedFile: $model.posterImage,
                allowedType: .image)
            HStack {
                Toggle("controls", isOn: $model.controls)
                Toggle("autoplay", isOn: $model.autoplay)
                Toggle("loop", isOn: $model.loop)
                Toggle("muted", isOn: $model.muted)
                Toggle("playsinline", isOn: $model.playsinline)
                Spacer()
            }
            HStack {
                Toggle("preload", isOn: $model.preload)
                Picker("", selection: $model.preloadType) {
                    ForEach(VideoBlock.Option.Preload.allCases, id: \.rawValue) { type in
                        Text("\(type.rawValue)").tag(type)
                    }
                }
                .disabled(!model.preload)
                .frame(maxWidth: 100)
                Spacer()
            }
        }
        .toggleStyle(.checkbox)
    }
}
