//
//  CommonNwCall.swift
//  WOKA
//
//  Created by MacBook Pro on 13/06/24.
//

import UIKit
import Alamofire
import FirebaseAnalytics

class CommonNwCallTheme{
    static let shareInstance = CommonNwCallTheme()
    
    /*
     Update our DB for the user not unique now.
     Logic is if user is newly registered and if he clicks on FM, Webseries, LiveTV , Shop, Karaoke, AudioBooks, Games from theme 1 or 2, he will not be unique. Mark our DB to make him isUnnique to false
    */
    func updateUniqueUser(){
        let headers : HTTPHeaders = ["device-id" : AuthFunc.shareInstance.getDeviceUUID(),
                                     "access-token" : AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["post_type" : "1",
                                   "is_clicked" : "1",
                                   "device_type" : "2"] // 1 - android, 2- iOS
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Analytics.unique_click_store, method: .post, parameters: params,headers : headers) {(result : Result<CommonResponseModel, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    /*
                     Error
                     */
                     print("Error Unique User")
                case 1:
                    print("Success Unique User")
                    // update local user data && trigger firebase analytics for engaged_users
                    AuthFunc.shareInstance.userData?.isUniqueUser = false
                    Analytics.logEvent(K.AnalyticsEventKeys.engaged_users_iOS, parameters: nil)
                default:
                    break
                }
            case .failure(_):
                print("Failure Unique User")
            }
        }
    }
    
    //check for unique user
    func checkUniqueUser(){
        guard let data = AuthFunc.shareInstance.userData, let uniqueUser = data.isUniqueUser else{return}
        if uniqueUser{ // do the network call and update the annalytics, this means user is unique
            print("User is Unique")
            CommonNwCallTheme.shareInstance.updateUniqueUser()
        }else{ // do nothing.
            print("User is Not Unique")
        }
    }
}

//
//    static let shareInstance = CommonNwCall()
//    var userNotification = [UserNotificationDM]()
//    
//    func getUserNotification(vc : UIViewController , isRefreshing : Bool? = nil, completion : @escaping (Bool) -> Void){
//        if isRefreshing == false || isRefreshing == nil{
//            Utilities.startProgressHUD()
//        }
//        let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
//        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.get_user_notifications, method: .get,headers : headers) {(result : Result<BaseResponseModel<[UserNotificationDM]>, NetworkManager.APIError>) in
//            switch result{
//            case .success(let data):
//                switch data.success{
//                case 0:
//                    /*
//                     Error
//                     */
//                    Utilities.dismissProgressHUD()
//                    vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
//                    completion(false)
//                case 1:
//                    Utilities.dismissProgressHUD()
//                    guard let data = data.data else{return}
//                    self.userNotification.removeAll()
//                    self.userNotification.append(contentsOf: data)
//                    //Fetched Blogs
//                    completion(true)
//                default:
//                    completion(false)
//                }
//            case .failure(let error):
//                Utilities.dismissProgressHUD()
//                vc.toast(msg: error.localizedDescription , time: 2)
//                completion(false)
//            }
//        }
//    }
//}
