//
//  ShopCategoryVM.swift
//  WOKA
//
//  Created by MacBook Pro on 22/07/24.
//

import UIKit
import Alamofire

class ShopCategoryVM{
    
    weak var vc : ShopCategoryVC!
    var superCatID : Int?
    
    var categoryData = [ShopCategoryDM.ResultData]()
    var cartButton: UIBarButtonItem!

    func initView(){
        setupCell()
        addGradient()
        getCategory()
        
        cartButton = BadgeBarBtn.shared.setupCartButton(target: self, action: #selector(cartButtonTapped))
        self.vc.navigationItem.rightBarButtonItem = cartButton
    }
    
    @objc func cartButtonTapped(){
        PersistentStorage.shared.addShopCount(postID: 0)

        if AuthFunc.shareInstance.guestUserLoginPopUp() { return}

        let sb = UIStoryboard(name: K.StoryBoard.cart, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Cart.cartListVC) as! CartListVC
        self.vc.navigationController?.pushViewController(vcPush, animated: true)
    }
    
    func setupCell(){
        vc.tableView.register(UINib(nibName: K.CellIdentifier.Shop.shopListingCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Shop.shopListingCell)
        vc.tableView.delegate = vc.self
        vc.tableView.dataSource = vc.self
    }
    
    func addGradient(){
        self.vc.title = "SHOP".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        let color1 = #colorLiteral(red: 0.6745098039, green: 0.6235294118, blue: 0.1725490196, alpha: 1)
        let color2 = #colorLiteral(red: 0.5450980392, green: 0.6745098039, blue: 0.1725490196, alpha: 1)
        
        vc.view.applyGradient(colors: [color2, color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0))
    }
    

    
    // MARK: - Get SuperCategory
    
    func getCategory(){
        guard let superCatID else{return}
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
        let params : Parameters = ["module_id" : "5",
                                   "super_category_id" : superCatID]
        
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Shop.category_listing, method: .post,parameters: params,headers: headers) { [weak self](result : Result<BaseResponseModel<ShopCategoryDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                guard let self else{
                    Utilities.dismissProgressHUD()
                    return
                }
                switch data.success{
                case 0:
                    /*
                     Error
                     */
                    Utilities.dismissProgressHUD()
                    Utilities.alertWithBtnCompletion(title: "Error", msgBody: data.message ?? K.ConstantString.unRecognised, okBtnStr: "Retry?", vc: self.vc) { isDone in
                        if isDone{
                            self.getCategory()
                        }
                    }
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let data = data.data?.result else{return}
                    self.categoryData = data
                    self.vc.tableView.reloadData()
                default:
                    Utilities.dismissProgressHUD()
                    break
                }
            case .failure(let error):
                guard let self else{
                    Utilities.dismissProgressHUD()
                    return
                }
                Utilities.dismissProgressHUD()
                Utilities.alertWithBtnCompletion(title: "Error", msgBody: error.localizedDescription, okBtnStr: "Retry?", vc: self.vc) { isDone in
                    if isDone{
                        self.getCategory()
                    }
                }
            }
        }
    }
}
