import SwiftUI
import FoundationModels
import MapKit

struct ContentView: View {
   @State private var appData = ApplicationData.shared
   @State private var selected: Int = 0
   let listCities = ["New York", "London", "Tokyo", "Sydney", "Berlin", "Paris", "Rome", "Cairo", "Beijing", "Mumbai"]

   var body: some View {
      VStack(alignment: .leading) {
         HStack {
            Picker("Select City: ", selection: $selected) {
               ForEach(listCities.indices, id: \.self) { index in
                  Text(listCities[index])
                     .tag(index)
               }
            }
            .disabled(appData.session.isResponding)
            Button("Send") {
               Task {
                  if selected >= 0 && selected < listCities.count {
                     appData.prompt = "I'm traveling to \(listCities[selected])"
                     await appData.sendPrompt()
                  }
               }
            }
            .buttonStyle(.borderedProminent)
            .disabled(appData.session.isResponding)
         }
         if let city = appData.response?.city {
             Text("City: \(city)")
         }
         if let country = appData.response?.country {
            Text("Country: \(country)")
         }
         if let language = appData.response?.language {
            Text("Language: \(language)")
         }
         if let currency = appData.response?.currency {
            Text("Currency: \(currency)")
         }
         if let attractions = appData.response?.attractions {
            Map {
               ForEach(attractions, id: \.self) { attraction in
                  if let name = attraction.name, let latitude = attraction.latitude, let longitude = attraction.longitude {
                     Marker(name, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
                  }
               }
            }
         }
         Spacer()
      }
      .frame(minWidth: 350, maxWidth: .infinity, minHeight: 300, maxHeight: .infinity, alignment: .leading)
      .padding()
   }
}