//
//  LNVisitorItemCell.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/12.
//

import Foundation
import UIKit
import SnapKit

class LNVisitorItemCell: UITableViewCell {
    private let avatar = UIImageView()
    private let onlineView = LNOnlineView()
    private let nameLabel = UILabel()
    private let genderView = LNGenderView()
    private let visitTimeLabel = UILabel()
    private let descLabel = UILabel()
    
    private var curItem: LNVisitorItemVO?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ item: LNVisitorItemVO) {
        avatar.showAvatar(item.avatar)
        onlineView.isHidden = !item.online
        nameLabel.text = item.nickname
        
//        visitTimeLabel.text = Double(item.visitTime).tencentIMTimeDesc
//        visitTimeLabel.isHidden = item.visitTime == 0
        visitTimeLabel.isHidden = true
        
        genderView.update(item.gender, item.age)
        
        curItem = item
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNVisitorItemCell {
    private func setupViews() {
        backgroundColor = .clear
        
        avatar.layer.cornerRadius = 20
        avatar.clipsToBounds = true
        contentView.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.leading.equalToSuperview().offset(21)
            make.top.equalToSuperview().offset(5)
            make.bottom.equalToSuperview().offset(-25)
        }
        
        onlineView.offset = 5
        contentView.addSubview(onlineView)
        onlineView.snp.makeConstraints { make in
            make.edges.equalTo(avatar).inset(-2)
        }
        
        let tips = buildTipsView()
        contentView.addSubview(tips)
        tips.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let infoView = buildInfoView()
        contentView.addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.leading.equalTo(avatar.snp.trailing).offset(13)
            make.centerY.equalTo(avatar)
            make.trailing.equalTo(tips.snp.leading).offset(-12)
        }
        
        contentView.onTap { [weak self] in
            guard let self else { return }
            guard let curItem else { return }
            pushToProfile(uid: curItem.userNO, scene: .visitor)
        }
    }
    
    private func buildInfoView() -> UIView {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 4
        
        stackView.addArrangedSubview(buildNameView())
        stackView.addArrangedSubview(buildDescView())
        
        return stackView
    }
    
    private func buildNameView() -> UIView {
        let container = UIView()
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_5
        container.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        container.addSubview(genderView)
        genderView.snp.makeConstraints { make in
            make.leading.equalTo(nameLabel.snp.trailing).offset(4)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        return container
    }
    
    private func buildDescView() -> UIView {
        visitTimeLabel.isHidden = true
        visitTimeLabel.font = .body_xs
        visitTimeLabel.textColor = .text_3
        
        return visitTimeLabel
    }
    
    private func buildTipsView() -> UIView {
        descLabel.text = .init(key: "B00092")
        descLabel.font = .body_xs
        descLabel.textColor = .text_4
        descLabel.setContentHuggingPriority(.required, for: .horizontal)
        descLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        return descLabel
    }
}
