//
//  TSAIPhotoChildVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/4.
//

import JXSegmentedView
import JXPagingView

class TSAIPhotoChildVC: TSBaseVC {

    //联动滚动回调
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    var style:TSGennerateType
    
    lazy var vm: TSAIPhotoChildVM = {
        let vm = TSAIPhotoChildVM(style: style)
        return vm
    }()
    
    init(style: TSGennerateType) {
        self.style = style
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    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: 20, right: 0)
        cp.itemDidSelectedHandler = { [weak self] (object, indexPath) in
            guard let self = self else { return }
            
            if let sections = vm.colDataArray.safeObj(At: indexPath.section) as? TSColVVMSectionModel{
                var dataModelArray:[TSActionInfoModel] = []
                for itemModel in sections.items {
                    if let model = itemModel.dataModel as? TSActionInfoModel {
                        dataModelArray.append(model)
                    }
                }
                
                let browseVC = TSGeneralPicBrowseVC()
                browseVC.dataModelArray = dataModelArray
                browseVC.currentIndex = indexPath.item
                browseVC.style = style
                kPresentModalVC(target: self, modelVC: browseVC,transitionStyle: .crossDissolve)
            }
        }

        return cp
    }()
    
    override func createView() {
        setNavBarViewHidden(true)
        contentView.addSubview(collectionComponent.collectionView)
    }
    
    func reloadView(){
        kExecuteOnMainThread {
            self.collectionComponent.clear()
            self.collectionComponent.reloadView(with:self.vm.colDataArray)
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.collectionComponent.collectionView.frame = self.view.bounds
    }
    
    override func dealThings() {
        reloadView()
        NotificationCenter.default.addObserver(forName: .kReloadUIData, object: nil, queue: nil) { notification in
            if let userInfo = notification.userInfo as? [String: TSGennerateType], let myEnum = userInfo["TSGennerateType"] {
                if myEnum == self.style {
                    self.vm.setRecentData()
                    self.reloadView()
                }
            }
        }
    }
}


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

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

    func listView() -> UIView { view }
    
    func listWillAppear() {
        
    }

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

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

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