//
//  DiySticker.swift
//  ClockWidget
//
//  Created by fff on 2024/6/28.
//

import UIKit
import ObjectMapper

extension DiyStickerModel: DiyElement {
    var elementView: DiyElementView {
        let view = DiyStickerView(element: self)
        currentView = view
        return view
    }
}

class DiyStickerView: DiyElementBaseView {
    lazy var imageView =  UIImageView()
    
    override func setupContentView() {
//        self.backgroundColor = .white.withAlphaComponent(0.5)
//        imageView.backgroundColor = .blue.withAlphaComponent(0.2)
        imageView.contentMode = .scaleAspectFit
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        if let sticker = element as? DiyStickerModel, let url = sticker.imageUrl {
            if url.hasPrefix("http") {
                // 网络图片
                imageView.setAsyncImage(urlString: url)
            } else if let image = UIImage(named: url) {
                imageView.image = image
            }
        }
    }
}
