//
//  LNOrderRecordListViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/24.
//

import Foundation
import UIKit
import SnapKit
import MJRefresh


extension UIView {
    func pushToOrderRecord() {
        let vc = LNOrderRecordListViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNOrderRecordListViewController: LNViewController {
    private var isLoading = false
    private let tableView = UITableView()
    
    private var orders: [LNOrderRecordItemVO] = []
    private var nextTag: String? = nil
    private let pageSize = 30
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
        
        tableView.mj_header?.beginRefreshing()
    }
}

extension LNOrderRecordListViewController {
    private func loadOrderList() {
        guard !isLoading else { return }
        
        isLoading = true
        
        LNOrderManager.shared.getRecordList(size: pageSize, next: nextTag) { [weak self] list, next in
            guard let self else { return }
            isLoading = false
            self.tableView.mj_header?.endRefreshing()
            if let list {
                if nextTag?.isEmpty == false {
                    orders.append(contentsOf: list)
                } else {
                    orders = list
                }
            }
            self.nextTag = next
            if next.isEmpty {
                tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                tableView.mj_footer?.endRefreshing()
            }
            tableView.reloadData()
        }
    }
}

extension LNOrderRecordListViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        orders.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: LNOrderRecordCell.className, for: indexPath) as! LNOrderRecordCell
        let item = orders[indexPath.row]
        cell.update(item)
        
        return cell
    }
}

extension LNOrderRecordListViewController: LNOrderManagerNotify {
    func onOrderInfoChanged(orderId: String) {
        tableView.reloadData()
    }
}

extension LNOrderRecordListViewController {
    private func setupViews() {
        title = .init(key: "A00171")
        view.backgroundColor = .primary_1
        
        let header = MJRefreshNormalHeader { [weak self] in
            guard let self else { return }
            self.nextTag = nil
            self.loadOrderList()
        }
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = true
        tableView.mj_header = header
        
        let footer = MJRefreshAutoNormalFooter { [weak self] in
            guard let self else { return }
            if self.nextTag == nil { return }
            self.loadOrderList()
        }
        footer.setTitle(.init(key: "A00076"), for: .noMoreData)
        footer.setTitle(.init(key: "A00046"), for: .idle)
        tableView.mj_footer = footer
        tableView.backgroundColor = .clear
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.allowsSelection = false
        tableView.register(LNOrderRecordCell.self, forCellReuseIdentifier: LNOrderRecordCell.className)
        tableView.contentInset = .init(top: 6, left: 0, bottom: 0, right: 0)
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
}

#if DEBUG

import SwiftUI

struct LNOrderRecordListViewControllerPreview: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        LNOrderRecordListViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
}

#Preview(body: {
    LNOrderRecordListViewControllerPreview()
})
#endif

