import SwiftUI
import ImagePlayground

struct ContentView: View {
   @State private var openPlayground: Bool = false
   @State private var image = UIImage(named: "nopicture")!
   @State private var instructions: String = ""

   var body: some View {
      VStack {
         Image(uiImage: image)
            .resizable()
            .frame(width: 300, height: 300)
         TextField("Describe the image", text: $instructions)
            .textFieldStyle(.roundedBorder)
            .padding()
         Button("Create Image") {
            openPlayground = true
         }
         .padding()
         .buttonStyle(.borderedProminent)
         .disabled(openPlayground || instructions.isEmpty)
         Spacer()
      }
      .imagePlaygroundSheet(isPresented: $openPlayground, concepts: [.text(instructions)], onCompletion: { url in
         if let newImage = UIImage(contentsOfFile: url.path) {
            self.image = newImage
            self.instructions = ""
         }
      })
   }
}