//
//  ContentMainNewOrRecommendSeriesView.swift
//  SodaLive
//
//  Created by klaus on 2/21/25.
//

import SwiftUI
import Kingfisher

struct ContentMainNewOrRecommendSeriesView: View {
    
    let title: String
    let recommendSeriesList: [GetRecommendSeriesListResponse]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 13.3) {
            Text(title)
                .font(.custom(Font.bold.rawValue, size: 18.3))
                .foregroundColor(Color(hex: "eeeeee"))
                .padding(.horizontal, 13.3)
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 13.3) {
                    ForEach(0..<recommendSeriesList.count, id: \.self) { index in
                        let item = recommendSeriesList[index]
                        ContentMainNewOrRecommendSeriesItemView(item: item)
                            .onTapGesture {
                                AppState.shared
                                    .setAppStep(step: .seriesDetail(seriesId: item.seriesId))
                            }
                    }
                }
                .padding(.horizontal, 13.3)
            }
        }
    }
}

struct ContentMainNewOrRecommendSeriesItemView: View {
    
    let item: GetRecommendSeriesListResponse
    
    var body: some View {
        VStack(alignment: .leading, spacing: 9) {
            KFImage(URL(string: item.imageUrl))
                .cancelOnDisappear(true)
                .downsampling(size: CGSize(width: 267, height: 141.3))
                .resizable()
                .scaledToFill()
                .frame(width: 267, height: 141.3)
                .clipShape(RoundedRectangle(cornerRadius: 5))
            
            Text(item.title)
                .font(.custom(Font.medium.rawValue, size: 13.3))
                .foregroundColor(.grayee)
            
            HStack(spacing: 5.3) {
                KFImage(URL(string: item.creatorProfileImageUrl))
                    .cancelOnDisappear(true)
                    .downsampling(size: CGSize(width: 30, height: 30))
                    .resizable()
                    .scaledToFill()
                    .frame(width: 30, height: 30)
                    .clipShape(Circle())
                
                Text(item.creatorNickname)
                    .font(.custom(Font.medium.rawValue, size: 10))
                    .foregroundColor(.gray77)
            }
            .onTapGesture {
                AppState.shared
                    .setAppStep(step: .creatorDetail(userId: item.creatorId))
            }
        }
    }
}

#Preview {
    ContentMainNewOrRecommendSeriesView(
        title: "추천 무료 시리즈",
        recommendSeriesList: [
            GetRecommendSeriesListResponse(
                seriesId: 1,
                title: "시리즈 1",
                imageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                creatorId: 1,
                creatorNickname: "유저1",
                creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png"
            ),
            GetRecommendSeriesListResponse(
                seriesId: 1,
                title: "시리즈 1",
                imageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                creatorId: 1,
                creatorNickname: "유저1",
                creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png"
            )
        ]
    )
}

/*
 contentId: 1,
 title: "안녕하세요 오늘은 커버곡을 들려드릴께요....안녕하세요 오늘은 커버곡을 들려드릴께요....",
 coverImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
 themeStr: "커버곡",
 price: 100,
 duration: "00:30:20",
 creatorId: 1,
 creatorNickname: "유저1",
 creatorProfileImageUrl: "https://test-cf.sodalive.net/profile/default-profile.png"
 
 
 */