//
//  LiveRoomChatView.swift
//  SodaLive
//
//  Created by klaus on 2024/01/17.
//

import SwiftUI

struct LiveRoomChatView: View {
    
    let messages: [LiveRoomChat]
    let getUserProfile: (Int) -> Void
    
    var body: some View {
        LazyVStack(alignment: .leading, spacing: 18) {
            ForEach(0..<messages.count, id: \.self) { index in
                switch (messages[index].type) {
                case LiveRoomChatType.ROULETTE_DONATION:
                    let chatMessage = messages[index] as! LiveRoomRouletteDonationChat
                    LiveRoomRouletteDonationChatItemView(chatMessage: chatMessage)
                    
                case LiveRoomChatType.DONATION:
                    let chatMessage = messages[index] as! LiveRoomDonationChat
                    LiveRoomDonationChatItemView(chatMessage: chatMessage)
                    
                case LiveRoomChatType.JOIN:
                    let chatMessage = messages[index] as! LiveRoomJoinChat
                    LiveRoomJoinChatItemView(chatMessage: chatMessage)
                    
                case LiveRoomChatType.HEART:
                    let chatMessage = messages[index] as! LiveRoomHeartDonationChat
                    LiveRoomHeartDonationChatItemView(chatMessage: chatMessage)
                    
                default:
                    let chatMessage = messages[index] as! LiveRoomNormalChat
                    LiveRoomChatItemView(
                        chatMessage: chatMessage,
                        onClickProfile: {
                            if chatMessage.userId != UserDefaults.int(forKey: .userId) {
                                getUserProfile(chatMessage.userId)
                            }
                        }
                    )
                }
            }
        }
    }
}

struct LiveRoomChatView_Previews: PreviewProvider {
    static var previews: some View {
        LiveRoomChatView(
            messages: [
                LiveRoomRouletteDonationChat(
                    profileUrl: "https://cf.sodalive.net/profile/26/26-profile-ddf78b4d-0300-4c50-9c84-5d8a95fd5fe2-4892-1705256364320",
                    nickname: "jkljkljkl",
                    rouletteResult: "sdfjkldfsjkl",
                    type: .ROULETTE_DONATION
                ),
                LiveRoomRouletteDonationChat(
                    profileUrl: "https://cf.sodalive.net/profile/26/26-profile-ddf78b4d-0300-4c50-9c84-5d8a95fd5fe2-4892-1705256364320",
                    nickname: "jkljkljkl",
                    rouletteResult: "sdfjkldfsjkl",
                    type: .ROULETTE_DONATION
                )
            ],
            getUserProfile: { _ in }
        )
    }
}
