import SwiftUI
import SFSafeSymbols

struct FileContentView: View {

    private let iconSize: CGFloat = 150

    @ObservedObject
    var file: FileResource

    @State
    private var fileContent: String = ""

    var body: some View {
        VStack {
            if file.isExternallyStored {
                VStack {
                    Image(systemSymbol: .squareDashed)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: iconSize)
                    Text("External file")
                        .font(.title)
                }
                .foregroundStyle(.secondary)
            } else {
                switch file.type.category {
                case .image:
                    (file.imageToDisplay ?? Image(systemSymbol: .exclamationmarkTriangle))
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                case .model:
                    VStack {
                        Image(systemSymbol: .cubeTransparent)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: iconSize)
                        Text("No preview available")
                            .font(.title)
                    }
                    .foregroundStyle(.secondary)
                case .text, .code:
                    TextFileContentView(file: file)
                        .id(file.identifier + file.modifiedDate.description)
                case .video:
                    VStack {
                        if let image = file.imageToDisplay {
                            image
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                        } else {
                            Image(systemSymbol: .film)
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: iconSize)
                            Button("Generate preview", action: generateVideoPreview)
                                .font(.body)
                        }
                    }.foregroundStyle(.secondary)
                case .resource:
                    VStack {
                        Image(systemSymbol: .questionmarkTextPage)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: iconSize)
                        Text("No preview available")
                            .font(.title)
                    }
                    .foregroundStyle(.secondary)
                case .audio:
                    VStack {
                        Image(systemSymbol: .waveformPath)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: iconSize)
                        Text("No preview available")
                            .font(.title)
                    }
                    .foregroundStyle(.secondary)

                case .route:
                    RoutePreviewView(file: file)
                }
            }
        }.padding()
    }

    private func generateVideoPreview() {
        file.createVideoThumbnail()
    }
}

extension FileContentView: MainContentView {

    init(item: FileResource) {
        self.file = item
    }

    static let itemDescription = "a file"
}

#Preview {
    FileContentView(file: .mock)
}
