CNLiveVodLandscapeXuanjiView.swift 13.4 KB
//
//  CNLiveVodLandscapeXuanjiView.swift
//  metaCode
//
//  Created by zx on 2025/3/28.
//
//  横屏选集

import Foundation

//专辑选择某一集
typealias CNLiveVodPlayerLandscapeXuanjiUpdateBlock = (_ jishuIndex:Int,_ albumModel:CNLiveVodAlbumListModel)->Void
//看点选择某一集
typealias CNLiveVodPlayerLandscapeXuanjiKanDianSelectBlock = (_ kanDianIndex:Int,_ vodPlayBlockListListModel:CNLiveVodPlayBlockListListModel)->Void

class CNLiveVodLandscapeXuanjiView:UIView, UITableViewDataSource, UITableViewDelegate{//, UICollectionViewDelegate, UICollectionViewDataSource{
    
    var currSeleIdx = 0
//    var selectBlock:CNLiveVodAlbumListCellSelectBlock?
    var xuanjiUpdateBlock:CNLiveVodPlayerLandscapeXuanjiUpdateBlock?
    var vodPlayerLandscapeXuanjiKanDianSelectBlock:CNLiveVodPlayerLandscapeXuanjiKanDianSelectBlock?
//    //实时热榜的列表
//    lazy var albumListArr: NSMutableArray = {
//        var albumListArr = NSMutableArray()
//        return albumListArr
//    }()
//    let kCNLiveVodAlbumListCollectionCellIdentifier = "kCNLiveVodAlbumListCollectionCellIdentifier"
//
//    lazy var realHotCollectionView: UICollectionView = {
//        let layout = UICollectionViewFlowLayout()
//        layout.scrollDirection = .horizontal
//        layout.itemSize = CGSizeMake(50, 50)
//        layout.sectionInset = .init(top: 0, left: 0, bottom: 0, right: 0)
//        layout.minimumLineSpacing = 13.5
//        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
//        cv.delegate = self
//        cv.dataSource = self
//        cv.backgroundColor = .red//.clear
//        cv.showsHorizontalScrollIndicator = false
//        cv.register(CNLiveVodAlbumListCollectionCell.self, forCellWithReuseIdentifier: kCNLiveVodAlbumListCollectionCellIdentifier)
//        return cv
//    }()
    
    let kCNLiveVodXuanjiKandianCellIdentifier = "kCNLiveVodXuanjiKandianCellIdentifier"
    
    lazy var kandianTableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(CNLiveVodXuanjiKandianCell.self, forCellReuseIdentifier: kCNLiveVodXuanjiKandianCellIdentifier)
        tableView.backgroundColor = .clear
        return tableView
    }()
    lazy var tableViewBgView :UIView = {
        let bottomNavView = UIView()
        bottomNavView.isUserInteractionEnabled = true
        return bottomNavView
    }()

    
    //
    var _vodAlbumListBaseModel : CNLiveVodAlbumListBaseModel?
    var vodAlbumListBaseModel : CNLiveVodAlbumListBaseModel? {
        get{
            _vodAlbumListBaseModel
        }
        set{
            _vodAlbumListBaseModel = newValue
            self.setupNewUI(model: vodAlbumListBaseModel)
        }
    }
    
    var _kanDianListArr : NSMutableArray?
    var kanDianListArr : NSMutableArray? {
        get{
            _kanDianListArr
        }
        set{
            _kanDianListArr = newValue!
            self.setupNewUI(kanDianListArr: _kanDianListArr!)
        }
    }


    //MARK: - 懒加载
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = Font_14
        titleLbl.textColor = HexColor("#FFFFFF")
        titleLbl.text = "选集"
        return titleLbl
    }()
    
    lazy var bgScrollView :UIScrollView = {
        let bgScrollView = UIScrollView()
        bgScrollView.showsVerticalScrollIndicator = false
        return bgScrollView
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = HexColor("#000000", alpha: 0.7)
        self.isUserInteractionEnabled = true
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    var footerRefreshView: MJRefreshAutoNormalFooter!
    var page = 1//当前页数
    
    func initUI(){
        
        self.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(31)
            make.centerX.equalToSuperview()
        }
        
        let leftPadding:CGFloat = 158.0
        let topPadding:CGFloat = 25.0
        let WH:CGFloat = 50.0
        let padding:CGFloat = 13.5
        let maxH = max(KSreenHeight, KSreenWidth)
        var containerWidth: CGFloat = maxH-158.0*2
        // 计算不包括最后一个方块间距时一行能放置的方块数量
        let maxBlocksWithoutLastSpacing = Int(containerWidth / (WH + padding))
        let hangMaxNum = maxBlocksWithoutLastSpacing//一行几块
        containerWidth = CGFloat(hangMaxNum)*(WH+padding)-padding

        self.addSubview(self.bgScrollView)
        bgScrollView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(topPadding)
            make.centerX.equalToSuperview()
            make.width.equalTo(containerWidth)
            make.bottom.equalToSuperview().offset(-50)
        }
        bgScrollView.backgroundColor = .clear
        
        self.addSubview(self.tableViewBgView)
        tableViewBgView.snp.makeConstraints { make in
            make.top.bottom.left.right.equalToSuperview()
        }
        tableViewBgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapAction)))
        
        tableViewBgView.addSubview(self.kandianTableView)
        kandianTableView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(topPadding)
            make.centerX.equalToSuperview()
            make.width.equalTo(containerWidth)
            make.bottom.equalToSuperview().offset(-50)
        }
        
        

    }
    
    //看点列表
    func setupNewUI(kanDianListArr:NSMutableArray){
//        CNLiveVodPlayblockListListModel
        self.bgScrollView.isHidden = true
        self.kandianTableView.isHidden = false
        self.kandianTableView.reloadData()
    }
    // MARK: - UITableViewDelegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.kanDianListArr?.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if let listModel = self.kanDianListArr?[indexPath.row] as? CNLiveVodPlayBlockListListModel{
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveVodXuanjiKandianCellIdentifier) as? CNLiveVodXuanjiKandianCell
            else {
                let cell = CNLiveVodXuanjiKandianCell()
                return cell
            }
            if self.currSeleIdx == indexPath.row{
                cell.isSelect = true
            }else{
                cell.isSelect = false
            }
            cell.vodPlayBlockListListModel = listModel

            cell.vodXuanjiKandianCellClickBlock = {[weak self]   vodPlayBlockListListModel in
                guard let self = self else { return }
                self.vodPlayerLandscapeXuanjiKanDianSelectBlock?(indexPath.row,vodPlayBlockListListModel ?? CNLiveVodPlayBlockListListModel())
                self.currSeleIdx = indexPath.row
                self.kandianTableView.reloadData()
                self.isHidden = true
            }
            return cell
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let imgH:CGFloat = 65.0
        return imgH+10.0
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let listModel = self.kanDianListArr?[indexPath.row] as? CNLiveVodPlayBlockListListModel{
            self.vodPlayerLandscapeXuanjiKanDianSelectBlock?(indexPath.row,listModel)
        }
    }
    
    //专辑列表
    func setupNewUI(model:CNLiveVodAlbumListBaseModel?){
//        self.albumListArr = NSMutableArray(array: model.list)
//        self.realHotCollectionView.reloadData()
        
        self.bgScrollView.isHidden = false
        self.kandianTableView.isHidden = true

        let WH:CGFloat = 50.0
        let padding:CGFloat = 13.5
        let maxH = max(KSreenHeight, KSreenWidth)
        let minH = max(KSreenHeight, KSreenWidth)
        var containerWidth: CGFloat = maxH-158.0*2
        // 计算不包括最后一个方块间距时一行能放置的方块数量
        let maxBlocksWithoutLastSpacing = Int(containerWidth / (WH + padding))
        let hangMaxNum = maxBlocksWithoutLastSpacing//一行几块
        containerWidth = CGFloat(hangMaxNum)*(WH+padding)-padding
        //lieIndex 当前是contents的第0列还是第1列
        var lieIndex = 0
        //hangIndex 当前是第几行,1...n
        var hangIndex = 1

        var maxContentHeight:CGFloat = self.bgScrollView.height
        for i in 0..<(model?.list.count ?? 0){
            let listMdl = model?.list[i]
            let rowH = WH+padding
            let imgX = CGFloat(lieIndex)*(WH+padding)
            let imgY = rowH*CGFloat(hangIndex-1)
            
            let imgBottom = imgY+WH
            if imgBottom > maxContentHeight{
                maxContentHeight = imgBottom
            }
            self.bgScrollView.contentSize = CGSize(width: containerWidth, height: maxContentHeight)
            let expandBtn = UIButton.init(type: .custom)
            expandBtn.frame = CGRect(x: imgX, y: imgY, width: WH, height: WH)
            
            if i == self.currSeleIdx{
                expandBtn.backgroundColor = HexColor("#D3E4FF")
                expandBtn.layer.borderWidth = 0.65
                expandBtn.layer.borderColor = HexColor("#478BFF")?.cgColor
                expandBtn.isSelected = true
            }else{
                expandBtn.backgroundColor = HexColor("#000000", alpha: 0.5)
                expandBtn.layer.borderWidth = 0
                expandBtn.layer.borderColor = HexColor("#000000")?.cgColor
                expandBtn.isSelected = false
            }
            expandBtn.setTitle("\(i+1)", for: .normal)
            expandBtn.setTitle("\(i+1)", for: .selected)
            expandBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
            expandBtn.setTitleColor(HexColor("#478BFF"), for: .selected)
            expandBtn.titleLabel?.font = Font_18
            expandBtn.layer.cornerRadius = 12
            expandBtn.clipsToBounds = true
            expandBtn.tag = 100+i
            expandBtn.addTarget(self, action: #selector(xuanjiBtnAction), for: .touchUpInside)
            self.bgScrollView.addSubview(expandBtn)
            
            let layerView = UIImageView()
            layerView.image = UIImage(named: "home_player_pay")
            layerView.frame = CGRect(x: 0, y: 0, width: 26, height: 15)
            expandBtn.addSubview(layerView)
            
            let isPaid = CNLivePlayerTool.isShowPaidIcon(product: listMdl?.product)
            if isPaid{
                layerView.isHidden = false
            }else{
                layerView.isHidden = true
            }
//            if listMdl.product?.productType == "2" || listMdl.product?.productType == "5"{//付费
//                layerView.isHidden = false
//            }else{
//                if listMdl.product?.check == true {
//                    //免费或者付过费了
//                    layerView.isHidden = true
//                } else {
//                    if listMdl.product?.productType == "4" {
//                        //这里表示可以进行试听
//                        layerView.isHidden = true
//                    } else {
//                        layerView.isHidden = true
//                    }
//                }
//            }
            
            if lieIndex < hangMaxNum-1{
                lieIndex = lieIndex+1
            }else{
                lieIndex = 0
                hangIndex += 1
            }
        }
        
    }
    
//    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        self.albumListArr.count
//    }
//    
//    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCNLiveVodAlbumListCollectionCellIdentifier, for: indexPath) as! CNLiveVodAlbumListCollectionCell
//        if self.currSeleIdx == indexPath.row{
//            //当前cell选中
//            cell.isJiShuSelected = true
//        }else{
//            //不选中
//            cell.isJiShuSelected = false
//        }
//        cell.num = indexPath.row
//        cell.vodAlbumListModel = self.albumListArr[indexPath.row] as? CNLiveVodAlbumListModel
//        return cell
//    }
//    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        if let vodAlbumListModel = self.albumListArr[indexPath.row] as? CNLiveVodAlbumListModel{
//            //点击某集,刷新页面
//            self.selectBlock?(vodAlbumListModel, indexPath.row, self.albumListArr)
//            self.currSeleIdx = indexPath.row
//            self.realHotCollectionView.reloadData()
//            
//        }
//    }

    @objc func xuanjiBtnAction(btn:UIButton){
        let index = btn.tag-100
        if let listModel = self.vodAlbumListBaseModel?.list[index] as? CNLiveVodAlbumListModel{
            self.xuanjiUpdateBlock?(index,listModel)
        }
    }
    @objc func tapAction(){
        self.isHidden = true
    }
    
}