//
//  DiyFixedTextElement.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/20.
//
import ObjectMapper
class DiyFixedTextElement: TSBaseModel ,DiyElement,ObservableObject {
    
    @Published var text: String?
    @Published var fontName: FontName = .PingFangSC
    @Published var textColor: String?
    @Published var alignment: NSTextAlignment = .center
    @Published var backgroundColor: String?
    var fontSize: CGFloat = 20
    var lineSpace: CGFloat = 0
    
    override func mapping(map: Map) {
        text            <- map["text"]
        fontName        <- map["fontName"]
        textColor       <- map["textColor"]
        fontSize        <- map["fontSize"]
        alignment       <- map["alignment"]
        lineSpace       <- map["lineSpace"]
        backgroundColor       <- map["backgroundColor"]
    }
    
    var elementView: DiyElementView {
        return DiyFixedTextView(element: self)
    }
    
    var supportEditActions: EditElementAction {
        return [.none];
    }
}

class DiyFixedTextView: DiyElementBaseView {
 
    lazy var label: UILabel = {
        var label = UILabel()
        if let textElement = element as? DiyFixedTextElement{
            label = UILabel.createLabel(text: textElement.text,font: .font(size: 16*kDiyDesignScaleW),textColor: textElement.textColor?.uiColor,textAlignment: .center,numberOfLines: 0)
        }
        return label
    }()
    
    
    
    override func setupContentView() {
        
        contentView.addSubview(label)
        label.snp.makeConstraints { make in
            make.top.equalTo(8*kDiyDesignScaleW)
            make.bottom.equalTo(-8*kDiyDesignScaleW)
            make.leading.equalTo(16*kDiyDesignScaleW)
            make.trailing.equalTo(-16*kDiyDesignScaleW)
            make.width.lessThanOrEqualTo((k_ScreenWidth-84)*kDiyDesignScaleW).priority(.high)
            make.width.greaterThanOrEqualTo(60*kDiyDesignScaleW).priority(.high)
            make.width.greaterThanOrEqualTo(22*kDiyDesignScaleW).priority(.high)
        }
        
        if let textElement = element as? DiyFixedTextElement{
            contentView.backgroundColor = textElement.backgroundColor?.uiColor
       
        }
        updateContent()
        startObserve()
    }
    
    override func setupLayout(with config: DiyElementTransform) {
        super.setupLayout(with: config)
        
        kDelayMainShort {
            self.contentView.cornerRadius = self.contentView.height/2.0
        }
    }
    
    func startObserve() {
        guard let textElement = element as? DiyFixedTextElement else { return }
        // 监听变化更新UI
        textElement.$text.receive(on: DispatchQueue.main).sink { [weak self] newValue in
            self?.updateContent()
        }.store(in: &observers)
//        textElement.$fontName.receive(on: DispatchQueue.main).sink { [weak self] newValue in
//            self?.updateContent()
//        }.store(in: &observers)
//        textElement.$textColor.receive(on: DispatchQueue.main).sink { [weak self] newValue in
//            self?.updateContent()
//        }.store(in: &observers)
//        textElement.$alignment.receive(on: DispatchQueue.main).sink { [weak self] newValue in
//            self?.updateContent()
//        }.store(in: &observers)
    }
    
    func updateContent() {
        guard let textElement = element as? DiyFixedTextElement else { return }
        if let text = textElement.text {
            label.text = text
            self.isHidden = text.count == 0 ? true : false
            kDelayMainShort {
                self.contentView.cornerRadius = self.contentView.height/2.0
            }
        }

   
//        contentView.cornerRadius = contentView.height/2.0
//        if textElement.lineSpace > 0 {
//            let style = NSMutableParagraphStyle()
//            style.lineSpacing = textElement.lineSpace
//            style.alignment = textElement.alignment
//            label.attributedText = NSAttributedString(string: textElement.text ?? "Add Text".localized, attributes: [
//                        .foregroundColor: textElement.textColor?.color ?? "#111111".color,
//                        .font: UIFont.font(name: textElement.fontName, size: textElement.fontSize),
//                        .paragraphStyle: style])
//        } else {
//            label.textAlignment = textElement.alignment
//            label.text = textElement.text ?? "Add Text".localized
//            label.font = UIFont.font(name: textElement.fontName, size: textElement.fontSize)
//            label.textColor = textElement.textColor?.color ?? "#111111".color
//        }
        
        setNeedsDisplay()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
}
