//
//  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: TSCollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = TSCollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [ :])
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, 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
                    model.response.vip = itemModel.dataModel.vip
                    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() {
        view.backgroundColor = "#222222".uiColor
        view.cornersRound(radius: 20, corner: [.topLeft,.topRight])
        edgesForExtendedLayout = []
        setNavBarViewHidden(true)
        setViewBgImageNamed(named: "emojis_view_bg")
        contentView.addSubview(collectionComponent.collectionView)
        reloadView()
    }
    
    func reloadView(){
        collectionComponent.clear()
        collectionComponent.reloadView(with:viewModel.colDataArray)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print("View did layout subviews")
        self.collectionComponent.collectionView.frame = self.view.bounds
    }
}

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 {
//            viewModel = TSEmojisChildViewModel(emojisType: emojisType)
            
            debugPrint("遇到空了")
            reloadView()
        }
        
    }

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

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

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