//
//  PlaylistAddContentView.swift
//  SodaLive
//
//  Created by klaus on 12/8/24.
//

import SwiftUI

struct PlaylistAddContentView: View {
    
    @StateObject var viewModel = OrderListAllViewModel()
    
    @Binding var isShowing: Bool
    @Binding var contentList: [AudioContentPlaylistContent]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 13.3) {
            ZStack {
                Text("새로운 콘텐츠 추가/제거")
                    .font(.custom(Font.bold.rawValue, size: 18.3))
                    .foregroundColor(Color.grayee)
                
                HStack(spacing: 0) {
                    Spacer()
                    
                    Text("닫기")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color.grayee)
                        .frame(minHeight: 48)
                        .onTapGesture { isShowing = false }
                }
            }
            .frame(height: 50)
            .frame(maxWidth: .infinity)
            .background(Color.black)
            
            HStack(alignment: .center, spacing: 5.3) {
                Text("전체")
                    .font(.custom(Font.medium.rawValue, size: 14.7))
                    .foregroundColor(Color.white)
                
                Text("\(viewModel.totalCount)개")
                    .font(.custom(Font.medium.rawValue, size: 12))
                    .foregroundColor(Color.gray90)
            }
            
            ScrollView(.vertical, showsIndicators: false) {
                LazyVStack(spacing: 10.7) {
                    ForEach(0..<viewModel.orderList.count, id: \.self) { index in
                        let item = viewModel.orderList[index]
                        PlaylistAddContentItemView(
                            item: item,
                            onClick: {
                                let isChecked = contentList.contains(where: {
                                    $0.id == item.contentId
                                })
                                
                                if isChecked {
                                    contentList.removeAll(where: {
                                        $0.id == item.contentId
                                    })
                                } else {
                                    contentList.append(
                                        AudioContentPlaylistContent(
                                            id: item.contentId,
                                            title: item.title,
                                            category: item.themeStr,
                                            coverUrl: item.coverImageUrl,
                                            duration: item.duration ?? "00:00:00",
                                            creatorNickname: item.creatorNickname,
                                            creatorProfileUrl: ""
                                        )
                                    )
                                }
                            },
                            isChecked: contentList.contains(where: {
                                $0.id == item.contentId
                            })
                        )
                        .contentShape(Rectangle())
                        .padding(.horizontal, 13.3)
                        .padding(.top, 6.7)
                        .onAppear {
                            if index == viewModel.orderList.count - 1 {
                                viewModel.getOrderList()
                            }
                        }
                    }
                }
            }
            .onAppear {
                viewModel.getOrderList()
            }
        }
        .padding(.horizontal, 13.3)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.black)
    }
}

#Preview {
    PlaylistAddContentView(
        isShowing: .constant(true),
        contentList: .constant(
            [
                AudioContentPlaylistContent(
                    id: 1,
                    title: "안녕하세요 오늘은 커버곡을 들려드리려고 해요안녕하세요 오늘은 커버곡을 들려드리려고 해요안녕하세요 오늘은 커버곡을 들려드리려고 해요안녕하세요 오늘은 커버곡을 들려드리려고 해요안녕하세요 오늘은 커버곡을 들려드리려고 해요안녕하세요 오늘은 커버곡을 들려드리려고 해요",
                    category: "커버곡",
                    coverUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                    duration: "00:30:20",
                    creatorNickname: "유저1",
                    creatorProfileUrl: "https://test-cf.sodalive.net/profile/default-profile.png"
                ),
                AudioContentPlaylistContent(
                    id: 2,
                    title: "안녕하세요 오늘은 커버곡을 들려드리려고 해요",
                    category: "커버곡",
                    coverUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                    duration: "00:30:20",
                    creatorNickname: "유저2",
                    creatorProfileUrl: "https://test-cf.sodalive.net/profile/default-profile.png"
                )
            ]
        )
    )
}
