//
//  DiyStaticElement.swift
//  ClockWidget
//
//  Created by fff on 2024/7/3.
//

import UIKit
import ObjectMapper

class DiyStaticElement: TSBaseModel, DiyElement, ObservableObject {
    var imageUrl: String?
    
    override func mapping(map: Map) {
        imageUrl            <- map["imageUrl"]
    }
    
    var elementView: DiyElementView {
        return DiyStaticElementView(element: self)
    }
    
    var supportEditActions: EditElementAction {
        return [.none];
    }
}

class DiyStaticElementView: DiyElementBaseView {
    lazy var imageView = UIImageView()
    lazy var blurEffectView: UIVisualEffectView = {
        let blurEffect = UIBlurEffect(style: .dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        // 设置模糊效果视图的大小和位置
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        return blurEffectView
    }()
    
//    override func setupLayout(with config: DiyElementTransform) {
//        super.setupLayout(with: config)
//        
//        if let blur = config.blur {
//            contentView.insertSubview(blurEffectView, at: 0)
//            imageView.snp.makeConstraints { make in
//                make.edges.equalToSuperview()
//            }
//        }
//    }
    
    override func setupContentView() {
//        imageView.backgroundColor = .blue.withAlphaComponent(0.2)
        imageView.contentMode = .scaleAspectFit
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        if let photo = element as? DiyStaticElement, let url = photo.imageUrl {
            // 网络图片
            imageView.setAsyncImage(urlString: url,contentMode: .scaleAspectFit)
        }
    }
    
    func updatePhoto(_ photo: UIImage) {
        
    }
}
