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

import MessageKit
import UIKit

class TSLayoutSizeCalculator: MessageSizeCalculator {//CellSizeCalculator {
    
//    override init(layout: MessagesCollectionViewFlowLayout? = nil) {
//        super.init()
//        self.layout = layout
//    }

//    var messagesLayout: MessagesCollectionViewFlowLayout {
//        layout as! MessagesCollectionViewFlowLayout
//    }
    
    static let topSpaceH = 0.0
    static let bottomSpaceH = 0.0
    

    static var avatarSize = CGSizeMake(40, 40)
    //MARK: ###################################左边头像###################################
    static var leadingAvatarEdge:UIEdgeInsets = UIEdgeInsets(top: topSpaceH, left: 16, bottom: 0, right: 0)
    static var leadingAvatarSize = CGSizeMake(40, 40)
    //MARK: ###################################右边头像###################################
    static var traingAvatarEdge:UIEdgeInsets = UIEdgeInsets(top: topSpaceH, left: 0, bottom: 0, right: 0)
    static var traingAvatarSize = CGSizeMake(0, 0)
    
    //MARK: ###################################消息容器###################################
    //消息容器的 UIEdgeInsets
    static var cellMessageContainerLeadingEdge:UIEdgeInsets = UIEdgeInsets(top: topSpaceH, left: 8, bottom: 0, right: 16)
    static var cellMessageContainerTraingEdge:UIEdgeInsets = UIEdgeInsets(top: topSpaceH, left: 16, bottom: 0, right: 16)
    static var cellMessageContainerMinSize:CGSize = CGSizeMake(48, 48)
    
    //消息容器最大宽度
    lazy var messageContainerLeadingMaxWidth: CGFloat = {
        //左头像
        var cellMessageContainerMaxWidth = messagesLayout.itemWidth - Self.leadingAvatarEdge.left - Self.leadingAvatarSize.width
        //容器 填充
        cellMessageContainerMaxWidth = cellMessageContainerMaxWidth - Self.cellMessageContainerLeadingEdge.left - Self.cellMessageContainerLeadingEdge.right
        return cellMessageContainerMaxWidth
    }()

    lazy var messageContainerTraingMaxWidth: CGFloat = {
        //右头像
        var cellMessageContainerMaxWidth = messagesLayout.itemWidth - Self.traingAvatarEdge.right - Self.traingAvatarSize.width
        //容器 填充
        cellMessageContainerMaxWidth = cellMessageContainerMaxWidth - Self.cellMessageContainerTraingEdge.left - Self.cellMessageContainerTraingEdge.right
        return cellMessageContainerMaxWidth
    }()


    var messagesDataSource: MessagesDataSource {
        self.messagesLayout.messagesDataSource
    }
    
    //cell itemSize
    override func sizeForItem(at indexPath: IndexPath) -> CGSize {
        let dataSource = messagesDataSource
        let message = dataSource.messageForItem(at: indexPath,in: messagesLayout.messagesCollectionView)
        let itemHeight = cellContentHeight(for: message,at: indexPath,fromCurrentSender:dataSource.isFromCurrentSender(message: message))
        return CGSize(width: messagesLayout.itemWidth,height: itemHeight)
    }
    
    //消息容器的高度
    func cellContentHeight(for message: MessageType,at indexPath: IndexPath,fromCurrentSender: Bool)-> CGFloat {
        return Self.cellMessageContainerMinSize.height
    }
    
}
