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

import Foundation
import Moya
import Combine

final class BlockMemberListViewModel: ObservableObject {
    private var userRepository = UserRepository()
    private var subscription = Set<AnyCancellable>()
    
    @Published var isLoading = false
    @Published var errorMessage = ""
    @Published var isShowPopup = false
    
    @Published var blockedMemberList = [GetBlockedMemberListItem]()
    @Published var totalCount = 0
    
    var page = 1
    var isLast = false
    private let pageSize = 10
    
    func getBlockedMemberList() {
        if (!isLast && !isLoading) {
            isLoading = true
            
            userRepository.getBlockedMemberList(page: page, size: pageSize)
                .sink { result in
                    switch result {
                    case .finished:
                        DEBUG_LOG("finish")
                    case .failure(let error):
                        ERROR_LOG(error.localizedDescription)
                    }
                } receiveValue: { [unowned self] response in
                    self.isLoading = false
                    let responseData = response.data
                    
                    do {
                        let jsonDecoder = JSONDecoder()
                        let decoded = try jsonDecoder.decode(ApiResponse<GetBlockedMemberListResponse>.self, from: responseData)
                        self.isLoading = false
                        
                        if let data = decoded.data, decoded.success {
                            if page == 1 {
                                blockedMemberList.removeAll()
                            }
                            
                            self.totalCount = data.totalCount
                            
                            if !data.items.isEmpty {
                                page += 1
                                self.blockedMemberList.append(contentsOf: data.items)
                            } else {
                                isLast = true
                            }
                        } else {
                            if let message = decoded.message {
                                self.errorMessage = message
                            } else {
                                self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
                            }
                            
                            self.isShowPopup = true
                        }
                    } catch {
                        self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
                        self.isShowPopup = true
                        self.isLoading = false
                    }
                }
                .store(in: &subscription)
        } else {
            isLoading = false
        }
    }
    
    func blockMember(userId: Int) {
        userRepository.memberBlock(userId: userId)
            .sink { result in
                switch result {
                case .finished:
                    DEBUG_LOG("finish")
                case .failure(let error):
                    ERROR_LOG(error.localizedDescription)
                }
            } receiveValue: { _ in }
            .store(in: &subscription)
    }
    
    func unBlockMember(userId: Int) {
        userRepository.memberUnBlock(userId: userId)
            .sink { result in
                switch result {
                case .finished:
                    DEBUG_LOG("finish")
                case .failure(let error):
                    ERROR_LOG(error.localizedDescription)
                }
            } receiveValue: { _ in }
            .store(in: &subscription)
    }
}
