//
//  AV_SportsCell.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/7/3.
//

import UIKit

class AV_SportsCell: UITableViewCell {

    @IBOutlet weak var bgView: UIView!
    
    @IBOutlet weak var headImagV: UIImageView!
    
    @IBOutlet weak var nameLabel: UILabel!
    
    var gradientLayer: CAGradientLayer?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.bgView.layer.cornerRadius = 15
        self.bgView.clipsToBounds = true
      
        setupGradientLayer()
    }
    override func layoutSubviews() {
          super.layoutSubviews()
          // Ensure the gradient layer fills the entire view
//        gradientLayer?.frame = bgView.bounds
        gradientLayer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width - 56, height: 96)
      }
    
    private func setupGradientLayer() {
          gradientLayer = CAGradientLayer()
        gradientLayer?.colors = [UIColor.hexSting(color: "#F7F8F9", alpha: 1).cgColor, // #F7F8F9
                                 UIColor.hexSting(color: "#F7F8F9", alpha: 1).cgColor] // #F7F8F9
          gradientLayer?.startPoint = CGPoint(x: 0, y: 0.5)
          gradientLayer?.endPoint = CGPoint(x: 1, y: 0.5)
        bgView.layer.insertSublayer(gradientLayer!, at: 0)
      }

      func animateGradient(to colors: [CGColor]) {
          // Ensure the gradient layer fills the entire view before starting the animation
          layoutIfNeeded()
//          gradientLayer?.frame = bgView.bounds
          gradientLayer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width - 56, height: 96)

          let animation = CABasicAnimation(keyPath: "colors")
          animation.fromValue = gradientLayer?.colors
          animation.toValue = colors
          animation.duration = 1.0
          animation.isRemovedOnCompletion = false
          animation.fillMode = .forwards
          gradientLayer?.add(animation, forKey: "colorChange")
      }
    
}
