//
//  TextMessageItemView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import SwiftUI
import Kingfisher

struct TextMessageItemView: View {
    
    let item: TextMessageItem
    
    var body: some View {
        
        let nickname = item.recipientNickname == UserDefaults.string(forKey: .nickname) ? item.senderNickname : item.recipientNickname
        
        let profileUrl = item.recipientNickname == UserDefaults.string(forKey: .nickname) ? item.senderProfileImageUrl : item.recipientProfileImageUrl
        
        HStack(spacing: 13.3) {
            KFImage(URL(string: profileUrl))
                .cancelOnDisappear(true)
                .downsampling(
                    size: CGSize(
                        width: 46.7,
                        height: 46.7
                    )
                )
                .resizable()
                .scaledToFill()
                .frame(width: 46.7, height: 46.7, alignment: .top)
                .cornerRadius(23.4)
                .clipped()
            
            VStack(alignment: .leading, spacing: 4.7) {
                Text(nickname)
                    .font(.custom(Font.medium.rawValue, size: 13.3))
                    .foregroundColor(Color(hex: "eeeeee"))
                
                Text(item.textMessage)
                    .font(.custom(Font.light.rawValue, size: 12))
                    .foregroundColor(Color(hex: "777777"))
                    .multilineTextAlignment(.leading)
                    .lineLimit(2)
            }
            
            Spacer()
            
            Text(item.date)
                .font(.custom(Font.light.rawValue, size: 12))
                .foregroundColor(Color(hex: "525252"))
        }
    }
}

struct TextMessageItemView_Previews: PreviewProvider {
    static var previews: some View {
        TextMessageItemView(
            item: TextMessageItem(
                messageId: 18,
                senderId: 19,
                senderNickname: "user8",
                senderProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                recipientNickname: "uset7",
                recipientProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                textMessage: "ㅅㅅㅅㅅㅅㅅㅅㅅ러러러라라가가각개가사러",
                date: "2022-05-23 16:15:22",
                isKept: false
            )
        )
    }
}
