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

import TYCyclePagerView

import Photos
import PhotosUI

class TSHomeVC : TSBaseVC {

    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()
        let titleImageView = UIImageView.createImageView(imageName: "nav_title_home",contentMode: .scaleToFill)
        
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
        }

        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 }

            let obj = dataArray.safeObj(At: indexPath.section)
            if let bannerModel = obj as? TSHomeBannerDataSectionModel {
                if let items = bannerModel.itemModels.first{
                    kPresentModalVC(target: self, modelVC: TSLiveWallpaperBrowseVC(itemModels: items.items,currentIndex: indexPath.row))
                }
            }else if let liveModel = obj as? TSImageDataSectionModel {
                kPresentModalVC(target: self, modelVC: TSLiveWallpaperBrowseVC(itemModels: liveModel.items,currentIndex: indexPath.row))
            }
        }

        return cp
    }()
    
    var dataArray:[Component] = kImageDataCenterShared.liveBannerArray + kImageDataCenterShared.liveListArray
    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()
        }
   
        collectionComponent.reloadView(with:dataArray)
        
        TSNetworkShard.monitorNetworkPermission { success in
            if success {
                self.collectionComponent.reloadData()
            }
        }
        
    }

}
