//
//  FollowRequestActionManager.swift
//  WoWonder
//
//  Created by Ubaid Javaid on 7/18/20.
//  Copyright © 2020 ScriptSun. All rights reserved.
//

import Foundation
import Alamofire
import WowonderMessengerSDK

class FollowRequestActionManager {
    
    static let sharedInstance = FollowRequestActionManager()
    
    func followRequestAction(user_Id: String, action: String, completionBlock : @escaping (_ Success: FollowRequestActionModal?, _ AuthError: ErrorModel?, _ ServerKeyError: ServerKeyErrorModel?, Error?) -> () ) {
        let params = [
            API.Params.server_key: API.SERVER_KEY.Server_Key,
            API.Params.user_id: user_Id,
            API.Params.request_action: action
        ]
        print("params >>>>> ", params)
        let urlString = API.Following_Methods.Accept_DeclineRequest + (AppInstance.instance.sessionId ?? "")
        print("urlString >>>>> ", urlString)
        AF.request(urlString, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if let res = response.value as? [String: Any] {
                guard let apiStatus = res["api_status"] else { return }
                if apiStatus is Int {
                    let data = try? JSONSerialization.data(withJSONObject: res, options: [])
                    let result = try? JSONDecoder().decode(FollowRequestActionModal.self, from: data!)
                    completionBlock(result, nil, nil, nil)
                } else {
                    let apiStatusString = apiStatus as? String
                    if apiStatusString == "400" {
                        print("apiStatus String = \(apiStatus)")
                        let data = try? JSONSerialization.data(withJSONObject: res, options: [])
                        let result = try? JSONDecoder().decode(ErrorModel.self, from: data!)
                        print("AuthError = \(result?.errors?.error_text ?? "")")
                        completionBlock(nil, result, nil, nil)
                    } else if apiStatusString == "404" {
                        let data = try? JSONSerialization.data(withJSONObject: res, options: [])
                        let result = try? JSONDecoder().decode(ServerKeyErrorModel.self, from: data!)
                        print("AuthError = \(result?.errors?.error_text ?? "")")
                        completionBlock(nil, nil, result, nil)
                    }
                }
            } else {
                print(response.error?.localizedDescription ?? "")
                completionBlock(nil, nil, nil, response.error)
            }
        }
    }
    
}
