//
//  ChatHistoryContentCell.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/27.
//

import UIKit

class ChatHistoryContentCell: UITableViewCell {
    
    lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#F6F6F6")
        view.cornerRadius = 15.0
        return view
    }()
    
    lazy var iconImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.cornerRadius = 12.5
        imgView.backgroundColor = .blue
        return imgView
    }()
    
    lazy var timeLab: UILabel = {
        let lab = UILabel()
        lab.text = "2025/09/26 17:30"
        lab.textColor = UIColor.hexString("#999999", alpha: 0.6)
        lab.font = UIFont.systemFont(ofSize: 12)
        return lab
    }()
    
    lazy var contentLab: UILabel = {
        let lab = UILabel()
        lab.text = "The Boss Fell for Me: My Days Screwing Nuts at Foxconn"
        lab.textColor = UIColor(hex: "#666666")
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.numberOfLines = 2
        return lab
    }()
    
    lazy var arrowImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_setting_go"))
        return imgView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configureViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configureViews() {
        
        contentView.addSubview(containerView)
        containerView.addSubview(iconImgView)
        containerView.addSubview(timeLab)
        containerView.addSubview(contentLab)
        containerView.addSubview(arrowImgView)
        
        containerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.bottom.equalToSuperview().inset(2.5)
        }
        
        iconImgView.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(12)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(25)
        }
        
        timeLab.snp.makeConstraints { make in
            make.left.equalTo(iconImgView.snp.right).offset(8)
            make.top.equalTo(iconImgView.snp.top)
            make.right.equalTo(arrowImgView.snp.left).offset(-15)
        }
        
        contentLab.snp.makeConstraints { make in
            make.left.equalTo(timeLab.snp.left)
            make.right.equalTo(timeLab.snp.right).offset(-15)
            make.top.equalTo(timeLab.snp.bottom).offset(6)
            make.bottom.equalToSuperview().inset(10)
        }

        arrowImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(15)
            make.width.height.equalTo(15)
        }
        
        // 设置内容优先级，确保自动布局正确计算高度
        contentLab.setContentHuggingPriority(.required, for: .vertical)
        contentLab.setContentCompressionResistancePriority(.required, for: .vertical)
    }
    
    // 计算cell的准确高度
    static func calculateHeight(for width: CGFloat) -> CGFloat {
        let containerWidth = width - 40 // 左右各20的inset
        let iconHeight: CGFloat = 25
        let iconTopMargin: CGFloat = 10
        let timeToContentMargin: CGFloat = 6
        let contentBottomMargin: CGFloat = 10
        let cellTopBottomMargin: CGFloat = 5 // 上下各2.5的inset
        
        // 计算时间标签高度
        let timeHeight: CGFloat = 12 // 字体大小12
        
        // 计算内容标签高度（2行文本）
        let contentFont = UIFont.systemFont(ofSize: 14)
        let contentWidth = containerWidth - 12 - 25 - 8 - 15 - 15 // 减去各种边距
        let contentHeight = contentFont.lineHeight * 2 // 2行文本
        
        let totalHeight = cellTopBottomMargin + iconTopMargin + max(iconHeight, timeHeight) + timeToContentMargin + contentHeight + contentBottomMargin + cellTopBottomMargin
        
        return totalHeight
    }
    
    
}
