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

import MessageKit
import UIKit

class TSMessageContentCell: MessageCollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        setupSubviews()
    }
    
    /// The `MessageCellDelegate` for the cell.
    weak var delegate: MessageCellDelegate?
    
    
    /// 消息内容容器.frame由 子视图自动布局撑起大小
    var messageContainerView: UIView = {
        let containerView = UIView()
        containerView.cornerRadius = 16
        return containerView
    }()

    // 左头像
    var leadingAvatarImageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "aichat_avatar")
        return imageView
    }()
    
    // 右头像(预留)
    var trailingAvatarImageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "aichat_avatar")
        imageView.isHidden = true
        return imageView
    }()
    
    
    //初始化 cell
    func setupSubviews() {
        contentView.addSubview(leadingAvatarImageView)
        contentView.addSubview(messageContainerView)
        contentView.addSubview(trailingAvatarImageView)
        
        
        let leadingAvatarEdge = TSLayoutSizeCalculator.leadingAvatarEdge
        let leadingAvatarSize = TSLayoutSizeCalculator.leadingAvatarSize
        leadingAvatarImageView.snp.makeConstraints { make in
            make.leading.equalTo(leadingAvatarEdge.left)
            make.top.equalTo(leadingAvatarEdge.top)
            make.size.equalTo(leadingAvatarSize)
        }
        
        //暂时没用,预留的,数值都是 0
        let traingAvatarEdge = TSLayoutSizeCalculator.traingAvatarEdge
        let trainggAvatarSize = TSLayoutSizeCalculator.traingAvatarSize
        trailingAvatarImageView.snp.makeConstraints { make in
            make.trailing.equalTo(traingAvatarEdge.right)
            make.top.equalTo(traingAvatarEdge.top)
            make.size.equalTo(trainggAvatarSize)
        }
        
        let cellMessageContainerEdge = TSLayoutSizeCalculator.cellMessageContainerLeadingEdge
        messageContainerView.snp.makeConstraints { make in
            make.leading.equalTo(leadingAvatarImageView.snp.trailing).offset(cellMessageContainerEdge.left)
            make.trailing.lessThanOrEqualTo(trailingAvatarImageView.snp.leading).offset(-16)
            make.top.equalTo(cellMessageContainerEdge.top)
            make.bottom.equalTo(-cellMessageContainerEdge.bottom)
        }
    }
    

    
    override func prepareForReuse() {
        super.prepareForReuse()
    }
    
    // 点击触发事件
    override func handleTapGesture(_ gesture: UIGestureRecognizer) {
        let touchLocation = gesture.location(in: self)
        
        switch true {
        case messageContainerView.frame
                .contains(touchLocation) && !cellContentView(canHandle: convert(touchLocation, to: messageContainerView)):
            delegate?.didTapMessage(in: self)
//        case cellTopLabel.frame.contains(touchLocation):
//            delegate?.didTapCellTopLabel(in: self)
//    case cellDateLabel.frame.contains(touchLocation):
//      delegate?.didTapMessageBottomLabel(in: self)
        default:
            delegate?.didTapBackground(in: self)
        }
    }
    
    /// Handle long press gesture, return true when gestureRecognizer's touch point in `messageContainerView`'s frame
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        let touchPoint = gestureRecognizer.location(in: self)
        guard gestureRecognizer.isKind(of: UILongPressGestureRecognizer.self) else { return false }
        return messageContainerView.frame.contains(touchPoint)
    }

    //设置更新内容
    func configure(
        with message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView,
        dataSource: MessagesDataSource,
        and sizeCalculator: TSLayoutSizeCalculator)
    {
        guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
            return
        }
//        cellTopLabel.frame = sizeCalculator.cellTopLabelFrame(
//            for: message,
//            at: indexPath)
//        
//        cellTopLabel.attributedText = dataSource.cellTopLabelAttributedText(
//            for: message,
//            at: indexPath)
        //    cellDateLabel.frame = sizeCalculator.cellMessageBottomLabelFrame(
        //      for: message,
        //      at: indexPath)
        //      cellDateLabel.attributedText = dataSource.messageBottomLabelAttributedText(
        //        for: message,
        //        at: indexPath)
        
        
        

        //是否是当前用户发送的消息
        let fromCurrentSender = dataSource.isFromCurrentSender(message: message)
        if fromCurrentSender {
            leadingAvatarImageView.isHidden = true
            let cellMessageContainerEdge = TSLayoutSizeCalculator.cellMessageContainerTraingEdge
            messageContainerView.snp.remakeConstraints { make in
                make.leading.greaterThanOrEqualTo(cellMessageContainerEdge.left)
                make.trailing.equalTo(-cellMessageContainerEdge.right)
                make.top.equalTo(cellMessageContainerEdge.top)
                make.bottom.equalTo(-cellMessageContainerEdge.bottom)
            }
        }else{
            leadingAvatarImageView.isHidden = false
            let cellMessageContainerEdge = TSLayoutSizeCalculator.cellMessageContainerLeadingEdge
            messageContainerView.snp.remakeConstraints { make in
                make.leading.equalTo(leadingAvatarImageView.snp.trailing).offset(cellMessageContainerEdge.left)
                make.trailing.lessThanOrEqualTo(-cellMessageContainerEdge.right)
                make.top.equalTo(cellMessageContainerEdge.top)
                make.bottom.equalTo(-cellMessageContainerEdge.bottom)
            }
        }
        
        messageContainerView.backgroundColor = displayDelegate.backgroundColor(for: message,at: indexPath,in: messagesCollectionView)
    }
    
    /// Handle `ContentView`'s tap gesture, return false when `ContentView` doesn't needs to handle gesture
    func cellContentView(canHandle _: CGPoint) -> Bool {
        false
    }
}
