//
//  FaqVM.swift
//  WOKA
//
//  Created by Bilal on 04/06/2024.
//

import UIKit
import Alamofire

class FaqVM{
    
    weak var vc : FaqVC!
    var faqData = [FaqListDM.ResultData]()
    var lastIndex : Int?
    
    func initView(){
        getFaqs()
    }
    
    // MARK: - Get Faq's
    
    func getFaqs(){
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["faq_category_key" : "watch"]
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.SideBarNav.faq_listing, method: .post, parameters: params,headers : headers) {(result : Result<BaseResponseModel<FaqListDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    self.handlError(msg: data.message ?? "Unrecognised error")
                    return
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let data = data.data?.result else{return}
                    self.faqData = data
                    self.vc.tableView.reloadData()
                default:
                    break
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                self.handlError(msg: error.localizedDescription)
            }
        }
    }
    
    private func handlError(msg : String){
        let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
        vcPush.contentLabel = msg
        vcPush.mainTitleText = "Error"
        vcPush.yesBtnText = "Retry?"
        vcPush.onDoneBlock = { isDone in
            self.getFaqs()
        }
        vcPush.modalPresentationStyle = .overCurrentContext
        vcPush.modalTransitionStyle = .crossDissolve
        self.vc.present(vcPush, animated: true)
    }
}
