//
//  LikeFavCommonFunc.swift
//  WOKA
//
//  Created by MacBook Pro on 27/06/24.
//

import Foundation
import Alamofire

class LikeFavCommonFunc{
    
    static let shareInstance = LikeFavCommonFunc()
    
    // MARK: - Like , unlike

    func likePost(postID : Int, postType : Int, vc : UIViewController, onCompletion : @escaping (Bool) -> Void){
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        
        let params : Parameters = ["post_id" : postID,
                                   "post_type" : postType]
        
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.post_like, method: .post, parameters: params, headers: headers) { (result : Result<CommonResponseModel, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                    onCompletion(false)
                case 1:
                    Utilities.dismissProgressHUD()
                    onCompletion(true)
                default:
                    Utilities.dismissProgressHUD()
                    onCompletion(false)
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                vc.toast(msg: error.localizedDescription , time: 2)
                onCompletion(false)
            }
        }
    }
    
    func unlikePost(postID : Int, postType : Int, vc : UIViewController, onCompletion : @escaping (Bool) -> Void){
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        
        let params : Parameters = ["post_id" : postID,
                                   "post_type" : postType]
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.post_unlike, method: .post, parameters: params, headers: headers) { (result : Result<BaseResponseModel<CommonResponseModel>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                    onCompletion(false)
                case 1:
                    Utilities.dismissProgressHUD()
                    onCompletion(true)
                default:
                    Utilities.dismissProgressHUD()
                    onCompletion(false)
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                vc.toast(msg: error.localizedDescription , time: 2)
                onCompletion(false)
            }
        }
    }
    
    // MARK: - Remove Favourite

    func removeFavourite(postID : Int, postType : Int, categoryID : Int, vc : UIViewController, onCompletion : @escaping (Bool) -> Void){
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        
        let params : Parameters = ["id" : postID,
                                   "post_type" : postType,
                                   "category_id" : categoryID]
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_remove, method: .post, parameters: params, headers: headers) { (result : Result<CommonResponseModel, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                    onCompletion(false)
                case 1:
                    Utilities.dismissProgressHUD()
                    onCompletion(true)
                default:
                    Utilities.dismissProgressHUD()
                    onCompletion(false)
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                vc.toast(msg: error.localizedDescription , time: 2)
                onCompletion(false)
            }
        }
    }
    
    func addFavourite(postID : Int, postType : Int, categoryID : Int, vc : UIViewController, onCompletion : @escaping (Bool) -> Void){
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        
        let params : Parameters = ["post_id" : postID,
                                   "post_type" : postType,
                                   "category_id" : categoryID]
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_add, method: .post, parameters: params, headers: headers) { (result : Result<CommonResponseModel, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                    onCompletion(false)
                case 1:
                    Utilities.dismissProgressHUD()
                    onCompletion(true)
                default:
                    Utilities.dismissProgressHUD()
                    onCompletion(false)
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                vc.toast(msg: error.localizedDescription , time: 2)
                onCompletion(false)
            }
        }
    }
}
