//
//  ChatListView.swift
//  AcaMate
//
//  Created by TAnine on 2/14/25.
//

import SwiftUI

struct ChatListView: View {
    var chatList: [SummaryChat]
    var body: some View {
        VStack(spacing: 10) {
            ForEach(Array(chatList.enumerated()), id:\.offset) { index, chat in
                ChatCellView(summaryChat: chat)
            }
        }
        
    }
}

struct ChatCellView: View {
    @EnvironmentObject var appVM: AppViewModel
    
    var summaryChat: SummaryChat
    
    var body: some View {
        VStack(spacing: 8) {
            HStack(alignment: .bottom, spacing: 4) {
                Image(.Icon.chatting).resizable()
                    .frame(width: 24, height: 24, alignment: .center)
                
                Text("\(summaryChat.chatName)")
                    .font(.nps(size: 20))
                    .foregroundStyle(Color(.Text.detail))
                    .multilineStyle()
                
                if summaryChat.groupNum > 0 {
                    HStack(alignment: .center, spacing: 0) {
                        Image(.Icon.person).resizable()
                            .frame(width: 12, height: 12, alignment: .center)
                        Text("\(summaryChat.groupNum)")
                            .font(.nps(size: 8))
                            .foregroundStyle(Color(.Text.detail))
                    }
                }
                Spacer(minLength: 1)
                
                HStack(alignment: .bottom, spacing: 4) {
                    Image(summaryChat.notiState ? .Icon.notificationON : .Icon.notificationOFF).resizable()
                        .frame(width: 12, height: 12, alignment: .center)
                    Text("\(summaryChat.teacherName)")
                        .font(.nps(font: .bold, size: 12))
                        .foregroundStyle(Color(.Text.detail))
                    Text("선생님")
                        .font(.nps(size: 8))
                        .foregroundStyle(Color(.Text.detail))
                }
                    
            }
            Text("\(summaryChat.lastMessage)")
                .font(.nps(size: 10))
                .foregroundStyle(Color(.Text.detail))
                .multilineStyle(limit: 2, scale: 1)
                .padding(.trailing, 8)
            
            HStack(alignment: .bottom, spacing: 4) {
                Spacer(minLength: 1)
                Group {
                    Text("\(summaryChat.dayDate)")
                        .font(.nps(size: 8))
                    Text("\(summaryChat.timeDate)")
                        .font(.nps(font: .bold, size: 12))
                }
                .foregroundStyle(Color(.Text.detail))
            }
        }
        
        .padding(EdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12))
        .background {
            RoundedRectangle(cornerRadius: 4)
                .stroke(Color(.Second.normal), lineWidth: 2)
                .fill(Color(.Second.light))
        }
        .onTapGesture {
            printLog("채팅 내부 셀 클릭")
            // MARK: TO-DO
            appVM.naviState.set(act: .ADD, path: .ChatRoom(id: summaryChat.id))
        }
    }
}
