//
//  BlockedMemberListItemView.swift
//  SodaLive
//
//  Created by klaus on 9/4/24.
//

import SwiftUI
import Kingfisher

struct BlockedMemberListItemView: View {
    
    let item: GetBlockedMemberListItem
    let blockMember: (Int) -> Void
    let unBlockMember: (Int) -> Void
    
    @State private var isBlocked = true
    
    var body: some View {
        VStack(spacing: 13.3) {
            HStack(spacing: 0) {
                KFImage(URL(string: item.profileImageUrl))
                    .cancelOnDisappear(true)
                    .downsampling(
                        size: CGSize(
                            width: 60,
                            height: 60
                        )
                    )
                    .resizable()
                    .frame(width: 60, height: 60)
                    .clipShape(Circle())
                
                Text(item.nickname)
                    .font(.custom(Font.medium.rawValue, size: 16.7))
                    .foregroundColor(Color.grayee)
                    .padding(.leading, 13.3)
                
                Spacer()
                
                Text(isBlocked ? "차단해제" : "차단")
                    .font(.custom(Font.medium.rawValue, size: 12))
                    .foregroundColor(Color.button)
                    .frame(minWidth: 83)
                    .padding(.vertical, 7)
                    .background(Color.button.opacity(isBlocked ? 0.2 : 0.0))
                    .cornerRadius(13.3)
                    .overlay(
                        RoundedRectangle(cornerRadius: 13.3)
                            .stroke(lineWidth: 1)
                            .foregroundColor(Color.button)
                    )
                    .onTapGesture {
                        if isBlocked {
                            unBlockMember(item.memberId)
                        } else {
                            blockMember(item.memberId)
                        }
                        
                        isBlocked = !isBlocked
                    }
            }
            
            Rectangle()
                .foregroundColor(Color.gray59)
                .frame(height: 0.5)
        }
        .onAppear {
            isBlocked = item.isBlocked
        }
    }
}

#Preview("차단됨") {
    BlockedMemberListItemView(
        item: GetBlockedMemberListItem(
            memberId: 1,
            nickname: "유저1",
            profileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
            isBlocked: true
        ),
        blockMember: { _ in },
        unBlockMember: { _ in }
    )
}


#Preview("차단해제됨") {
    BlockedMemberListItemView(
        item: GetBlockedMemberListItem(
            memberId: 1,
            nickname: "유저1",
            profileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
            isBlocked: false
        ),
        blockMember: { _ in },
        unBlockMember: { _ in }
    )
}
