//
//  NewDataView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 13/02/2023.
//

import SwiftUI

enum DataTab: Int, Identifiable, CaseIterable {
    
    case countdown
    case stopwatch

    var id: Int { return self.rawValue }

    var localizedString: String {
        switch self {
        case .countdown: return NSLocalizedString("Coundown", comment: "")
        case .stopwatch: return NSLocalizedString("Stopwatch", comment: "")
        }
    }
}


struct NewDataView: View {
    
    @Environment(\.managedObjectContext) private var viewContext
    
    @Binding var isPresented: Bool
    
    @State var selection: Int = 0
    
    var body: some View {

        NavigationStack {
            
            VStack {
                
                Picker("", selection: $selection) {
                    ForEach(DataTab.allCases) { tab in
                        Text(tab.localizedString)
                    }
                }
                .pickerStyle(.segmented)
                .padding(.horizontal)
                
                TabView(selection: $selection) {
                    NewCountdownView(isPresented: $isPresented)
                        .tag(0)
                        .environment(\.managedObjectContext, viewContext)
                    NewStopwatchView(isPresented: $isPresented)
                        .tag(1)
                        .environment(\.managedObjectContext, viewContext)
                }.tabViewStyle(.page(indexDisplayMode: .never))
            }
        }
        
    }
}

struct NewDataView_Previews: PreviewProvider {
    static var previews: some View {
        NewDataView(isPresented: .constant(true))
            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}
