//
//  MenuSettingsView.swift
//  SodaLive
//
//  Created by klaus on 10/7/24.
//

import SwiftUI

struct MenuSettingsView: View {
    
    @StateObject var viewModel = MenuSettingsViewModel()
    
    @Binding var isShowing: Bool
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 13.3) {
                DetailNavigationBar(title: "메뉴 설정") {
                    isShowing = false
                }
                
                HStack(spacing: 13.3) {
                    SelectedButtonView(
                        title: "메뉴 1",
                        isActive: true,
                        isSelected: viewModel.selectedMenu == .MENU_1
                    )
                    .onTapGesture {
                        viewModel.selectMenuPreset(selectedMenuPreset: .MENU_1)
                    }
                    
                    SelectedButtonView(
                        title: "메뉴 2",
                        isActive: viewModel.menuList.count > 0,
                        isSelected: viewModel.selectedMenu == .MENU_2
                    )
                    .onTapGesture {
                        viewModel.selectMenuPreset(selectedMenuPreset: .MENU_2)
                    }
                    
                    SelectedButtonView(
                        title: "메뉴 3",
                        isActive: viewModel.menuList.count > 1,
                        isSelected: viewModel.selectedMenu == .MENU_3
                    )
                    .onTapGesture {
                        viewModel.selectMenuPreset(selectedMenuPreset: .MENU_3)
                    }
                }
                .padding(.horizontal, 13.3)
                
                TextViewWrapper(
                    text: $viewModel.menu,
                    placeholder: "메뉴판을 작성해주세요.",
                    textColorHex: "eeeeee",
                    backgroundColorHex: "303030"
                )
                .frame(height: 350)
                .cornerRadius(6.7)
                .padding(.horizontal, 13.3)
                
                Text("저장하기")
                    .font(.custom(Font.bold.rawValue, size: 18.3))
                    .foregroundColor(.white)
                    .padding(.vertical, 16)
                    .frame(maxWidth: .infinity)
                    .background(Color.button)
                    .cornerRadius(10)
                    .padding(.horizontal, 13.3)
                    .clipShape(Rectangle())
                    .onTapGesture { viewModel.saveMenu() }
                
                Spacer()
            }
            .onAppear {
                viewModel.getAllMenuPreset {
                    self.isShowing = false
                }
            }
            .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .frame(width: screenSize().width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .cornerRadius(20)
                        .padding(.bottom, 66.7)
                    Spacer()
                }
            }
        }
    }
}

#Preview {
    MenuSettingsView(isShowing: .constant(true))
}