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

import MessageKit
import UIKit

class TSTextMessageContentCell: TSMessageContentCell {
    var messageLabel: UILabel = {
        let label = UILabel.createLabel(font: .font(size: 16),numberOfLines: 0)
        label.lineBreakMode = .byWordWrapping
        return label
    }()
    
    lazy var activityIndicator: UIActivityIndicatorView = {
        activityIndicator = UIActivityIndicatorView(style: .medium)
        activityIndicator.color = .white
        activityIndicator.isHidden = true
        return activityIndicator
    }()
    
    lazy var refreshBtn: UIButton = {
        let refresh = UIButton.createButton(image: UIImage(named: "aichat_refresh")) { [weak self]  in
            guard let self = self else { return }

        }
        return refresh
    }()
    

    override func prepareForReuse() {
        super.prepareForReuse()
    }
    
    override func setupSubviews() {
        super.setupSubviews()
        
        let labelEdge = TSTextLayoutSizeCalculator.cellMessagelabelEdge
        messageCenterContainerView.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { make in
            make.edges.equalTo(labelEdge)
            make.width.lessThanOrEqualTo(300) // 设置最大宽度为 300
            make.width.greaterThanOrEqualTo(24.0)
            make.height.greaterThanOrEqualTo(24.0)
        }
        
        messageCenterContainerView.addSubview(activityIndicator)
        activityIndicator.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(12)
            make.width.height.equalTo(24.0)
        }
        
    }
    
    override func configure(
        with message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView,
        dataSource: MessagesDataSource,
        and sizeCalculator: TSLayoutSizeCalculator)
    {
        super.configure(with: message, at: indexPath, in: messagesCollectionView, dataSource: dataSource, and: sizeCalculator)
        guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
            return
        }
        
        //更新frame
        let calculator = sizeCalculator as? TSTextLayoutSizeCalculator
        //全面控制 label size
//        let labelFrame = calculator?.messageLabelSize(for: message, at: indexPath,fromCurrentSender: dataSource.isFromCurrentSender(message: message)) ?? TSTextLayoutSizeCalculator.cellMessagelabelMinSize
//        messageLabel.snp.updateConstraints { make in
//            make.width.equalTo(labelFrame.width)
//            make.height.equalTo(labelFrame.height)
//        }
        
        
        let isCurrentSender = dataSource.isFromCurrentSender(message: message)

        //label 高度自适应
        let maxWidth = calculator?.cellMessagelabelMaxWidth(fromCurrentSender: isCurrentSender) ?? TSTextLayoutSizeCalculator.cellMessagelabelMinSize.width
        messageLabel.snp.updateConstraints { make in
            make.width.lessThanOrEqualTo(maxWidth)
        }
        
        if let msgModel = message as? TSChatMessage {
            //显示旋转的动画
            switch msgModel.sendState {
            case .start:
                startAnimating()
            case .progress(_):
                stopAnimating()
            case .success(_):
                stopAnimating()
            case .failed(_):
                stopAnimating()
            default:
                stopAnimating()
            }
        }
    
        //给 label 赋值
        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
//            debugPrint("text赋值")
        case .attributedText(let text):
            messageLabel.text = text.string
            messageLabel.attributedText = text
//            debugPrint("attributedText赋值")
        default:
            break
        }
    }


    func startAnimating() {
        activityIndicator.isHidden = false
        activityIndicator.startAnimating()
    }
    
    func stopAnimating() {
        activityIndicator.isHidden = true
        activityIndicator.stopAnimating()
    }
}
