//
//  UserNotificationVM.swift
//  WOKA
//
//  Created by MacBook Pro on 05/08/24.
//

import UIKit
import Alamofire

class UserNotificationVM{
    
    weak var vc : UserNotificationVC!
    var userNotification = [UserNotificationDM]()
    let refreshControl = UIRefreshControl()
    let feedbackGenerator = UIImpactFeedbackGenerator(style: .light)
    
    func initView(){
        let color1 = #colorLiteral(red: 0.05490196078, green: 0.01176470588, blue: 0.3882352941, alpha: 1)
        let color2 = #colorLiteral(red: 0.2041364683, green: 0.156624428, blue: 0.5904380268, alpha: 1)
        self.vc.view.applyMultiGradient(colors: [color1, color2,color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 0.8))
        self.vc.title = "NOTIFICATIONS"
        setupCell()
        Utilities.startProgressHUD()
        getUserNotification()
        
        //Adding pull to Refresh
        refreshControl.attributedTitle = NSAttributedString(string: "Refreshing...",attributes: [.foregroundColor: UIColor.white])
        refreshControl.tintColor = .white
        refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
        vc.tableView.addSubview(refreshControl)
    }
    
    func setupCell(){
        vc.tableView.register(UINib(nibName: K.CellIdentifier.Home.userNotificationCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Home.userNotificationCell)
        vc.tableView.delegate = vc.self
        vc.tableView.dataSource = vc.self
    }
    
    // MARK: - Pull to refresh

    @objc func refresh(_ sender: AnyObject) {
//        CommonNwCall.shareInstance.getUserNotification(vc: self,isRefreshing: true) { [weak self] isDone in
//            guard let self else{return}
//            feedbackGenerator.impactOccurred()
//            tableView.reloadData()
//            refreshControl.endRefreshing()
//        }
        getUserNotification()
        refreshControl.beginRefreshing()
    }
    
    // MARK: - Get Notifications
    
    func getUserNotification(){
        let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.get_user_notifications, method: .get,headers : headers) { [weak self](result : Result<BaseResponseModel<[UserNotificationDM]>, NetworkManager.APIError>) in
            guard let self else{return}
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    /*
                     Error
                     */
                    Utilities.dismissProgressHUD()
                    self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                    self.vc.noDataStack.isHidden = false
                    self.vc.tableView.isHidden = true
                    refreshControl.endRefreshing()
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let data = data.data else{
                        self.vc.noDataStack.isHidden = false
                        return
                    }
                    self.userNotification.removeAll()
                    self.userNotification.append(contentsOf: data)
                    feedbackGenerator.impactOccurred()
                    self.vc.tableView.reloadData()
                    if data.count == 0{
                        self.vc.noDataStack.isHidden = false
                    }else{
                        self.vc.noDataStack.isHidden = true
                    }
                    self.vc.tableView.isHidden = false
                    refreshControl.endRefreshing()
                default:
                    self.vc.toast(msg: K.ConstantString.unRecognised , time: 2)
                    Utilities.dismissProgressHUD()
                    refreshControl.endRefreshing()
                    break
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                refreshControl.endRefreshing()
                vc.toast(msg: error.localizedDescription , time: 2)
                self.vc.noDataStack.isHidden = false
                self.vc.tableView.isHidden = true
            }
        }
    }

}
