//
//  HomeViewModel.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import Foundation
import Combine

import AppTrackingTransparency
import AdSupport

final class HomeViewModel: ObservableObject {
    
    private var subscription = Set<AnyCancellable>()
    
    private let userRepository = UserRepository()
    private let eventRepository = EventRepository()
    private let contentRepository = ContentRepository()
    private let playbackTrackingRepository = PlaybackTrackingRepository()
    
    enum CurrentTab: String {
        case content, live, explorer, message, mypage
    }
    
    @Published var currentTab: CurrentTab = .content
    
    func pushTokenUpdate(pushToken: String) {
        userRepository.updatePushToken(pushToken: pushToken)
            .sink { result in
                switch result {
                case .finished:
                    DEBUG_LOG("finish")
                case .failure(let error):
                    ERROR_LOG(error.localizedDescription)
                }
            } receiveValue: { _ in
            }
            .store(in: &subscription)
    }
    
    func fetchAndUpdateIdfa() {
        ATTrackingManager.requestTrackingAuthorization { [unowned self] status in
            if status == .authorized {
                let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                self.userRepository.updateIdfa(request: IdfaUpdateRequest(adid: idfa))
                    .sink { result in
                        switch result {
                        case .finished:
                            DEBUG_LOG("finish")
                        case .failure(let error):
                            ERROR_LOG(error.localizedDescription)
                        }
                    } receiveValue: { _ in
                    }
                    .store(in: &self.subscription)
            }
        }
    }
    
    func getMemberInfo() {
        userRepository.getMemberInfo()
            .sink { result in
                switch result {
                case .finished:
                    DEBUG_LOG("finish")
                case .failure(let error):
                    ERROR_LOG(error.localizedDescription)
                }
            } receiveValue: { 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 {
                        UserDefaults.set(data.can, forKey: .can)
                        UserDefaults.set(data.isAuth, forKey: .auth)
                        UserDefaults.set(data.role.rawValue, forKey: .role)
                        if data.followingChannelLiveNotice == nil && data.followingChannelUploadContentNotice == nil && data.messageNotice == nil {
                            AppState.shared.isShowNotificationSettingsDialog = true
                        }
                    }
                } catch {
                    print(error)
                }
            }
            .store(in: &subscription)
    }
    
    func getEventPopup() {
        eventRepository.getEventPopup()
            .sink { result in
                switch result {
                case .finished:
                    DEBUG_LOG("finish")
                case .failure(let error):
                    ERROR_LOG(error.localizedDescription)
                }
            } receiveValue: { response in
                let responseData = response.data
                
                do {
                    let jsonDecoder = JSONDecoder()
                    let decoded = try jsonDecoder.decode(ApiResponse<EventItem>.self, from: responseData)
                    
                    if let data = decoded.data, decoded.success {
                        if data.id != UserDefaults.int(forKey: .notShowingEventPopupId) {
                            AppState.shared.eventPopup = data
                        }
                    }
                } catch {
                }
            }
            .store(in: &subscription)
    }
    
    func addAllPlaybackTracking() {
        let playbackTrackingList = playbackTrackingRepository.getAllPlaybackTracking()
        let trackingDataList = playbackTrackingList
            .filter {
                return $0.endPosition != nil
            }
            .filter { ($0.endPosition! - $0.startPosition) >= 10 }
            .map {
                PlaybackTrackingData(contentId: $0.audioContentId, playDateTime: $0.playDateTime, isPreview: $0.isPreview)
            }
        
        contentRepository.addAllPlaybackTracking(request: AddAllPlaybackTrackingRequest(trackingDataList: trackingDataList))
            .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(ApiResponseWithoutData.self, from: responseData)
                    
                    if decoded.success {
                        self.playbackTrackingRepository.removeAllPlaybackTracking()
                    }
                } catch {
                }
            }
            .store(in: &subscription)
    }
}
