CNLiveVodXuanjiKandianCell.swift 6.78 KB
//
//  CNLiveVodXuanjiKandianCell.swift
//  metaCode
//
//  Created by zx on 2025/4/9.
//  选集-看点列表

import Foundation

typealias CNLiveVodXuanjiKandianCellClickBlock = (_ model:CNLiveVodPlayBlockListListModel?)->Void

class CNLiveVodXuanjiKandianCell:UITableViewCell{
    var isSelect:Bool = false
    
    var vodXuanjiKandianCellClickBlock : CNLiveVodXuanjiKandianCellClickBlock?
    //slideModel
    var _vodPlayBlockListListModel : CNLiveVodPlayBlockListListModel?
    var vodPlayBlockListListModel : CNLiveVodPlayBlockListListModel? {
        get{
            _vodPlayBlockListListModel
        }
        set{
            _vodPlayBlockListListModel = newValue!
            self.setupNewUI(model: _vodPlayBlockListListModel!)
        }
    }
    
    //MARK: - 懒加载
    lazy var totalBtn: UIButton = {
        let  expandBtn = UIButton(type: .custom)
        expandBtn.backgroundColor = HexColor("#D3E4FF")//nor EEEEEE,sele #D3E4FF
        expandBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
        expandBtn.setTitleColor(HexColor("#478BFF"), for: .selected)
        expandBtn.layer.cornerRadius = 9
        expandBtn.clipsToBounds = true
//        expandBtn.isUserInteractionEnabled = false
        expandBtn.addTarget(self, action: #selector(totalBtnAction), for: .touchUpInside)
        return expandBtn
    }()
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.textColor = .white
        titleLabel.textAlignment = .left
        titleLabel.font = Font_18
        return titleLabel
    }()
    lazy var timeTitleLabel: UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.textColor = .white
        titleLabel.textAlignment = .left
        titleLabel.font = Font_18
        return titleLabel
    }()
    //付费角标
    lazy var payIconView :UIImageView = {
        let layerView = UIImageView()
        layerView.image = UIImage(named: "home_player_pay")
        layerView.frame = CGRect(x: 0, y: 0, width: 26, height: 15)
        return layerView
    }()

    
    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() {
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        self.contentView.addSubview(self.totalBtn)
        totalBtn.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
        }

        self.totalBtn.addSubview(self.timeTitleLabel)
        timeTitleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-30)
        }
        
        self.totalBtn.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(25)
            make.centerY.equalToSuperview()
            make.right.lessThanOrEqualTo(self.timeTitleLabel.snp.left).offset(-10)
        }

        self.totalBtn.addSubview(self.payIconView)
        payIconView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.width.equalTo(26)
            make.height.equalTo(15)
        }
         
    }
    
    func setupNewUI(model:CNLiveVodPlayBlockListListModel){
        
        self.titleLabel.text = model.title
        if let formattedTime = convertSecondsToFormattedTime(secondsString: model.duration) {
            print(formattedTime) // 输出: "05:59"
            self.timeTitleLabel.text = formattedTime
        } else {
            print("Invalid seconds string")
            self.timeTitleLabel.text = "00:00"
        }

        self.totalBtn.isSelected = isSelect
        if isSelect{
            //当前选中
            self.titleLabel.textColor = HexColor("#478BFF")
            self.timeTitleLabel.textColor = HexColor("#478BFF")
            self.totalBtn.backgroundColor = HexColor("#D3E4FF")
        }else{
            //未选中
            self.titleLabel.textColor = HexColor("#FFFFFF")
            self.timeTitleLabel.textColor = HexColor("#FFFFFF")
            self.totalBtn.backgroundColor = HexColor("#000000", alpha: 0.5)

        }
        
//        if model.product?.albumProductType.count ?? 0 > 0{
//            //专辑
//            if model.product?.albumProductType == "2" || model.product?.albumProductType == "5"{
//                //人民币 ,专辑付费
//                
//            }else{
//                //免费
//            }
//        }else{
//            //单集
//            if model.product?.productType.count ?? 0 > 0{
//                //单集
//                if model.product?.productType == "2"{
//                    //人民币付费
//                    
//                }else{
//                    //免费
//                    
//                }
//            }else{
//                //免费
//                
//            }
//        }
        let isPaid = CNLivePlayerTool.isShowPaidIcon(product: model.product)
        if isPaid{
            payIconView.isHidden = false
        }else{
            payIconView.isHidden = true
        }
//        if model.product?.productType == "2" || model.product?.productType == "5"{//付费
//            payIconView.isHidden = false
//        }else{
//            if model.product?.check == true {
//                //免费或者付过费了
//                payIconView.isHidden = true
//            } else {
//                if model.product?.productType == "4" {
//                    //这里表示可以进行试听
//                    payIconView.isHidden = true
//                } else {
//                    payIconView.isHidden = true
//                }
//            }
//        }
            
    }
    func convertSecondsToFormattedTime(secondsString: String) -> String? {
        if let seconds = Int(secondsString) {
            let hours = seconds / 3600
            let minutes = (seconds % 3600) / 60
            let secondsRemainder = seconds % 60
            
            if hours == 0 {
                // 如果小时为0,则返回mm:ss格式
                return String(format: "%02d:%02d", minutes, secondsRemainder)
            } else {
                // 如果小时不为0,则返回hh:mm:ss格式
                return String(format: "%02d:%02d:%02d", hours, minutes, secondsRemainder)
            }
        }
        return nil // 如果字符串不能转换为整数,则返回nil
    }
    @objc func totalBtnAction(btn: UIButton){
        self.vodXuanjiKandianCellClickBlock?(self.vodPlayBlockListListModel)

    }
    
}