//
//  MyOrderDetailsVC.swift
//  WOKA
//
//  Created by MacBook Pro on 08/08/24.
//

import UIKit
import Alamofire

class MyOrderDetailsVC: UIViewController {

    @IBOutlet weak var orderIDNumber: UILabel!
    @IBOutlet weak var airWayBillNo: UILabel!
    @IBOutlet weak var status: UILabel!
    @IBOutlet weak var expectedDate: UILabel!
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var noDataStack: UIStackView!
    @IBOutlet weak var mainStack: UIStackView!
    
    var orderID : String?
    var data : OrderDetailsDM.ResultData?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupCell()

        if let orderID{
            getOrdersDetails(orderID: orderID)
        }
    }
    
    func setupCell(){
        tableView.register(UINib(nibName: K.CellIdentifier.SideBarNav.myOrderDetailsCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.SideBarNav.myOrderDetailsCell)
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    @IBAction func retryBtnTapped(_ sender: UIButton) {
        if let orderID{
            getOrdersDetails(orderID: orderID)
        }
    }
    
    // MARK: - Get MyORders
    
    func getOrdersDetails(orderID : String){
        Utilities.startProgressHUD()
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
                                     "access-token": AuthFunc.shareInstance.getAccessToken()]
        let url = "\(APIEndPoints.SideBarNav.order_status_track )/\(orderID)"
        NetworkManager.shareInstance.apiRequest(url: url, method: .get, headers : headers) { [weak self](result : Result<BaseResponseModel<OrderDetailsDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    self?.toast(msg: K.ConstantString.unRecognised, time: 1.5)
                    self?.noDataStack.isHidden = false
                    return
                case 1:
                    Utilities.dismissProgressHUD()
                    guard let data = data.data?.result?.first , let self else{return}
                    self.data = data
                    self.tableView.reloadData()
                    setData()
                    self.mainStack.isHidden = false
                    self.tableView.isHidden = false
                    self.noDataStack.isHidden = true
                default:
                    break
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                self?.noDataStack.isHidden = false
                self?.toast(msg: error.localizedDescription, time: 2)
            }
        }
    }
    
    func scrollToBottom(animated: Bool) {
        let bottomOffset = CGPoint(x: 0, y: tableView.contentSize.height - tableView.bounds.size.height)
        if bottomOffset.y > 0 {
            tableView.setContentOffset(bottomOffset, animated: animated)
        }
    }
    
    func setData(){
        if let data{
            self.orderIDNumber.text = orderID ?? "NA"
            self.airWayBillNo.text = data.awbno
            self.status.text = data.shipmentLatestStatus
            self.expectedDate.text = data.edd
        }
    }
}

// MARK: - TableView DataSource , Delegates

extension MyOrderDetailsVC : TableViewSRC{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data?.scanDetail?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.SideBarNav.myOrderDetailsCell) as! MyOrderDetailsCell
        
        if let data = self.data?.scanDetail?[indexPath.row]{
            cell.setData(data: data)
        }
        return cell
    }
}
