//
//  PlaylistAddContentItemView.swift
//  SodaLive
//
//  Created by klaus on 12/9/24.
//

import SwiftUI
import Kingfisher

struct PlaylistAddContentItemView: View {
    
    let item: GetAudioContentOrderListItem
    let onClick: () -> Void
    @State private var isChecked: Bool
    
    init(item: GetAudioContentOrderListItem, onClick: @escaping () -> Void, isChecked: Bool) {
        self.item = item
        self.onClick = onClick
        self._isChecked = State(initialValue: isChecked)
    }
    
    var body: some View {
        HStack(spacing: 13.3) {
            KFImage(URL(string: item.coverImageUrl))
                .cancelOnDisappear(true)
                .downsampling(size: CGSize(width: 40, height: 40))
                .resizable()
                .scaledToFill()
                .frame(width: 40, height: 40, alignment: .center)
                .cornerRadius(5.3)
                .clipped()
            
            VStack(alignment: .leading, spacing: 2.6) {
                HStack(spacing: 8) {
                    Text(item.themeStr)
                        .font(.custom(Font.medium.rawValue, size: 10))
                        .foregroundColor(Color(hex: "3bac6a"))
                        .padding(2.6)
                        .background(Color(hex: "28312b"))
                        .cornerRadius(2.6)
                    
                    if let duration = item.duration {
                        Text(duration)
                            .font(.custom(Font.medium.rawValue, size: 10))
                            .foregroundColor(Color.gray77)
                            .padding(2.6)
                            .background(Color.gray22)
                            .cornerRadius(2.6)
                    }
                }
                
                Text(item.title)
                    .font(.custom(Font.medium.rawValue, size: 12))
                    .foregroundColor(Color.grayd2)
                    .lineLimit(2)
                    .truncationMode(.tail)
            }
            
            Spacer()
            
            Image(isChecked ? "ic_check_blue" : "ic_playlist_add")
                .padding(8)
        }
        .contentShape(Rectangle())
        .onTapGesture {
            onClick()
            isChecked.toggle()
        }
    }
}

#Preview {
    PlaylistAddContentItemView(
        item: GetAudioContentOrderListItem(
            contentId: 1,
            coverImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
            creatorNickname: "유저1",
            title: "안녕하세요 오늘은 커버곡을 들려드릴께요....안녕하세요 오늘은 커버곡을 들려드릴께요....",
            themeStr: "커버곡",
            duration: "00:30:20",
            isAdult: false,
            orderType: .KEEP,
            likeCount: 0,
            commentCount: 0
        ),
        onClick: {},
        isChecked: true
    )
}
