//
//  IMImageContentView.swift

//
//  Created by Leon on 2025/8/22.
//

import UIKit
class IMImageContentConfig: IMContentBaseConfig {
    override func contentSize(model: SessionBaseModel) -> CGSize {
//        return CGSize(width: 60, height: 60)
        guard let attach = model.baseRemoteInfo?.customAttachment else { return CGSize(width: 144, height: 144) }
        
        var imgSize = CGSize(width: 1920, height: 1080)
        if let price = attach.unlockPrice, price > 0{
            return CGSize(width: 144, height: 144)
        }else{
            imgSize = CGSize(width: attach.width?.value ?? 1920, height: attach.height?.value ?? 1080)
        }
        
        let constants = ImageDisplayLogic.adjustConstants(for: SessionBaseModel.maxBubbleContentWidth)
        let sizeInfo = ImageDisplayLogic.calculateImageDisplaySize(
            originalWidth: imgSize.width,
            originalHeight: imgSize.height,
            constants: constants
        )
        //dlog("🐰type:\(sizeInfo.type)")
        return CGSize(width: sizeInfo.width, height: sizeInfo.height)
    }
    
    override func cellInsets(model: SessionBaseModel) -> UIEdgeInsets {
        return UIEdgeInsets(top: 8, left: 24, bottom: 8, right: 24)
    }
    
    override func contentViewClass(model: SessionBaseModel) -> IMContentBaseView.Type {
        return IMImageContentView.self
    }
}

class IMImageContentView: IMContentBaseView {
    var effectView: UIVisualEffectView!
    var imageView: UIImageView!
    
    var unlockContainer: UIView!
    var lockIcon : UIImageView!
    var unlockIconPriceLabel: CLIconLabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        
        containerView.cornerRadius = 16
        
        effectView = {
            let v = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
            v.alpha = 1
            v.cornerRadius = 16
            insertSubview(v, at: 0)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
        imageView = UIImageView()
        containerView.addSubview(imageView)
        imageView.contentMode = .scaleAspectFill
        imageView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        unlockContainer = {
            let v = UIView()
            containerView.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            
            let lockIcon = UIImageView()
            lockIcon.image = MWIconFont.image(fromIcon: .iconPrivate, size: CGSize(width: 32, height: 32), color: .white)
            v.addSubview(lockIcon)
            lockIcon.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(46)
            }
            self.lockIcon = lockIcon
            
            v.isHidden  = true
            
            return v
        }()
        
        unlockIconPriceLabel = {
            let v = CLIconLabel()
            v.iconSize = CGSize(width: 16, height: 16)
            v.iconImageView.image = UIImage(named: "icon_32_diamond")
            v.contentLabel.font = .t.tlm
            unlockContainer.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(lockIcon.snp.bottom).offset(16)
            }
            return v
        }()
    }
    
    override func refreshModel(model: SessionBaseModel) {
        super.refreshModel(model: model)
        
//        guard let raw = model.v2msg?.attachment?.raw, let data = CodableHelper.decode(IMCustomAttachment.self, from: raw) else{
//            return
//        }
        
        guard let customAttachment = model.baseRemoteInfo?.customAttachment else{
            return
        }
        
        if model.baseRemoteInfo?.cellType != .image{
            assert(false)
            return
        }
                
        // model.v2msg?.attachment
        imageView.loadImage(customAttachment.url)
        
        if let unlockPrice = customAttachment.unlockPrice, unlockPrice > 0{
            unlockContainer.isHidden = false
            unlockIconPriceLabel.contentLabel.text = "\(String.thousandString(float: (Double(unlockPrice) / 100.0), maxDigits: 2)) unlock"
        }else{
            unlockContainer.isHidden = true
        }
        

        
    }
    
    override func cellTapAction() {
        let event = IMEventModel()
        event.eventType = .image
        event.cellModel = self.model
        event.senderView = self.imageView
        delegate?.onTapAction(event: event)
    }
}
