//
//  NW_CommunityCell.swift
//  wallpaper_BProject
//
//  Created by 忆海16 on 2024/8/30.
//

import UIKit

class NW_CommunityCell: UITableViewCell {
    @IBOutlet weak var contentImageV: UIImageView!
    
    @IBOutlet weak var contentLabel: UILabel!

    @IBOutlet weak var zanbtn: UIButton!
    
    @IBOutlet weak var zanLabel: UILabel!
    
    @IBOutlet weak var plLabel: UILabel!
    
    @IBOutlet weak var lookLabel: UILabel!
    
    @IBOutlet weak var zfLabel: UILabel!
    
    
    @IBOutlet weak var bgView: UIView!
    
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        
        
        contentImageV.layer.cornerRadius = 10
       
        zanLabel.text =  "\(getRandomInt() ?? 10)"
        plLabel.text =  "\(getRandomInt() ?? 10)"
        lookLabel.text =  "\(getRandomInt() ?? 10)"
        zfLabel.text =  "\(getRandomInt() ?? 10)"
//        applyGradient(
//            to: bgView,
//            colors: [
//                UIColor(red: 0.949, green: 0.969, blue: 0.914, alpha: 1).cgColor,
//                UIColor(red: 0.882, green: 0.922, blue: 0.808, alpha: 1).cgColor
//            ],
//            cornerRadius: 8,
//            startPoint: CGPoint(x: 0.745123, y: -0.063808),
//            endPoint: CGPoint(x: 0.5, y: 1)
//        )
        
        
        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 0.01) {
            self.applyGradientToBgView()
        }
       
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 在布局子视图时重新调整渐变图层的大小
        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 0.01) {
            self.applyGradientToBgView()
        }
        
    }
    private func applyGradientToBgView() {
           // 移除旧的渐变层
           bgView.layer.sublayers?.removeAll(where: { $0 is CAGradientLayer })

           let gradientLayer = CAGradientLayer()
           gradientLayer.colors = [
               UIColor(red: 0.949, green: 0.969, blue: 0.914, alpha: 1).cgColor,
               UIColor(red: 0.882, green: 0.922, blue: 0.808, alpha: 1).cgColor
           ]
           gradientLayer.startPoint = CGPoint(x: 0.745123, y: -0.063808)
           gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
           gradientLayer.frame = bgView.bounds
           gradientLayer.cornerRadius = 8

           let maskLayer = CAShapeLayer()
           maskLayer.path = UIBezierPath(roundedRect: bgView.bounds, cornerRadius: 8).cgPath
           gradientLayer.mask = maskLayer

           bgView.layer.insertSublayer(gradientLayer, at: 0)
       }
    
    @IBAction func btnzan(_ sender: Any) {
        if zanbtn.isSelected == true{
            zanbtn.isSelected = false
            zanLabel.text = "\((Int(zanLabel.text ?? "10") ?? 10) - 1)"
        }else{
            zanbtn.isSelected = true
            zanLabel.text = "\((Int(zanLabel.text ?? "10") ?? 10) + 1)"
        }
        
    }
    func applyGradient(to view: UIView, colors: [CGColor], cornerRadius: CGFloat, startPoint: CGPoint, endPoint: CGPoint) {
        // Create a gradient layer
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = colors
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        gradientLayer.frame = view.bounds
        
        // Create a mask layer with rounded corners
        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(roundedRect: view.bounds, cornerRadius: cornerRadius).cgPath
        
        // Apply the mask and gradient to the view
        view.layer.insertSublayer(gradientLayer, at: 0)
        view.layer.mask = maskLayer
    }
    
}
