//
//  DriveBoxView.swift
//  AcaMate
//
//  Created by TAnine on 2/12/25.
//

import SwiftUI

struct DriveBoxView: View {
    @EnvironmentObject var appVM: AppViewModel
    
    @State var summaryDriveList: [SummaryDrive]
    
    var body: some View {
        DashBoardView(image: Image(.Icon.drive), title: "배차 현황") {
            if summaryDriveList.isEmpty {
                EmptyBoxView(title: "등록된 차량이 없습니다.")
            } else {
                VStack(spacing: 12) {
                    ForEach(Array(summaryDriveList.enumerated()),id: \.offset) { index, drive in
                        DriveCellView(summaryDrive: $summaryDriveList[index])
                    }
                }
                
            }
        } moreAction: {
            appVM.menuName = .Management
        }
    }
}

struct DriveCellView: View {
    @EnvironmentObject var appVM: AppViewModel
    @StateObject var btnVM = ButtonViewModel()
    @Binding var summaryDrive: SummaryDrive
    
    @State var setNotiBtn = UUID()
    
    var body: some View {
        VStack(spacing:0) {
            HStack(spacing: 0) {
                Image(summaryDrive.isUsable ? .Icon.driveON : .Icon.driveOFF)
                    .resizable()
                    .frame(width: 24, height: 24, alignment: .center)
                Spacer(minLength: 1)
                
                HStack(alignment: .bottom, spacing: 4) {
                    Text("[\(summaryDrive.title)]")
                        .font(.nps(size: 14))
                    
                    Text("\(summaryDrive.isIn ? "등원" : "하원")")
                        .font(.nps(font: .bold, size: 20))
                    
                    Text("차량")
                        .font(.nps(size: 18))
                }
                .foregroundStyle(Color(.Text.detail))
                Spacer(minLength: 1)
                SimpleBtnView(vm: btnVM, id: setNotiBtn)
            }
            HStack(spacing: 0) {
                Spacer(minLength: 1)
                Text("\(summaryDrive.driver) 기사님")
                    .font(.nps(size: 12))
                    .foregroundStyle(Color(.Text.detail))
            }
            HStack(spacing: 0) {
                Text("\(summaryDrive.isUsable ? (summaryDrive.isDrive ? "운행": "") : "정지" )")
                    .font(.nps(font: .bold, size: 20))
                    .foregroundStyle(summaryDrive.isUsable ? (summaryDrive.isDrive ? Color(.Other.red): Color(.Text.detail)) : Color(.Text.disabled))
                Spacer(minLength: 1)
            }
            HStack(alignment: .bottom, spacing: 4) {
                Text("출발")
                    .font(.nps(size: 16))
                
                Text("\(summaryDrive.time)")
                    .font(.nps(font: .bold, size: 20))
                Spacer(minLength: 1)
                
                Text("\(summaryDrive.number) 번")
                    .font(.nps(font: .bold, size: 20))
                Text("차량")
                    .font(.nps(size: 16))
                
                    
            }
            .foregroundStyle(Color(.Text.detail))
                    
        }
        .padding(12)
        .background {
            RoundedRectangle(cornerRadius: 4)
                .stroke(summaryDrive.isUsable ? (summaryDrive.isDrive ? Color(.Other.red): Color(.Second.normal)) : Color(.Disable.dark),
                        lineWidth: 2)
                .fill(summaryDrive.isUsable ? (summaryDrive.isDrive ? Color(.Danger.normal): Color(.Second.light)) : Color(.Disable.normal))
        }
        .onTapGesture {
            // MARK: TO-DO
            // 차량 페이지 이동 로직 추가
            printLog(summaryDrive)
        }
        .onAppear {
            btnVM.setImage(for: setNotiBtn, newImage: summaryDrive.isAlarm ? Image(.Icon.notificationON) : Image(.Icon.notificationOFF))
            btnVM.setSize(for: setNotiBtn, newWidth: 24, newHeight: 24)
            btnVM.setAction(for: setNotiBtn) {
                
                if summaryDrive.isUsable {
                    if summaryDrive.isAlarm{
                        appVM.alertData = AlertData(body: "알람이 해제되었습니다.",
                                                    button: [
                                                        ButtonType(name: "확인", role: .cancel) { printLog("차량 알림 변경 알럿") }
                                                    ])
                    } else {
                        appVM.alertData = AlertData(body: "알람이 설정되었습니다.",
                                                    button: [
                                                        ButtonType(name: "확인", role: .cancel) { printLog("차량 알림 변경 알럿") }
                                                    ])
                    }
                    appVM.showAlert.toggle()
                    summaryDrive.isAlarm.toggle()
                }
            }
        }
        .onChange(of: summaryDrive.isAlarm) { _, _ in
            btnVM.setImage(for: setNotiBtn, newImage: summaryDrive.isAlarm ? Image(.Icon.notificationON) : Image(.Icon.notificationOFF))
        }
    }
}
