//
//  LNRoomGiftItemCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/23.
//

import Foundation
import UIKit
import SnapKit


class LNRoomGiftItemCell: UICollectionViewCell {
    private let selectionBackground = UIImageView()
    private let iconView = UIImageView()
    private let nameLabel = UILabel()
    private let priceLabel = UILabel()
    override var isSelected: Bool {
        didSet {
            selectionBackground.isHidden = !isSelected
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ item: LNGiftItemVO) {
        guard let res = LNGiftManager.shared.resource(for: item.resId) else {
            isHidden = true
            return
        }
        isHidden = false
        nameLabel.text = res.curName
        priceLabel.text = item.value.currencyDisplay
        iconView.sd_setImage(with: URL(string: res.icon))
    }
    
    func showJumpAnimate() {
        iconView.layer.removeAllAnimations()
        iconView.transform = .identity
        
        let totalDuration: TimeInterval = 1.0
        let firstJumpHeight: CGFloat = 8.0
        let secondJumpHeight: CGFloat = 6.0
        
        let perTime = totalDuration / 4.0
        UIView.animateKeyframes(withDuration: totalDuration, delay: 0, options: [.calculationModeCubic], animations: {
            UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: perTime) {
                self.iconView.transform = CGAffineTransform(translationX: 0, y: -firstJumpHeight)
            }
            UIView.addKeyframe(withRelativeStartTime: perTime, relativeDuration: perTime) {
                self.iconView.transform = .identity
            }
            UIView.addKeyframe(withRelativeStartTime: 2 * perTime, relativeDuration: perTime) {
                self.iconView.transform = CGAffineTransform(translationX: 0, y: -secondJumpHeight)
            }
            UIView.addKeyframe(withRelativeStartTime: 3 * perTime, relativeDuration: perTime) {
                self.iconView.transform = .identity
            }
        })
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNRoomGiftItemCell {
    func setupViews() {
        backgroundColor = .clear
        
        selectionBackground.image = .icGiftSelectedBg
        selectionBackground.isHidden = true
        contentView.addSubview(selectionBackground)
        selectionBackground.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        iconView.contentMode = .scaleAspectFit
        contentView.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.height.equalTo(58)
        }
        
        nameLabel.font = .body_xs
        nameLabel.textColor = .text_1
        nameLabel.textAlignment = .center
        contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(iconView.snp.bottom).offset(2)
            make.centerX.equalToSuperview()
            make.horizontalEdges.equalToSuperview().inset(4)
        }
        
        let priceView = UIView()
        contentView.addSubview(priceView)
        priceView.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(2)
            make.centerX.equalToSuperview()
            make.bottom.lessThanOrEqualToSuperview()
        }
        
        let diamond = UIImageView.diamondImageView()
        diamond.contentMode = .scaleAspectFit
        priceView.addSubview(diamond)
        diamond.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(10)
        }
        
        priceLabel.font = .body_xs
        priceLabel.textColor = .text_1.withAlphaComponent(0.5)
        priceView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.leading.equalTo(diamond.snp.trailing).offset(2)
            make.top.bottom.trailing.equalToSuperview()
        }
    }
}
