//
//  TSRandomWallpaperVC.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

class TSRandomWallpaperVC: TSBaseVC {
    
    @UserDefault(key: "isShowGuide", defaultValue: true)
    var isShowGuide: Bool
    
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()
        let titleImageView = UIImageView.createImageView(imageName: "nav_title_random",contentMode: .scaleToFill)
        
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
//            make.width.equalTo(185)
//            make.height.equalTo(24)
        }

        return navBarView
    }()
    
    lazy var collectionComponent: CollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = CollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [ :])
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: k_Height_TabBar, right: 0)
        cp.itemDidSelectedHandler = { [weak self] (object, indexPath) in
            guard let self = self else { return }
            
            if let dataModel = object as? TSRandomWallpaperDataItemModel{
                if dataModel.style == .randomBanner {
                    let tutorialsVC = TSRandomWallpaperTutorialsVC()
                    tutorialsVC.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(tutorialsVC, animated: true)
                }else{
                    kPresentModalVC(target: self, modelVC: TSRandomWallpaperBrowseVC(dataModel: dataModel))
                }
            }
        }
        
        cp.itemActionHandler = { [weak self] (object, indexPath) in
            guard let self = self else { return }
            if indexPath.section == 0 {
                isShowGuide = false
                reloadView()
            }
        }
        cp.itemWillDisplayHandler = { cell, obj, IndexPath in

        }
        cp.itemDidEndDisplayingHandler = { cell, obj, indexPath in
      
        }
        
        return cp
    }()
    
    
    override func createView() {
        setViewBgImageNamed(named: "view_main_bg")
        
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(collectionComponent.collectionView)
        collectionComponent.collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
   
        reloadView()
        
        TSNetworkShard.monitorNetworkPermission { success in
            if success {
                self.collectionComponent.reloadData()
            }
        }
    }
    
    func reloadView() {
        collectionComponent.clear()
        if isShowGuide {
            collectionComponent.reloadView(with:kImageDataCenterShared.randomBannerArray + kImageDataCenterShared.randomListArray)
        }else{
            collectionComponent.reloadView(with:kImageDataCenterShared.randomListArray)
        }
    }
}
