//
//  FS_HomeCollectionViewCell.swift
//  Funny_sounds
//
//  Created by 忆海16 on 2024/8/15.
//

import UIKit
import SnapKit
import SDWebImage

class FS_HomeCollectionViewCell: UICollectionViewCell {
    
    lazy var bgImageV:UIImageView = {
        let bgImageV = UIImageView()
        bgImageV.image = UIImage(named: "collectionBgV")
        bgImageV.contentMode = .scaleToFill
        
        return bgImageV
    }()
    
    lazy var conentImageV:UIImageView = {
        let conentImageV = UIImageView()
        conentImageV.image = UIImage(named: "collectionImageV")
        conentImageV.contentMode = .scaleAspectFit
        
        return conentImageV
    }()
    
    
    lazy var nameLabel: BorderedLabel = {
        let nameLabel = BorderedLabel()
        nameLabel.text = "Air Horn"
        nameLabel.font = UIFont(name: "PaytoneOne-Regular", size: 20)
        nameLabel.textColor = .white // 设置文本颜色为白色
        nameLabel.numberOfLines = 0
        nameLabel.textAlignment = .center
        
        // 设置阴影效果
        let shadow = NSShadow()
        shadow.shadowColor = UIColor.black.withAlphaComponent(1.0) // 黑色阴影
        shadow.shadowOffset = CGSize(width: 0, height: 6) // 向下偏移2个点
        shadow.shadowBlurRadius = 0 // 无模糊度
        
        // 设置属性字符串并应用阴影
        let attributedText = NSMutableAttributedString(string: nameLabel.text!)
        attributedText.addAttribute(.shadow, value: shadow, range: NSRange(location: 0, length: nameLabel.text!.count))
        nameLabel.attributedText = attributedText
        
        nameLabel.sizeToFit()
        
        return nameLabel
    }()


    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
        contentView.backgroundColor = .clear
        
    }
    
    //    MARK: -设置布局
    func setUI(){
        self.addSubview(bgImageV)
        self.addSubview(conentImageV)
        self.addSubview(nameLabel)
        
        bgImageV.snp.makeConstraints { make in
            make.width.height.equalToSuperview()
        }
        
        conentImageV.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            //            make.left.equalToSuperview().offset(7)
            //            make.right.equalToSuperview().offset(7)
            make.centerX.equalTo(bgImageV)
            make.height.equalTo(140)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(conentImageV.snp.bottom).offset(4)
            make.width.equalToSuperview()
//            make.height.equalTo(23)
        }
        
   
        
    }
    
    var model:SoundCategory?{
        didSet{
            if let imageURL = URL(string: model?.categoryUrl ?? "") {
//                wpImagV.sd_setImage(with: imageURL, completed: nil)
                conentImageV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "collectionImageV"))
                }
            nameLabel.text = model?.categoryName
        }
    }
    
}


class BorderedLabel: UILabel {
    override func drawText(in rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        
        // 保存原始的文本颜色
        let originalTextColor = self.textColor
        
        // 设置描边颜色和宽度
        context?.setLineWidth(3) // 描边宽度，这里可以调整宽度
        context?.setLineJoin(.round) // 圆角样式的描边
        context?.setTextDrawingMode(.stroke) // 设置文本绘制模式为描边
        self.textColor = UIColor.black // 设置描边颜色为黑色
        
        // 绘制描边
        super.drawText(in: rect)
        
        // 恢复文本颜色并绘制文本内容
        context?.setTextDrawingMode(.fill) // 设置文本绘制模式为填充
        self.textColor = originalTextColor // 恢复原始的文本颜色
        
        super.drawText(in: rect)
    }
}
