//
//  UICollectionView+Refresh.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/2.
//

import UIKit
import MJRefresh

extension UICollectionView {
    func addRefresh(handler: (() -> Void)?) {
        let header = MJRefreshNormalHeader {
            handler?()
        }
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = true
        header.setTitle("", for: .idle)
        header.loadingView?.color = UIColor.white
        mj_header = header
    }
    
    func addLoadMore(handler: (() -> Void)?) {
        let footer = MJRefreshAutoNormalFooter {
            handler?()
        }
        footer.isRefreshingTitleHidden = true
        footer.setTitle("", for: .idle)
        footer.height = 30
        footer.loadingView?.color = UIColor.white
        mj_footer = footer
    }
    
    func endRefreshing(noMore: Bool = false) {
        mj_header?.endRefreshing()
        if noMore {
            mj_footer?.endRefreshingWithNoMoreData()
            (mj_footer as? MJRefreshAutoNormalFooter)?.stateLabel?.isHidden = true
        } else {
            mj_footer?.endRefreshing()
            (mj_footer as? MJRefreshAutoNormalFooter)?.stateLabel?.isHidden = false
        }
    }
}

