import SwiftUI

// --- HELPER STRUCTS & EXTENSIONS ---

// Dummy data for the account list
struct Account: Identifiable {
    let id = UUID()
    let name: String
    let username: String
    let isCurrent: Bool
}

// Custom Transition for older Xcode versions
extension AnyTransition {
    static var slideAndFade: AnyTransition {
        let insertion = AnyTransition.move(edge: .top).combined(with: .opacity)
        let removal = AnyTransition.move(edge: .top).combined(with: .opacity)
        return .asymmetric(insertion: insertion, removal: removal)
    }
}

// Helper Views for buttons
struct SideMenuButton: View {
    let icon: String
    let title: String
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            HStack(spacing: 15) {
                Image(systemName: icon)
                    .font(.title3)
                    .frame(width: 30)
                Text(title)
                    .font(.subheadline)
            }
            .foregroundColor(.primary)
            .padding(.vertical, 8)
        }
    }
}

struct SideMenuFooterButton: View {
    let icon: String
    let title: String
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            VStack {
                Image(systemName: icon)
                    .font(.title2)
                Text(title)
                    .font(.caption2)
            }
            .frame(maxWidth: .infinity)
            .foregroundColor(.primary)
        }
        .frame(maxWidth: .infinity)
    }
}


// --- MAIN VIEW ---

struct SideMenuView: View {
    @ObservedObject var viewModel: LoginViewModel
    @EnvironmentObject var themeManager: ThemeManager
    @Environment(\.colorScheme) var colorScheme
    @Binding var isPresented: Bool
    @State private var isAccountListExpanded = false
    @State private var navigateToSettings = false
    @State private var navigateToFAQ = false

    // Adjustable paddings
    private let topPadding: CGFloat = 66
    private let bottomPadding: CGFloat = 34

    private var themeToggleButton: some View {
        Button(action: {
            themeManager.toggleTheme(from: colorScheme)
        }) {
            Image(systemName: iconName)
                .font(.title2)
                .foregroundColor(.primary)
        }
    }

    private var iconName: String {
        let effectiveScheme: ColorScheme
        switch themeManager.theme {
        case .system:
            effectiveScheme = colorScheme
        case .light:
            effectiveScheme = .light
        case .oledDark:
            effectiveScheme = .dark
        }
        
        return effectiveScheme == .dark ? "sun.max.fill" : "moon.fill"
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            NavigationLink(
                destination: SettingsView(viewModel: viewModel),
                isActive: $navigateToSettings
            ) {
                EmptyView()
            }
            NavigationLink(
                destination: FAQView(),
                isActive: $navigateToFAQ
            ) {
                EmptyView()
            }

            ScrollView(showsIndicators: false) {
                VStack(alignment: .leading, spacing: 0) { // Parent VStack

                    // Menu Items
                    VStack(alignment: .leading, spacing: 20) {
                        // Section 1
                        VStack(alignment: .leading, spacing: 7) {
                            SideMenuButton(icon: "person.2.fill", title: NSLocalizedString("Добавить друзей", comment: "Add friends"), action: {})
                            SideMenuButton(icon: "star.fill", title: NSLocalizedString("Fun Fest", comment: "Fun Fest"), action: {})
                            SideMenuButton(icon: "lightbulb.fill", title: NSLocalizedString("Центр авторов", comment: "Creator Center"), action: {})
                        }
                        .padding(.top, topPadding)

                        Divider()

                        // Section 2
                        VStack(alignment: .leading, spacing: 7) {
//                            Text("CATEGORY").font(.caption2).foregroundColor(.secondary)
                            SideMenuButton(icon: "doc.text", title: NSLocalizedString("Черновики", comment: "Drafts"), action: {})
                            SideMenuButton(icon: "bubble.left", title: NSLocalizedString("Мои комментарии", comment: "My Comments"), action: {})
                            SideMenuButton(icon: "clock", title: NSLocalizedString("История", comment: "History"), action: {})
                            SideMenuButton(icon: "arrow.down.circle", title: NSLocalizedString("Мои загрузки", comment: "My Downloads"), action: {})
                        }

                        Divider()

                        // Section 3
                        VStack(alignment: .leading, spacing: 7) {
//                            Text("SERVICES").font(.caption2).foregroundColor(.secondary)
                            SideMenuButton(icon: "shippingbox", title: NSLocalizedString("Заказы", comment: "Orders"), action: {})
                            SideMenuButton(icon: "cart", title: NSLocalizedString("Корзина", comment: "Cart"), action: {})
                            SideMenuButton(icon: "wallet.pass", title: NSLocalizedString("Кошелёк", comment: "Wallet"), action: {})
                        }
                        
                        Divider()
                        
                        // Section 4
                        VStack(alignment: .leading, spacing: 15) {
                            SideMenuButton(icon: "square.grid.2x2", title: NSLocalizedString("Мини-приложения", comment: "Applets"), action: {})
                        }
                    }
                    .padding()
                }
                .frame(maxWidth: .infinity, alignment: .leading) // Align to the left
            }
            .clipped()

            Spacer()

            // Footer
            HStack(spacing: 0) {
                SideMenuFooterButton(
                    icon: "qrcode.viewfinder",
                    title: NSLocalizedString("Скан", comment: "Scan"),
                    action: {}
                )
                Spacer(minLength: 40)
                SideMenuFooterButton(
                    icon: "questionmark.circle",
                    title: NSLocalizedString("Помощь", comment: "Help Center")
                ) {
                    withAnimation(.easeInOut) {
                        isPresented = false
                    }
                    navigateToFAQ = true
                }
                Spacer(minLength: 40)
                SideMenuFooterButton(
                    icon: "gear",
                    title: NSLocalizedString("Настройки", comment: "Settings")
                ) {
                    withAnimation(.easeInOut) {
                        isPresented = false
                    }
                    navigateToSettings = true
                }
            }
            .padding(.horizontal, 28)
            .padding(.vertical)
            .padding(.bottom, bottomPadding)
        }
        .background(Color(UIColor.systemBackground))
    }
}

// --- PREVIEW ---

struct SideMenuView_Previews: PreviewProvider {
    static var previews: some View {
        let mockViewModel = LoginViewModel()
        NavigationView {
            SideMenuView(viewModel: mockViewModel, isPresented: .constant(true))
                .environmentObject(ThemeManager())
        }
    }
}
