import SwiftUI
import SFSafeSymbols

struct FileToAddView: View {

    @ObservedObject
    var file: FileToAdd

    let delete: (FileToAdd) -> Void

    var symbol: SFSymbol {
        if file.idAlreadyExists {
            return .documentOnDocument
        }
        if file.isSelected {
            return .checkmarkCircleFill
        }
        return .circle
    }

    var color: Color {
        if file.idAlreadyExists {
            return .red
        }
        if file.isSelected {
            return .blue
        }
        return .gray
    }

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Image(systemSymbol: symbol)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 20, height: 20)
                    .foregroundStyle(color)
                    .onTapGesture {
                        if !file.idAlreadyExists {
                            file.isSelected.toggle()
                        }
                    }
                Image(systemSymbol: .trashCircleFill)
                    .resizable()
                    .frame(width: 20, height: 20)
                    .foregroundStyle(.red)
                    .onTapGesture {
                        delete(file)
                    }
                TextField("", text: $file.uniqueId)
                    .textFieldStyle(.roundedBorder)
                    .frame(maxWidth: 200)

            }
            Text(file.url?.path() ?? "Placeholder file")
                .foregroundStyle(.secondary)
        }
    }
}

#Preview {
    List {
        FileToAddView(file: .init(content: .mock, url: URL(fileURLWithPath: "/path/to/file.swift")), delete: { _ in })
        FileToAddView(file: .init(content: .mock, url: URL(fileURLWithPath: "/path/to/file2.swift")), delete: { _ in })
    }
}
