//
//  SoundImageFormView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 01/02/2023.
//

import SwiftUI

struct SoundImageFormView : View {
    
    var imageBinding: Binding<CoolPic>
    
    @EnvironmentObject var model: TimerModel

    var repeatCountBinding: Binding<Int16>? = nil
    var optionalSound: Binding<Bool>? = nil

    @State var imageSelectionSheetShown: Bool = false

    var body: some View {
        
        Group {
            
            Section(header: Text("Properties")) {
                
                if self.optionalSound != nil {
                    
                    Toggle("Play sound on end", isOn: optionalSound!)

                    if self.optionalSound?.wrappedValue == true {
                        NavigationLink {
                            
                            PlaylistsView().environmentObject(self.model)
                            
//                            SoundSelectionView(playlistBinding: playlistBinding)
                        } label: {
                            Text("Sound")
                        }

//                        Picker(selection: soundBinding) {
//                            ForEach(Sound.allCases) { sound in
//                                Text(sound.localizedString).tag(sound)
//                            }
//                        } label: {
//                            Text("Sound")
//                        }
                    }
                    
                } else {
//                    Picker(selection: soundBinding) {
//                        ForEach(Sound.allCases) { sound in
//                            Text(sound.localizedString).tag(sound)
//                        }
//                    } label: {
//                        Text("Sound")
//                    }
                }
                
                NavigationLink {
                    NavigationStack {
                        PlaylistsView().environmentObject(self.model)
                    }
                } label: {
                    HStack {
                        Text("Sound")
                        Spacer()
                        Text(self.model.soundSelection)
                    }
                }

                if self.repeatCountBinding != nil {
                    Picker("Repeat Count", selection: self.repeatCountBinding!) {
                        ForEach(0..<6) {
                            let count = Int16($0)
                            Text("\(count)").tag(count)
                        }
                        
                    }
                }
                
            }
            
            Section(header: Text("Background")) {
                
                Button {
                    self.imageSelectionSheetShown = true
                } label: {
                    Group {
                        if let image = self.imageBinding.wrappedValue {
                            Image(image.rawValue).resizable()
                        } else {
                            Image(imageBinding.wrappedValue.rawValue).resizable()
                        }
                    }
                    .font(Font.system(size: 90.0))
                    .aspectRatio(1, contentMode: .fit)
                    .frame(width: 100.0, height: 100.0)
                    .cornerRadius(20.0)
                    
                }
                
            }
        }.sheet(isPresented: self.$imageSelectionSheetShown) {
            ImageSelectionView(showBinding: self.$imageSelectionSheetShown, imageBinding: self.imageBinding)
        }
    }
    
}

struct SoundImageFormView_Previews: PreviewProvider {
    
    static var previews: some View {
        Form {
            SoundImageFormView(
                imageBinding: .constant(.pic1),
                repeatCountBinding: .constant(2))
                .environmentObject(TimerModel())
        }
    }
}
