HustleScoreCell.swift 9.1 KB
//
//  HustleScoreCell.swift
//  AoleiSports
//
//  Created by ilCode on 2024/6/24.
//

import UIKit

/// 球队技术统计cell
class HustleScoreCell: BaseTableViewCell {
    static let reuseIdentifier = "HustleScoreCell"
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = kWhite
        view.corners(radius: 8)
        view.border(borderColor: UIColor.colorWith(hexString: "E9EAEF"), borderWidth: 1)
        view.layer.shadowColor = UIColor.colorWith(hexString: "E9EAEF").cgColor
        view.layer.shadowOffset = CGSize(width: 0, height: 0.5)
        view.layer.shadowOpacity = 0.3
        view.layer.shadowRadius = 8
        view.layer.masksToBounds = false
        return view
    }()
    
    private lazy var hostNameLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .center
        lab.font = kBoldFontSize(16)
        lab.textColor = kMainTitleColor
        lab.numberOfLines = 2
        return lab
    }()
    private lazy var gameLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .center
        lab.font = kBoldFontSize(14)
        lab.textColor = kMainTitleColor
        return lab
    }()
    private lazy var guestNameLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .center
        lab.font = kBoldFontSize(16)
        lab.textColor = kMainTitleColor
        lab.numberOfLines = 2
        return lab
    }()
    private lazy var teamGameStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [hostNameLab, gameLab, guestNameLab])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fillEqually
        stackView.spacing = 2
        return stackView
    }()
    
    private lazy var matchTimeLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .center
        lab.font = kFontSize(14)
        lab.textColor = kMidTitleColor
        return lab
    }()
    
    private lazy var atackView: HostGuestSkillView = {
        let aView = HostGuestSkillView(skillType: .attack)
        return aView
    }()
    private lazy var statusView: HostGuestSkillView = {
        let aView = HostGuestSkillView(skillType: .status)
        return aView
    }()
    private lazy var techView: HostGuestSkillView = {
        let aView = HostGuestSkillView(skillType: .tech)
        return aView
    }()
    
    private lazy var freeLab: UILabel = {
        let lab = UILabel()
        lab.text = "免费查看"
        lab.textAlignment = .center
        lab.font = kFontSize(14)
        lab.textColor = UIColor.colorWith(hexString: "D8B47A")
        return lab
    }()
    
    var model: HadoopMatchModel? {
        didSet {
            hostNameLab.text = model?.hostName
            gameLab.text = model?.comName
            guestNameLab.text = model?.awayName
            matchTimeLab.text = ScoreTools.getFormatMatchTime(time: model?.matchTime, format: "MM-dd HH:mm")
            if let hostModel = model?.homeData, let guestModel = model?.awayData {
                atackView.update(hostValue: hostModel.attackTotal, guestValue: guestModel.attackTotal)
                statusView.update(hostValue: hostModel.statusScore, guestValue: guestModel.statusScore)
                techView.update(hostValue: hostModel.techScore, guestValue: guestModel.techScore)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = kMainBgColor
        
        contentView.addSubview(bgView)
        bgView.addSubview(teamGameStackView)
        bgView.addSubview(matchTimeLab)
        bgView.addSubview(atackView)
        bgView.addSubview(statusView)
        bgView.addSubview(techView)
        bgView.addSubview(freeLab)
        
        bgView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.bottom.equalToSuperview().offset(-5)
        }
        teamGameStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.right.equalToSuperview()
        }
        matchTimeLab.snp.makeConstraints { make in
            make.top.equalTo(teamGameStackView.snp.bottom).offset(5)
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
        }
        atackView.snp.makeConstraints { make in
            make.top.equalTo(matchTimeLab.snp.bottom).offset(5)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(20)
        }
        statusView.snp.makeConstraints { make in
            make.top.equalTo(atackView.snp.bottom).offset(5)
            make.left.right.height.equalTo(atackView)
        }
        techView.snp.makeConstraints { make in
            make.top.equalTo(statusView.snp.bottom).offset(5)
            make.left.right.height.equalTo(atackView)
        }
        freeLab.snp.makeConstraints { make in
            make.top.equalTo(techView.snp.bottom).offset(5)
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HostGuestSkillView: UIStackView {
    enum SkillType {
        case attack
        case status
        case tech
    }
    
    private lazy var hostAttackLab: UILabel = {
        let lab = UILabel()
        lab.font = kFontSize(14)
        lab.textColor = kMidTitleColor
        return lab
    }()
    private lazy var hostProgressView: UIProgressView = {
        let proView = UIProgressView()
        proView.trackTintColor = .clear
        proView.corners(radius: 2)
        proView.transform = CGAffineTransform(rotationAngle: .pi)
        return proView
    }()
    private lazy var hostStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [hostAttackLab, hostProgressView])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fill
        stackView.spacing = 10
        return stackView
    }()
    
    private lazy var skillStatusLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .center
        lab.font = kFontSize(14)
        lab.textColor = kMidTitleColor
        return lab
    }()
    
    private lazy var guestProgressView: UIProgressView = {
        let proView = UIProgressView()
        proView.trackTintColor = .clear
        proView.corners(radius: 2)
        return proView
    }()
    private lazy var guestAttackLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .right
        lab.font = kFontSize(14)
        lab.textColor = kMidTitleColor
        return lab
    }()
    private lazy var guestStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [guestProgressView, guestAttackLab])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fill
        stackView.spacing = 10
        return stackView
    }()
    
    private lazy var skillStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [hostStackView, skillStatusLab, guestStackView])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fill
        stackView.spacing = 20
        return stackView
    }()
    
    init(skillType: SkillType) {
        super.init(frame: .zero)
        
        addSubview(skillStackView)
        
        skillStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        skillStatusLab.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
        }
        hostProgressView.snp.makeConstraints { make in
            make.height.equalTo(6)
        }
        guestProgressView.snp.makeConstraints { make in
            make.height.equalTo(6)
        }
        
        var progressTintColor = UIColor.colorWith(hexString: "2FA3ED")
        var skillStatus = "进攻"
        if skillType == .status {
            progressTintColor = UIColor.colorWith(hexString: "8BC8F5")
            skillStatus = "状态"
        } else if skillType == .tech {
            progressTintColor = UIColor.colorWith(hexString: "C5E3F9")
            skillStatus = "技术"
        }
        hostProgressView.progressTintColor = progressTintColor
        guestProgressView.progressTintColor = progressTintColor
        skillStatusLab.text = skillStatus
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(hostValue: Int?, guestValue: Int?) {
        hostAttackLab.text = "\(hostValue ?? 0)"
        guestAttackLab.text = "\(guestValue ?? 0)"
        hostProgressView.progress = Float(hostValue ?? 0) / Float(100)
        guestProgressView.progress = Float(guestValue ?? 0) / Float(100)
    }
}