//
//  TSHomeTopBannerCell.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/23.
//


import TYCyclePagerView

//MARK: TSHomeTopBannerCell
class TSHomeTopBannerCell : TSBaseCollectionCell , TYCyclePagerViewDelegate, TYCyclePagerViewDataSource,ComponentView {

    let topBarnerItemW = 225.0
    let topBarnerItemH = 486.41
    
    var itemDidSelectedHandler: ((Any?, IndexPath) -> Void)?
    
    lazy var cyclePagerView: TYCyclePagerView = {
        let pagerView = TYCyclePagerView()
        pagerView.isInfiniteLoop = true
        pagerView.autoScrollInterval = 0.0
        pagerView.delegate = self
        pagerView.dataSource = self
        pagerView.layout.layoutType = TYCyclePagerTransformLayoutType.linear
        pagerView.register(TSHomeCyclePagerViewCell.classForCoder(), forCellWithReuseIdentifier: TSHomeCyclePagerViewCell.reuseIdentifier)
        return pagerView
    }()

    lazy var topBarnerView: UIView = {
        let topBarnerView = UIView()
        topBarnerView.addSubview(cyclePagerView)
        cyclePagerView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(topBarnerItemH)
        }

        return topBarnerView
    }()
    
    var itemModelArray:[TSImageDataItemModel] = [TSImageDataItemModel]()
    
    override func creatUI() {
        bgContentView.addSubview(topBarnerView)
        topBarnerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let previewImageView = UIImageView.createImageView(imageName: "iPhone_lock_screen_preview")
        bgContentView.addSubview(previewImageView)
        previewImageView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.equalTo(topBarnerItemW)
            make.height.equalTo(topBarnerItemH)
        }
    }

    func numberOfItems(in pageView: TYCyclePagerView) -> Int {
        return itemModelArray.count
    }
    
    func pagerView(_ pagerView: TYCyclePagerView, cellForItemAt index: Int) -> UICollectionViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: TSHomeCyclePagerViewCell.reuseIdentifier, for: index) as! TSHomeCyclePagerViewCell
        
        if let itemModel = itemModelArray.safeObj(At: index) {
            cell.itemModel = itemModel
        }
        
        return cell
    }
    
    func layout(for pageView: TYCyclePagerView) -> TYCyclePagerViewLayout {
        let layout = TYCyclePagerViewLayout()
        layout.itemSize = CGSize(width:topBarnerItemW, height: topBarnerItemH)
        layout.itemSpacing = 23
        layout.itemHorizontalCenter = true
        layout.layoutType = .linear
        return layout
    }

    func renderView(with object: Any?, component: CollectionViewComponent, attributes: [String : Any]?) {
        if let model = object as? TSHomeBannerDataItemModel {
            if itemModelArray.isEmpty {
                itemModelArray = model.items
                cyclePagerView.reloadData()
                
                itemDidSelectedHandler = component.itemDidSelectedHandler
            }else{
                cyclePagerView.reloadData()
            }
        }
    }
    
    func pagerView(_ pageView: TYCyclePagerView, didSelectedItemCell cell: UICollectionViewCell, at index: Int) {
        if let itemModel = itemModelArray.safeObj(At: index) {
            itemDidSelectedHandler?(itemModel,NSIndexPath(item: index, section: 0) as IndexPath)
        }
    }
}


//MARK: TSHomeCyclePagerViewCell
class TSHomeCyclePagerViewCell: TSBaseCollectionCell {
    static let reuseIdentifier = "TSHomeCyclePagerViewCell"
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = kWapppaperPlaceholderImage
        return imageView
    }()
    
    lazy var liveIconView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = KIconLiveImage
        return imageView
    }()
    
    var itemModel:TSImageDataItemModel = TSImageDataItemModel(){
        didSet{
            imageView.setAsyncImage(urlString: itemModel.imageUrl,placeholder: kWapppaperPlaceholderImage,showLoading: true)
        }
    }
    
    override func creatUI() {
        bgContentView.cornerRadius = 24
        
        bgContentView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        bgContentView.addSubview(liveIconView)
        liveIconView.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.top.equalTo(8)
            make.width.height.equalTo(24)
        }
    }
}

