//
//  IMTextContentView.swift

//
//  Created by Leon on 2025/8/18.
//

import UIKit
import ActiveLabel
class IMTextContentConfig: IMContentBaseConfig {
    override func contentSize(model: SessionBaseModel) -> CGSize {
        guard model.v2msg != nil else { return .zero }
        
        let contentView = IMTextContentView.init(frame: .zero)
//        contentView.contentLabel.text = contentView.contentWith(model: model)
        //contentView.contentLabel.nim_setText(contentView.contentWith(model: model))
//        var size = contentView.contentLabel.sizeThatFits(CGSize(width: SessionBaseModel.maxBubbleContentWidth, height: CGFloat.greatestFiniteMagnitude))
        
        let string = contentView.contentWith(model: model) ?? " "
        var size = string.size(for: UIFont.t.tbm, size: CGSize(width: SessionBaseModel.maxBubbleContentWidth, height: CGFloat.greatestFiniteMagnitude), lineBreakMode: .byWordWrapping)
        var fixSize = CGSize(width: size.width + 2, height: size.height)
        //  dlog("size:\(fixSize)")
        if fixSize.height < 20 {
            fixSize = CGSize(width: size.width, height: 20)
        }
        return fixSize
    }
    
    override func contentInsets(model: SessionBaseModel) -> UIEdgeInsets {
        return UIEdgeInsets(top: 20, left: 24, bottom: 20, right: 24)
    }
    
    override func cellInsets(model: SessionBaseModel) -> UIEdgeInsets {
        return UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
    }
    
    override func contentViewClass(model: SessionBaseModel) -> IMContentBaseView.Type {
        return IMTextContentView.self
    }
}

class IMTextContentView: IMContentBaseView{
    var contentLabel: ActiveLabel!
    
    required override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        contentLabel = {
            let v = ActiveLabel()
            v.textColor = UIColor.c.ctsn
            v.font = CLSystemToken.font(token: .tbm)
            v.numberOfLines = 0
            //v.enabledTypes = [.url]
            v.textColor = .white
        
            containerView.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
    }
    
    func contentWith(model: SessionBaseModel) -> String! {
        var content: String? = nil
        let message = model.v2msg
        
        if String.realEmpty(str: content) {
            content = message?.text
        }
        return content ?? ""
    }
    
    override func refreshModel(model: SessionBaseModel) {
        super.refreshModel(model: model)
        
        contentLabel.text = ""
        
        if model.shouldShowLeft {
            contentLabel.textColor = .white
        } else {
            contentLabel.textColor = .white
        }
        contentLabel.text = contentWith(model:model)
    }
}
