//
//  TSEmojisChildVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/16.
//

import JXSegmentedView
import JXPagingView

class TSEmojisChildVC: TSBaseVC {
    
    var emojisType:String
    init(emojisType:String) {
        self.emojisType = emojisType
        super.init()
    }
    
    lazy var viewModel: TSEmojisChildViewModel = {
        var viewModel = TSEmojisChildViewModel(emojisType: emojisType)
        return viewModel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //联动滚动回调
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    
    lazy var collectionComponent: CollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = CollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [ :])
        cp.collectionView.frame = self.view.bounds
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: k_Height_TabBar, right: 0)

        cp.didScrollHandler = { [weak self] scrollView in
            guard let self = self else { return }
            listViewDidScrollCallback?(scrollView)
        }
        
        cp.itemDidSelectedHandler = { [weak self] (object, indexPath) in
            guard let self = self else { return }
            
            if let sections = viewModel.colDataArray.safeObj(At: indexPath.section) as? TSEmojisColSectionModel{
                var dataModelArray:[TSGenmojiModel] = []
                for itemModel in sections.items {
                    let model = TSGenmojiModel()
                    model.response.resultUrl = itemModel.dataModel.imageUrl
                    dataModelArray.append(model)
                }
                
                let browseVC = TSSmallIconBrowseVC()
                browseVC.dataModelArray = dataModelArray
                browseVC.currentIndex = indexPath.item
                kPresentModalVC(target: self, modelVC: browseVC,transitionStyle: .crossDissolve)
            }
        }

        return cp
    }()



    override func createView() {
        edgesForExtendedLayout = []
        setNavBarViewHidden(true)
        contentView.addSubview(collectionComponent.collectionView)
        reloadView()
    }
    
    func reloadView(){
        collectionComponent.clear()
        collectionComponent.reloadView(with:viewModel.colDataArray)
    }
}

extension TSEmojisChildVC: JXPagingViewListViewDelegate {
    
    func listScrollView() -> UIScrollView {
        collectionComponent.collectionView
    }

    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        listViewDidScrollCallback = callback
    }

    func listView() -> UIView { view }
    
    func listWillAppear() {
//        print("\(title ?? ""):\(#function)")
        if collectionComponent.collectionView.visibleCells.count == 0 {
            debugPrint("遇到空了")
            reloadView()
        }
        
    }

    func listDidAppear() {
//        print("\(title ?? ""):\(#function)")
    }

    func listWillDisappear() {
//        print("\(title ?? ""):\(#function)")
    }

    func listDidDisappear() {
//        print("\(title ?? ""):\(#function)")
    }
}
