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

import UIKit

class IMTipsContentConfig: IMContentBaseConfig {
    override func contentSize(model: SessionBaseModel) -> CGSize {
        guard let message = model.v2msg else { return .zero }
        
        let contentView = IMTipsContentView.init(frame: .zero)
        contentView.contentLabel.text = contentView.contentWith(message: message)
        let size = contentView.contentLabel.sizeThatFits(CGSize(width: SessionBaseModel.maxContentWidth, height: CGFloat.greatestFiniteMagnitude))
        var width = size.width
        if size.height > 30 {
            width = SessionBaseModel.maxContentWidth
        }
        return CGSize(width: width + 16, height: size.height + 8 + 1)// +8
    }
    
    override func contentViewClass(model: SessionBaseModel) -> IMContentBaseView.Type {
        return IMTipsContentView.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 IMTipsContentView: IMContentBaseView {
    var effectView: UIVisualEffectView!
    var contentLabel: LineSpaceLabel!
    
    
    required 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 = LineSpaceLabel()
        addSubview(contentLabel)
        contentLabel.textAlignment = .center
        contentLabel.numberOfLines = 0
        contentLabel.textColor = .white
        let typo = CLSystemToken.typography(token: .tbs)
        contentLabel.config(typo)
        contentLabel.backgroundColor = .clear
        contentLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
        }
    }
    
    func contentWith(message: V2NIMMessage?) -> String! {
        var content: String? = nil
//        let info = IMRemoteUtil.dealRemoteInfo(message: message)
//        let remote = message?.remoteExt as? [String: Any]
//        if info.cellType == .create {
//            let createInfo = IMCreateRemoteInfo.deserialize(from: remote, designatedPath: nil) ?? IMCreateRemoteInfo()
//            content = createInfo.getContent()
//        } else if (info.cellType == .keyword) {
//            var hasKeyword = false
//            if let yidunExt = message?.yidunAntiSpamRes {
//                let yidunInfo = IMYidunExtInfo.deserialize(from: yidunExt, designatedPath: nil)
//                if yidunInfo?.code == 200 {
//                    hasKeyword = true
//                }
//            }
//            if hasKeyword {
//                content = IMTipsContentView.yidunContent(message: message)
//            } else {
//                let createInfo = IMKewordRemoteInfo.deserialize(from: remote, designatedPath: nil) ?? IMKewordRemoteInfo()
//                content = createInfo.getContent()
//            }
//            
//        }
        if content == nil {
            content = message?.text
        }
        return content ?? ""
    }
    
    override func refreshModel(model: SessionBaseModel) {
        super.refreshModel(model: model)
        let content = contentWith(message: model.v2msg)
        contentLabel.text = content
        if content?.count ?? 0 > 60 {
            contentLabel.textAlignment = .left
        } else {
            contentLabel.textAlignment = .center
        }
    }
    
//    static public func yidunContent(message:NIMMessage?) -> String {
//        guard let yidunExt = message?.yidunAntiSpamRes else { return "" }
//        guard let yidunInfo = IMYidunExtInfo.deserialize(from: yidunExt, designatedPath: nil) else { return "" }
//        guard let antInfo = IMYidunAntispamInfo.deserialize(from: yidunInfo.ext, designatedPath: "antispam") else { return "" }
//        guard let labels = antInfo.labels else { return "" }
//        var values = [String]()
//        for label in labels {
//            if let subLabels = label.subLabels {
//                for subInfo in subLabels {
//                    if let detailInfo = subInfo.details {
//                        if let hitInfos = detailInfo.hitInfos {
//                            for hitInfo in hitInfos {
//                                if let value = hitInfo.value, value.count > 0 {
//                                    values.append(value)
//                                }
//                                
//                            }
//                        }
//                    }
//                }
//            }
//        }
//        
//        if values.count > 0 {
//            let keyword = values.joined(separator: "/")
//            return R.string.localizable.contain_sensitive_words.localized([keyword])
//        } else {
//            return R.string.localizable.contain_sensitive_words.localized()
//        }
//    }
}
