//
//  CustomTextMessageContentCell.swift
//  ChatExample
//
//  Created by Vignesh J on 01/05/21.
//  Copyright © 2021 MessageKit. All rights reserved.
//

import MessageKit
import UIKit
import SwiftyMarkdown
class CustomTextMessageContentCell: CustomMessageContentCell {
    var messageLabel: UILabel = {
        let label = UILabel.createLabel(font: .font(size: 16),numberOfLines: 0)
        return label
    }()
    
    override func prepareForReuse() {
        super.prepareForReuse()
        //        messageLabel.attributedText = nil
        //        messageLabel.text = nil
    }
    
    override func setupSubviews() {
        super.setupSubviews()
        messageContainerView.addSubview(messageLabel)
    }
    
    override func configure(
        with message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView,
        dataSource: MessagesDataSource,
        and sizeCalculator: CustomLayoutSizeCalculator)
    {
        super.configure(
            with: message,
            at: indexPath,
            in: messagesCollectionView,
            dataSource: dataSource,
            and: sizeCalculator)
        
        guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
            return
        }
        
        let calculator = sizeCalculator as? CustomTextLayoutSizeCalculator
        messageLabel.frame = calculator?.messageLabelFrame(for: message,at: indexPath) ?? .zero
        let textMessageKind = message.kind
        switch textMessageKind {
        case .text(let text), .emoji(let text):
            //            let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView)
            //            messageLabel.textColor = textColor
            
            messageLabel.text = text
            messageLabel.attributedText = SwiftyMarkdown(string: text).attributedString()
            //        extractAndPrintSubstring(from: messageLabel.text ?? "", to: text)
            
            
        case .attributedText(let text):
            messageLabel.attributedText = text
        default:
            break
        }
    }
    
    
    func extractAndPrintSubstring(from sourceString: String, to targetString: String) {
        // 查找 sourceString 在 targetString 中的结束位置
        if let range = targetString.range(of: sourceString) {
            // 获取 sourceString 在 targetString 中的结束位置
            let startIdx = range.upperBound
            // 提取从该位置到 targetString 结束的子字符串
            let extractedSubstring = targetString[startIdx...]
            
            // 将提取的子字符串逐个字符输出
            for character in extractedSubstring {
                print(character)
                messageLabel.text = (messageLabel.text ?? "") + String(character)
                messageLabel.attributedText = SwiftyMarkdown(string: messageLabel.text ?? "").attributedString()
            }
        } else {
            print("未找到 sourceString")
            messageLabel.text = targetString
            messageLabel.attributedText = SwiftyMarkdown(string: targetString).attributedString()
        }
    }
}
