//
//  ManagementBoxView.swift
//  AcaMate
//
//  Created by TAnine on 2/10/25.
//

import SwiftUI

struct ManagementBoxView: View {
    @StateObject var btnVM = ButtonViewModel()
    @State private var leftBtnID = UUID()
    @State private var rightBtnID = UUID()
    @State var managementList: [SummaryManagement]
    @State private var countNum: Int = 0
    
    var body: some View {
        DashBoardView(image: Image(.Icon.edu), title: "학습 관리") {
            if managementList.isEmpty {
                EmptyBoxView(title: "학습하고 있는 강의가 없습니다.")
            } else {
                HStack {
                    SimpleBtnView(vm: btnVM, id: leftBtnID)
                    Spacer(minLength: 1)
                    SimpleBtnView(vm: btnVM, id: rightBtnID)
                }
                .padding([.bottom],12)
                
                MangCellView(summaryMang: $managementList[countNum])
            }
            
        } moreAction: {
            // MARK: TO-DO
            // 학습관리 전체 페이지 이동 로직 추가
            printLog("학습 관리의 더보기")
        }
        .onAppear {
            btnVM.setImage(for: leftBtnID, newImage: Image(.Icon.left))
            btnVM.setSize(for: leftBtnID, newWidth: 24, newHeight: 24)
            btnVM.setAction(for: leftBtnID) { countNum -= 1 }
            btnVM.setIsUsable(for: leftBtnID, newValue: false)
            
            btnVM.setImage(for: rightBtnID, newImage: Image(.Icon.right))
            btnVM.setSize(for: rightBtnID, newWidth: 24, newHeight: 24)
            btnVM.setAction(for: rightBtnID) { countNum += 1 }
            if managementList.count == 0 {
                btnVM.setIsUsable(for: rightBtnID, newValue: false)
            }
            
        }
        .onChange(of: countNum) { oldValue, newValue in
            if countNum == 0 {
                btnVM.setIsUsable(for: leftBtnID, newValue: false)
                
            } else if 0 < countNum && countNum < managementList.count-1  {
                btnVM.setIsUsable(for: leftBtnID, newValue: true)
                btnVM.setIsUsable(for: rightBtnID, newValue: true)
            }
            else {
                btnVM.setIsUsable(for: rightBtnID, newValue: false)
            }
        }
    }
}

struct MangCellView: View {
    @StateObject var btnVM = ButtonViewModel()
    @Binding var summaryMang: SummaryManagement
    @State var ratioBtn = UUID()
    @State var flagBtn = UUID()
    var body: some View {
        VStack(spacing: 0) {
            VStack(spacing: 8) {
                HStack(spacing: 12) {
                    Image(.Icon.management)
                        .resizable()
                        .frame(width: 24, height: 24, alignment: .center)
                    Text("\(summaryMang.title)")
                        .font(.nps(size: 20))
                        .foregroundStyle(Color(.Text.title))
                    Spacer(minLength: 1)
                }
                HStack(spacing: 0) {
                    Spacer(minLength: 1)
                    Text("\(summaryMang.teacher) 선생님")
                        .font(.nps(size: 12))
                        .foregroundStyle(Color(.Text.detail))
                }
                
                
            }
            // 상단 내용은 여까지
            HStack(spacing: 6) {
                HStack(spacing: 10) {
                    CircleBtnView(vm: btnVM, id: ratioBtn)
                    VStack(spacing: 10) {
                        HStack(spacing: 4) {
                            Text("학습")
                                .font(.nps(size: 16))
                                .foregroundStyle(Color(.Text.detail))
                            Text("진도")
                                .font(.nps(font: .bold, size: 16))
                                .foregroundStyle(Color(.Text.detail))
                            Spacer(minLength: 1)
                        }
                        HStack(spacing: 4) {
                            Spacer(minLength: 1)
                            Text("\(summaryMang.ratio)")
                                .font(.nps(font: .bold, size: 24))
                                .foregroundStyle(summaryMang.ratio > 70 ? Color(.Other.blue):Color(.Other.red))
                            Text("%")
                                .font(.nps(font: .bold, size: 20))
                                .foregroundStyle(Color(.Text.detail))
                            
                        }
                    }
                    VStack(spacing: 10) {
                        HStack(spacing: 4) {
                            Text("숙제")
                                .font(.nps(font: .bold, size: 16))
                                .foregroundStyle(Color(.Text.detail))
                            Text("개수")
                                .font(.nps(size: 16))
                                .foregroundStyle(Color(.Text.detail))
                            
                            Spacer(minLength: 1)
                        }
                        HStack(spacing: 4) {
                            Spacer(minLength: 1)
                            Text("\(summaryMang.homework)")
                                .font(.nps(font: .bold, size: 24))
                                .foregroundStyle(Color(.Second.normal))
                            Text("개")
                                .font(.nps(font: .bold, size: 20))
                                .foregroundStyle(Color(.Text.detail))
                            
                        }
                    }
                }
                
            }
            
        }
        .padding(12)
        .background {
            RoundedRectangle(cornerRadius: 4)
                .stroke(Color(.Second.normal), lineWidth: 2)
                .fill(Color(.Second.light))
        }
        .onTapGesture {
            // MARK: TO-DO
            // 해당 과목의 학습관리 페이지 이동 로직 추가
            printLog(summaryMang)
        }
        
    }
}
