//
//  ContentPlaylistModifyView.swift
//  SodaLive
//
//  Created by klaus on 12/10/24.
//

import SwiftUI

struct ContentPlaylistModifyView: View {
    @StateObject var viewModel = ContentPlaylistModifyViewModel()
    
    let playlistId: Int
    @Binding var isShowing: Bool
    @Binding var reloadData: Bool
    
    @State private var isShowAddContentView = false
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(alignment: .leading, spacing: 0) {
                HStack(spacing: 0) {
                    Button {
                        isShowing = false
                    } label: {
                        Image("ic_back")
                            .resizable()
                            .frame(width: 20, height: 20)
                        
                        Text("재생목록 수정")
                            .font(.custom(Font.bold.rawValue, size: 18.3))
                            .foregroundColor(Color.grayee)
                    }
                    
                    Spacer()
                    
                    Text("수정")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color.grayee)
                        .frame(minHeight: 48)
                        .onTapGesture {
                            viewModel.modifyPlaylist {
                                reloadData = true
                                isShowing = false
                            }
                        }
                }
                .frame(height: 50)
                .padding(.horizontal, 13.3)
                .frame(maxWidth: .infinity)
                .background(Color.black)
                
                HStack(spacing: 0) {
                    Text("재생목록 제목")
                        .font(.custom(Font.bold.rawValue, size: 16.7))
                        .foregroundColor(Color.grayee)
                    
                    Spacer()
                    
                    Text("\(viewModel.title.count)/30")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.gray77)
                        .onChange(of: viewModel.title) { newValue in
                            if newValue.count > 30 {
                                viewModel.title = String(newValue.prefix(30))
                            }
                        }
                }
                .padding(.top, 26.7)
                .padding(.horizontal, 13.3)
                
                TextField("", text: $viewModel.title)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .font(.custom(Font.bold.rawValue, size: 13.3))
                    .foregroundColor(Color.grayee)
                    .keyboardType(.webSearch)
                    .frame(maxWidth: .infinity)
                    .padding(.horizontal, 13.3)
                    .padding(.vertical, 17)
                    .background(Color.gray22)
                    .cornerRadius(6.7)
                    .padding(.top, 13.3)
                    .padding(.horizontal, 13.3)
                
                HStack(spacing: 0) {
                    Text("재생목록 설명을 입력해 주세요")
                        .font(.custom(Font.bold.rawValue, size: 16.7))
                        .foregroundColor(Color.grayee)
                    
                    Spacer()
                    
                    Text("\(viewModel.desc.count)/40")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.gray77)
                        .onChange(of: viewModel.desc) { newValue in
                            if newValue.count > 40 {
                                viewModel.desc = String(newValue.prefix(40))
                            }
                        }
                }
                .padding(.top, 26.7)
                .padding(.horizontal, 13.3)
                
                TextField("", text: $viewModel.desc)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .font(.custom(Font.bold.rawValue, size: 13.3))
                    .foregroundColor(Color.grayee)
                    .keyboardType(.webSearch)
                    .frame(maxWidth: .infinity)
                    .padding(.horizontal, 13.3)
                    .padding(.vertical, 17)
                    .background(Color.gray22)
                    .cornerRadius(6.7)
                    .padding(.top, 13.3)
                    .padding(.horizontal, 13.3)
                
                HStack(spacing: 8) {
                    Image("btn_plus_round")
                    
                    Text("새로운 콘텐츠 추가/제거")
                        .font(.custom(Font.bold.rawValue, size: 14.7))
                        .foregroundColor(Color.button)
                }
                .padding(.top, 26.7)
                .padding(.horizontal, 13.3)
                .onTapGesture {
                    isShowAddContentView = true
                }
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVStack(alignment: .leading, spacing: 13.3) {
                        ForEach(0..<viewModel.contentList.count, id: \.self) { index in
                            PlaylistCreateContentView(content: viewModel.contentList[index])
                        }
                    }
                    .padding(.horizontal, 13.3)
                }
                .padding(.vertical, 13.3)
            }
            .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .frame(width: screenSize().width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .cornerRadius(20)
                        .padding(.bottom, 66.7)
                    Spacer()
                }
            }
            .onAppear {
                viewModel.playlistId = playlistId
            }
            
            if isShowAddContentView {
                PlaylistAddContentView(
                    isShowing: $isShowAddContentView,
                    contentList: $viewModel.contentList
                )
            }
        }
    }
}

#Preview {
    ContentPlaylistModifyView(
        playlistId: 0,
        isShowing: .constant(true),
        reloadData: .constant(false)
    )
}