import SwiftUI

struct MenuMainView: View {
    @ObservedObject var taskViewModel = TaskViewModel()

    var body: some View {
        VStack(spacing: 0) {
            TaskTitleView(
                taskViewModel: taskViewModel,
                title: TaskViewMode.MenuList.title,
                viewMode: nil
            )

            ScrollViewReader { proxy in
                ScrollView(.vertical){
                    VStack(spacing: 16) {
                        Color.clear.frame(height: 1).id("top")

                        MenuButton(
                            title: TaskViewMode.FuelSwitching.title,
                            isEnabled: SharingData.my.isFuelSwitchTask,
                            action: { taskViewModel.viewMode = .FuelSwitching }
                        )

                        MenuButton(
                            title: TaskViewMode.NgaNotification.title,
                            isEnabled: SharingData.my.isNga,
                            action: { taskViewModel.viewMode = .NgaNotification }
                        )
                    }
                    .padding(.top, 16)
                }
                .onChange(of: taskViewModel.viewMode) { _ in
                    withAnimation {
                        proxy.scrollTo("top", anchor: .top)
                    }
                }
            }

            Spacer()
        }
    }
}

struct MenuButton: View {
    let title: String
    let isEnabled: Bool
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Text(title)
                .font(FontStyle.DefaultText.font)
                .padding()
                .frame(maxWidth: .infinity, minHeight: 42)
        }
        .foregroundColor(ColorSet.ButtonText.color)
        .background(isEnabled ? ColorSet.PrimaryActiveIcon.color : ColorSet.SecondaryDisable.color)
        .cornerRadius(30)
        .disabled(!isEnabled)
        .frame(width: 153)
    }
}

#Preview {
    MenuMainView(taskViewModel: TaskViewModel())
}
