//
//  MyOrdersVC.swift
//  WOKA
//
//  Created by MacBook Pro on 10/06/24.
//

import UIKit

class MyOrdersVC: UIViewController {
    
    var vm = MyOrdersVM()
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var noDataStack: UIStackView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setColor(color: .black)
        navigationController?.setNavigationBarHidden(false, animated: animated)
        
        // Update the badge label with the current cart data
        if let button = vm.cartButton.customView as? UIButton {
            BadgeBarBtn.shared.updateBadge(for: button, with: CartDataCache.cartListData.count.toString())
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        self.navigationController?.setColor(color: .black)
    }

    @IBAction func retryBtnTapped(_ sender: UIButton) {
        Utilities.startProgressHUD()
        vm.getOrders()
    }
}

// MARK: - TableView DataSource , Delegates

extension MyOrdersVC : TableViewSRC{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return vm.orderData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.SideBarNav.myOrderCell) as! MyOrderCell
        let data = vm.orderData[indexPath.row]
        cell.setData(data: data)
        
        cell.btnTapped = { [weak self] in
            guard let self else{return}
            PersistentStorage.shared.addShopCount(postID: 0)
            let sb = UIStoryboard(name: K.StoryBoard.sideBarNav, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.SideBarNav.myOrderDetailsVC) as! MyOrderDetailsVC
            let orderID = vm.orderData[indexPath.row].orderID
            vcPush.orderID = orderID
            self.navigationController?.present(vcPush, animated: true)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let sb = UIStoryboard(name: K.StoryBoard.sideBarNav, bundle: nil)
//        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.SideBarNav.myOrderDetailsVC) as! MyOrderDetailsVC
//        let orderID = vm.orderData[indexPath.row].orderID
//        vcPush.orderID = orderID
//        self.navigationController?.present(vcPush, animated: true)
    }
}
