//
//  IMTimeStampContentView.swift
//  LegendTeam
//
//  Created by 梁博 on 21/12/21.
//

import UIKit

class IMTimeStampContentConfig: IMContentBaseConfig {
    override func contentSize(model: SessionBaseModel) -> CGSize {
        //guard let message = model.v2msg else { return .zero }
        
        let contentView = IMTimeStampContentView.init(frame: .zero)
        contentView.contentLabel.text = contentView.contentTextWith(model: model)
        let size = contentView.contentLabel.sizeThatFits(CGSize(width: SessionBaseModel.maxContentWidth, height: CGFloat.greatestFiniteMagnitude))
        let width = size.width
        
        return CGSize(width: width + 16, height: 28)
    }
    
    override func contentViewClass(model: SessionBaseModel) -> IMContentBaseView.Type {
        return IMTimeStampContentView.self
    }
    
    override func cellInsets(model: SessionBaseModel) -> UIEdgeInsets {
        return UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
    }
    
    override func onlyShowContent(model: SessionBaseModel) -> Bool {
        return true
    }
}

class IMTimeStampContentView: IMContentBaseView {
    var effectView: UIVisualEffectView!
    var contentLabel: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        effectView = {
            let v = UIVisualEffectView(effect: UIBlurEffect(style: .light))
            v.alpha = 1
            v.cornerRadius = 4
            insertSubview(v, at: 0)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
        contentLabel = UILabel()
        self.containerView.addSubview(contentLabel)
        contentLabel.font = .t.tbs
        contentLabel.textColor = .white
        contentLabel.numberOfLines = 0
        contentLabel.textAlignment = .center
        contentLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
        }
    }
    
    func contentTextWith(model: SessionBaseModel?) -> String {
        var content = ""

        let second = Int(model?.timeStamp ?? 0)
        content = Date.timerStyle(style: .IMCHAT, second: second)
        return content
    }

    override func refreshModel(model: SessionBaseModel) {
        super.refreshModel(model: model)
        
        contentLabel.text = contentTextWith(model: model)
    }
}
