CNLiveVodPlayShopCell.swift 6.61 KB
//
//  CNLiveVodPlayShopCell.swift
//  metaCode
//
//  Created by zx on 2025/3/4.
//
//  点播-电商

import Foundation

//typealias CNLiveVodShopCellExpandBlock = (_ vodPlayShopModels:[CNLiveVodPlayShopModel])->Void
typealias CNLiveVodShopCellExpandBlock = (_ vodPlayShopModels:Any)->Void

class CNLiveVodPlayShopCell:UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource{
    var expandBlock:CNLiveVodShopCellExpandBlock?

    var _vodPlayShopModels : [CNLiveVodPlayShopModel]?
    var vodPlayShopModels : [CNLiveVodPlayShopModel]? {
        get{
            _vodPlayShopModels
        }
        set{
            _vodPlayShopModels = newValue!
            self.setupNewUI(models: vodPlayShopModels!)
        }
    }
    //默认电商商品list
    var _albumModels : [CNLiveAlbumModel]?
    var albumModels : [CNLiveAlbumModel]? {
        get{
            _albumModels
        }
        set{
            _albumModels = newValue!
            self.setupNewUI(goodsModels: _albumModels!)
        }
    }


    lazy var albumListArr: NSMutableArray = {
        var albumListArr = NSMutableArray()
        return albumListArr
    }()
    let kCNLiveVodPlayShopCollectionCellIdentifier = "kCNLiveVodPlayShopCollectionCellIdentifier"

    lazy var realHotCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSizeMake(235, 90)
        layout.sectionInset = .init(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumLineSpacing = 5
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.delegate = self
        cv.dataSource = self
        cv.backgroundColor = .white
        cv.showsHorizontalScrollIndicator = false
        cv.register(CNLiveVodPlayShopCollectionCell.self, forCellWithReuseIdentifier: kCNLiveVodPlayShopCollectionCellIdentifier)
        return cv
    }()

    //MARK: - 懒加载
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = BoldFont_18
        titleLbl.textColor = HexColor("#000102")
        titleLbl.text = "相关推荐"
        return titleLbl
    }()
    //more
    lazy var detailBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 24)
        expandBtn.setImage(UIImage(named: "home_player_more"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_more"), for: .highlighted)
        expandBtn.addTarget(self, action: #selector(detailBtnAction), for: .touchUpInside)
        return expandBtn
    }()


    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    private func setupUI() {
        contentView.backgroundColor = .white
        self.contentView.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(15)
            make.width.lessThanOrEqualToSuperview().offset(-80)
        }
        
        self.contentView.addSubview(self.detailBtn)
        detailBtn.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.top)
            make.width.equalTo(50)
            make.right.equalToSuperview()
            make.height.equalTo(24)
        }
        
        self.contentView.addSubview(self.realHotCollectionView)
        realHotCollectionView.snp.makeConstraints { make in
            make.left.equalTo(self.titleLabel.snp.left)
            make.top.equalTo(self.titleLabel.snp.bottom).offset(15)
            make.width.equalTo(KSreenWidth-20)
            make.height.equalTo(90)
        }

        self.contentView.clipsToBounds = true
    }
    
    func setupNewUI(goodsModels:[CNLiveAlbumModel]){
        self.albumListArr = NSMutableArray(array: goodsModels)
        self.realHotCollectionView.reloadData()
    }

    func setupNewUI(models:[CNLiveVodPlayShopModel]){
        self.albumListArr = NSMutableArray(array: models)
        self.realHotCollectionView.reloadData()
    }

    @objc func detailBtnAction(){
        if let vodPlayShopModels = self.vodPlayShopModels,self.vodPlayShopModels != nil,self.vodPlayShopModels?.count ?? 0 > 0{
            self.expandBlock?(vodPlayShopModels)
        }else if let albumModels = self.albumModels,self.albumModels != nil ,self.albumModels?.count ?? 0 > 0{
            self.expandBlock?(albumModels)
        }else{
            
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.albumListArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveVodPlayShopCollectionCellIdentifier, for: indexPath) as! CNLiveVodPlayShopCollectionCell

        if let model = self.albumListArr[indexPath.row] as? CNLiveVodPlayShopModel{
            cell.vodPlayShopModel = model
        }else if let model = self.albumListArr[indexPath.row] as? CNLiveAlbumModel{
            cell.albumModel = model
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vodPlayShopModel = self.albumListArr[indexPath.row] as? CNLiveVodPlayShopModel{
            //相关推荐的商品
            
            let urlString = hubilieDSHost+"pages/main/detail/detail?"+"id=\(vodPlayShopModel.id)&uid=\(UserInfoManager.shared.userInfo.uid)"
            
            CNLiveDownSlideTool.pushWebAndVodVCWithContentsMdl(model: kCNDownSlideContentsModelModelProduct , contentId: "" , pid: "" , shareUrl: "" , thirdUrl: urlString , title: vodPlayShopModel.title , s_to: "" ,trailerId: "" , controller: nil, slideModel: nil)
        }else if let albumModel = self.albumListArr[indexPath.row] as? CNLiveAlbumModel{
            //默认商品
            let urlString = hubilieDSHost+"pages/main/detail/detail?"+"id=\(albumModel.contentId)&uid=\(UserInfoManager.shared.userInfo.uid)"
            
            CNLiveDownSlideTool.pushWebAndVodVCWithContentsMdl(model: kCNDownSlideContentsModelModelProduct , contentId: "" , pid: "" , shareUrl: "" , thirdUrl: urlString , title: albumModel.title , s_to: "" ,trailerId: "" , controller: nil, slideModel: nil)
        }
    }

    
}