//
//  MyListDataTemp.swift
//  WOKA
//
//  Created by Bilal on 02/08/2024.
//

import Foundation
import Alamofire

class MyListDataTemp{
    
    static let shareInstance = MyListDataTemp()
    
    var favListingData : FavouriteListingDM.ResultData?
    
//    var webSeriesHindi = [FavouriteListingDM.ResultData.ShowDatum]()
    
    var isDatafetched = false
    
    
    // MARK: - Get Favourite Listing
    
    func updateAudioBooks(onCompletion : @escaping (Bool) -> ()){
        if !isDatafetched{
            
        }
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["post_type" : 7,
                                   "api_version" : "v2",
                                   "start" : 0,
                                   "limit" : "3",
                                   "category_id" : 0]
        
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post,parameters : params, headers: headers) { [weak self](result : Result<BaseResponseModel<FavouriteListingDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    self?.favListingData?.audioData = []
                    onCompletion(true)
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let audioData = data.data?.result?.audioData else{
                        onCompletion(false)
                        return
                    }
                    
                    self?.favListingData?.audioData = audioData
                    onCompletion(true)
                default:
                    onCompletion(false)
                }
            case .failure(let error):
                print("Audio Error:- ", error)
                onCompletion(false)
            }
        }
    }
    
    func updateKaraoke(onCompletion : @escaping (Bool) -> ()){
        if !isDatafetched{
            
        }
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["post_type" : 8,
                                   "api_version" : "v2",
                                   "start" : 0,
                                   "limit" : "3",
                                   "category_id" : 0]
        
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post,parameters : params, headers: headers) { [weak self](result : Result<BaseResponseModel<FavouriteListingDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    self?.favListingData?.singKaraokeData = []
                    onCompletion(true)
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let singKaraokeData = data.data?.result?.singKaraokeData else{
                        onCompletion(false)
                        return
                    }
                    
                    self?.favListingData?.singKaraokeData = singKaraokeData
                    onCompletion(true)
                default:
                    onCompletion(false)
                }
            case .failure(let error):
                print("Audio Error:- ", error)
                onCompletion(false)
            }
        }
    }
    
    func updateGames(onCompletion : @escaping (Bool) -> ()){
        if !isDatafetched{
            
        }
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["post_type" : 6,
                                   "api_version" : "v2",
                                   "start" : 0,
                                   "limit" : "3",
                                   "category_id" : 0]
        
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post,parameters : params, headers: headers) { [weak self](result : Result<BaseResponseModel<FavouriteListingDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    self?.favListingData?.gameData = []
                    onCompletion(true)
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let gameData = data.data?.result?.gameData else{
                        onCompletion(false)
                        return
                    }
                    
                    self?.favListingData?.gameData = gameData
                    onCompletion(true)
                default:
                    onCompletion(false)
                }
            case .failure(let error):
                print("Audio Error:- ", error)
                onCompletion(false)
            }
        }
    }
    
    func updateWebSeries(catID : Int,onCompletion : @escaping (Bool) -> ()){
        if !isDatafetched{
            
        }
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["post_type" : 1,
                                   "api_version" : "v2",
                                   "start" : 0,
                                   "limit" : "3",
                                   "category_id" : catID]
        
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post,parameters : params, headers: headers) { [weak self](result : Result<BaseResponseModel<FavouriteListingDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    if catID == 1{
                        self?.favListingData?.showData?.english = []
                        onCompletion(true)
                    }else{
                        self?.favListingData?.showData?.hindi = []
                        onCompletion(true)
                    }
                case 1:
                    Utilities.dismissProgressHUD()
                    if catID == 1{
                        guard let englishData = data.data?.result?.showData?.english else{
                            onCompletion(false)
                            return
                        }
                        self?.favListingData?.showData?.english = englishData
                        onCompletion(true)
                    }else{
                        guard let hindiData = data.data?.result?.showData?.hindi else{
                            onCompletion(false)
                            return
                        }
                        self?.favListingData?.showData?.hindi = hindiData
                        onCompletion(true)
                    }
                default:
                    onCompletion(false)
                }
            case .failure(let error):
                print("Audio Error:- ", error)
                onCompletion(false)
            }
        }
    }
}
