//
//  ConsoleDetailView.swift
//  Zockerhoehle
//
//  Created by Julian-Steffen Müller on 25.09.19.
//  Copyright © 2019 Julian-Steffen Müller. All rights reserved.
//

import SwiftUI

struct GameSeriesEditView: View {
    @ObservedObject var gameSeries : GameSeries
    
    @State var isImportingLogo : Bool = false
    
    var body: some View {
        Form {
            TextField("Name der Konsole", text: $gameSeries.name ?? "")
            
            //TODO Image
            /*gameSeriesViewModel.cover.map {
                Image (uiImage: $0)
                    .resizable()
                    .frame(width:100, height: 100)
            }*/
            
            Button(action: { self.isImportingLogo = true }, label: { Text("Pick Image") })
        }
        .navigationBarTitle("Editiere Spieleserie")
        .fileImporter(
            isPresented: $isImportingLogo,
            allowedContentTypes: [.jpeg, .png],
            allowsMultipleSelection: false
        ) { result in
            do {
                let selectedFile : URL = try result.get().first!
                
                //It seems that isUbiquitousItem checks if the file is contained in the Apps iCloud folder
                if (ICloudManager.inICloudContainer(url: selectedFile)) {
                    gameSeries.cover_icloud_path = ICloudManager.relativePathFrom(url: selectedFile)
                    
                    print("Selected Image in iCloud Path \(gameSeries.cover_icloud_path ?? "n/a")")
                }else{
                    Alert(title: Text("Falscher Ordner"))
                    print("Außerhalb \(selectedFile.relativeString)")
                }
            }catch{
                print("GameSeriesEditView::body Error getting result '\(result)'")
            }
        }
    }
}
