//
//  IMGiftContentView.swift
//  LegendTeam
//
//  Created by liangbo on 2022/3/2.
//

import UIKit

class IMGiftContentConfig: IMContentBaseConfig {
    override func contentSize(model: SessionBaseModel) -> CGSize {
        return CGSize(width:SessionBaseModel.maxBubbleContentWidth , height: 72)
    }
    
    override func cellInsets(model: SessionBaseModel) -> UIEdgeInsets {
        return UIEdgeInsets(top: 1, left: 16, bottom: 8, right: 16)
    }
    
    override func contentViewClass(model: SessionBaseModel) -> IMContentBaseView.Type {
        return IMGiftContentView.self
    }
}

class IMGiftContentView: IMContentBaseView {
    
    var effectView: UIVisualEffectView!
    var iconBlock: UIView!
    var giftIcon: UIImageView!
    var label: LineSpaceLabel!
   
    required override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        effectView = {
            let v = UIVisualEffectView(effect: UIBlurEffect(style: .light))
            v.alpha = 1
            v.cornerRadius = 16
            insertSubview(v, at: 0)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
        iconBlock = {
            let v = UIView()
            v.cornerRadius = 8
            v.backgroundColor = .c.cseln
            containerView.addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 48, height: 48))
                make.leading.equalToSuperview().offset(16)
                make.centerY.equalToSuperview()
            }
            return v
        }()
        
        giftIcon = {
            let v = UIImageView()
            iconBlock.addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo((CGSize(width: 48, height: 48)))
                make.center.equalToSuperview()
            }
            return v
        }()
        
        label = {
            let v = LineSpaceLabel()
            let typo = CLSystemToken.typography(token: .tbm)
            v.config(typo)
            containerView.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.leading.equalTo(iconBlock.snp.trailing).offset(10)
                make.trailing.lessThanOrEqualToSuperview().offset(-10)
            }
            return v
        }()
    }
    
    override func refreshModel(model: SessionBaseModel) {
        super.refreshModel(model: model)
        
        
        guard let info = IMCustomAttachment.getCustomInfo(model: model) else{
            return
        }

        giftIcon.loadImage(info.giftIcon, bgColor: .clear)
        label.text = "\(info.giftName ?? "-") x\(info.giftNum ?? 1)"//"Diamod x1"
    }
    
    @objc func checkTapAction() {
        // 跳转钱包
        AppRouter.goWalletCenter()
    }
}
