import SwiftUI
import SwiftData
import CryptoKit

struct ContentView: View {
    
    private let unlockButtonSize: CGFloat = 250
    private let smallButtonSize: CGFloat = 50
    private let buttonBackground: Color = .white.opacity(0.2)
    private let buttonColor: Color = .white
    
    @ObservedObject
    var coordinator: RequestCoordinator
    
    @State private var showSettingsSheet = false
    @State private var showHistorySheet = false
    @State private var didShowKeySheetOnce = false
    
    init(modelContext: ModelContext) {
        self.coordinator = .init(modelContext: modelContext)
    }
    
    var body: some View {
        VStack(spacing: 20) {
            HStack {
                Spacer()
                Text("Sesame")
                    .font(.title)
                Spacer()
            }
            Text(coordinator.state.description)
            Spacer()
            HStack(alignment: .bottom, spacing: 0) {
                Button(action: { showHistorySheet = true }) {
                    Image(systemSymbol: .clockArrowCirclepath)
                        .foregroundColor(.white)
                        .frame(width: smallButtonSize, height: smallButtonSize)
                        .background(.white.opacity(0.2))
                        .cornerRadius(smallButtonSize / 2)
                        .font(.title2)
                    
                }
                Button("Unlock", action: coordinator.startUnlock)
                    .frame(width: unlockButtonSize, height: unlockButtonSize)
                    .background(buttonBackground)
                    .cornerRadius(unlockButtonSize / 2)
                    .foregroundColor(buttonColor)
                    .font(.title)
                Button(action: { showSettingsSheet = true }) {
                    Image(systemSymbol: .gearshape)
                        .foregroundColor(.white)
                        .frame(width: smallButtonSize, height: smallButtonSize)
                        .background(.white.opacity(0.2))
                        .cornerRadius(smallButtonSize / 2)
                        .font(.title2)
                }
            }
            
            Picker("Connection type", selection: $coordinator.connectionType) {
                ForEach(ConnectionStrategy.allCases, id: \.rawValue) { connection in
                    Text(connection.description).tag(connection)
                }
            }
            .pickerStyle(.segmented)
            .padding(.horizontal, 30)
        }
        .background(coordinator.state.color)
        .onAppear(perform: coordinator.startUpdatingServerChallenge)
        .onDisappear(perform: coordinator.endUpdatingServerChallenge)
        .animation(.easeInOut, value: coordinator.state.color)
        .sheet(isPresented: $showSettingsSheet) {
            SettingsView(
                keyManager: coordinator.keyManager,
                serverAddress: $coordinator.serverPath,
                localAddress: $coordinator.localAddress)
        }
        .sheet(isPresented: $showHistorySheet) { HistoryView() }
        .preferredColorScheme(.dark)
    }
    
    
    
}

#Preview {
    do {
        let config = ModelConfiguration(isStoredInMemoryOnly: true)
        let container = try ModelContainer(for: HistoryItem.self, configurations: config)
        
        let item = HistoryItem.mock
        container.mainContext.insert(item)
        try container.mainContext.save()
        return ContentView(modelContext: container.mainContext)
            .modelContainer(container)
    } catch {
        fatalError("Failed to create model container.")
    }
}
