//
//  TSLightShapeView.swift
//  Girly
//
//  Created by 100Years on 2025/1/8.
//

class TSLightShapeView: TSBaseView {

    private var shwoWidth:CGFloat = k_ScreenWidth
    private var less:CGFloat = 5.0
    lazy var bgShapeView: UIView = {
        let shapeView = UIView()
        shapeView.frame = CGRect(x: 0, y: 0, width: shwoWidth-less, height: shwoWidth-less)
        shapeView.backgroundColor = color
        return shapeView
    }()
    
   
    lazy var blurView: UIView = {
        let blurView = createBlurEffectView(style:.extraLight,backgroundColor: .clear)
        blurView.frame = CGRect(x: 0, y: 0, width: shwoWidth, height: shwoWidth)
        return blurView
    }()
    
    lazy var shapeView: UIView = {
        let shapeView = UIView()
        shapeView.frame = CGRect(x: 0, y: 0, width: shwoWidth, height: shwoWidth)
        shapeView.backgroundColor = color
        return shapeView
    }()
    
    var scaleFactor:CGFloat = 1.0
    
    var imageNamed:String = "bold_roundness_shape" {
        didSet{
            if UIImage(named: imageNamed) != nil {
                setShapeView(shapeView: bgShapeView)
                setShapeView(shapeView: shapeView)
            }
        }
    }
    
    var color:UIColor = .white {
        didSet{
            shapeView.backgroundColor = color
            bgShapeView.backgroundColor = color
        }
    }
    
    override func creatUI() {
        self.backgroundColor = .mainBg
    }
    
    
    func addContent() {
        shwoWidth = min(contentView.width, contentView.height)
        contentView.addSubview(bgShapeView)
        contentView.addSubview(blurView)
        contentView.addSubview(shapeView)
        
        let bottom = 0
        bgShapeView.snp.makeConstraints { make in
            make.leading.equalTo(less)
            make.trailing.equalTo(-less)
            make.top.equalTo(0)
            make.bottom.equalTo(bottom)
        }
        
        blurView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(bottom)
        }
        
        shapeView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(bottom)
        }
        
        self.setNeedsLayout()
        kDelayMainShort {
            self.setShapeView(shapeView: self.bgShapeView)
            self.setShapeView(shapeView: self.shapeView)
        }
        
        for subview in blurView.subviews {
            if String(describing: type(of: subview)).contains("UIVisualEffectSubview") {
                subview.isHidden = true
            }
        }
    
    }
    
    
    func setShapeView(shapeView:UIView){
        if let image = UIImage(named: imageNamed) {
            shapeView.setImageMask(image: image)
        }
    }
}
