MyRewardsProfileInfoTableViewCell.swift 4.76 KB
//
//  MyRewardsProfileInfoTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 23/7/25.
//

import UIKit

protocol MyRewardsProfileInfoTableViewCellDelegate: AnyObject {
    func didTapProfileButton()
}

@objc(MyRewardsProfileInfoTableViewCell)
public class MyRewardsProfileInfoTableViewCell: UITableViewCell {

    // MARK: - IBOutlets

    /// Bold "Rewards" title label (top-left)
    @IBOutlet weak var titleLabel: UILabel!

    /// Horizontally scrollable container for tag pills
    @IBOutlet weak var tagsScrollView: UIScrollView!

    /// Horizontal stack view inside the scroll view; tag pill views are added here dynamically
    @IBOutlet weak var tagsStackView: UIStackView!

    /// Settings / gear button (top-right)
    @IBOutlet weak var profileButton: UIButton!

    // MARK: - Delegate

    weak var delegate: MyRewardsProfileInfoTableViewCellDelegate?

    // MARK: - Private

    private var profileModel: ProfileModel?

    // MARK: - Lifecycle

    public override func awakeFromNib() {
        super.awakeFromNib()

        selectionStyle = .none

        // Title
        titleLabel.font = UIFont(name: "PingLCG-Bold", size: 26)
            ?? UIFont.boldSystemFont(ofSize: 26)
        titleLabel.textColor = UIColor(rgb: 0x1D2023)
        titleLabel.text = "Rewards"

        // Gear button: render image as-is (no tint) so the gray circle style is preserved
        let gearImage = UIImage(named: "settings_circle", in: Bundle.frameworkResourceBundle, compatibleWith: nil)?
            .withRenderingMode(.alwaysOriginal)
        profileButton.setImage(gearImage, for: .normal)
        profileButton.setTitle("", for: .normal)
        profileButton.addTarget(self, action: #selector(profileButtonTapped), for: .touchUpInside)

        // Tags scroll view
        tagsScrollView.showsHorizontalScrollIndicator = false
        tagsScrollView.showsVerticalScrollIndicator = false

        // Tags stack view
        tagsStackView.axis = .horizontal
        tagsStackView.spacing = 9
        tagsStackView.alignment = .center
        tagsStackView.distribution = .fill
    }

    public override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    public override func prepareForReuse() {
        super.prepareForReuse()
        clearTags()
    }

    // MARK: - Actions

    @objc private func profileButtonTapped() {
        delegate?.didTapProfileButton()
    }

    // MARK: - Configuration

    public func configureCell(data: SectionModel?) {
        guard let data = data,
              data.sectionType == .myRewardsProfileInfo else {
            configureDefaultState()
            return
        }

        if let metadata = data.metadata,
           let profile = metadata["profile"] as? ProfileModel {
            configureCell(profile: profile)
        } else {
            configureDefaultState()
        }
    }

    public func configureCell(profile: ProfileModel) {
        self.profileModel = profile
        populateTags(segments: profile._user_segments)
    }

    private func configureDefaultState() {
        self.profileModel = nil
        clearTags()
    }

    // MARK: - Tag Helpers

    /// Removes all tag pill views from the stack view.
    private func clearTags() {
        tagsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
    }

    /// Creates one pill view per segment string and adds them to the stack view.
    private func populateTags(segments: [String]) {
        clearTags()

        for segment in segments where !segment.isEmpty {
            let pill = makePillView(text: segment)
            tagsStackView.addArrangedSubview(pill)
        }
    }

    /// Builds a rounded pill view with the given text.
    private func makePillView(text: String) -> UIView {
        let container = UIView()
        container.backgroundColor = UIColor(rgb: 0xF1F2F4)
        container.layer.cornerRadius = 13
        container.clipsToBounds = true

        let label = UILabel()
        label.text = text
        label.font = UIFont(name: "PingLCG-Regular", size: 12)
            ?? UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor(rgb: 0x00111B)
        label.numberOfLines = 1
        label.translatesAutoresizingMaskIntoConstraints = false

        container.addSubview(label)
        container.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: container.topAnchor, constant: 6),
            label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -6),
            label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 8),
            label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -8)
        ])

        return container
    }
}