//
//  NotificationSettingsViewModel.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import Foundation
import Combine

final class NotificationSettingsViewModel: ObservableObject {
    
    private let userRepository = UserRepository()
    private var subscription = Set<AnyCancellable>()
    
    @Published var followingChannelLive = false {
        didSet {
            submit(live: followingChannelLive)
        }
    }
    @Published var followingChannelUploadContent = false {
        didSet {
            submit(uploadContent: followingChannelUploadContent)
        }
    }
    @Published var message = false {
        didSet {
            submit(message: message)
        }
    }
    
    @Published var errorMessage = ""
    @Published var isShowPopup = false
    @Published var isLoading = false
    
    func getMemberInfo() {
        isLoading = true
        
        userRepository.getMemberInfo()
            .sink { result in
                switch result {
                case .finished:
                    DEBUG_LOG("finish")
                case .failure(let error):
                    ERROR_LOG(error.localizedDescription)
                }
            } receiveValue: { [unowned self] response in
                let responseData = response.data
                
                do {
                    let jsonDecoder = JSONDecoder()
                    let decoded = try jsonDecoder.decode(ApiResponse<GetMemberInfoResponse>.self, from: responseData)
                    
                    if let data = decoded.data, decoded.success {
                        self.followingChannelLive = data.followingChannelLiveNotice ?? false
                        self.followingChannelUploadContent = data.followingChannelUploadContentNotice ?? false
                        self.message = data.messageNotice ?? false
                    } 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)
    }
    
    func submit(live: Bool? = nil, uploadContent: Bool? = nil, message: Bool? = nil) {
        if !isLoading && (live != nil || uploadContent != nil || message != nil) {
            userRepository
                .updateNotificationSettings(live: live, uploadContent: uploadContent, message: message)
                .sink { result in
                    switch result {
                    case .finished:
                        DEBUG_LOG("finish")
                    case .failure(let error):
                        ERROR_LOG(error.localizedDescription)
                    }
                } receiveValue: { _ in
                }
                .store(in: &subscription)
        }
    }
}
