//
//  TSDiyKeyboardVM.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/21.
//

let kKeyDelete = "delete"

class TSDiyKeyboardVM{
    //####################################传入参数####################################//
    
    var templateModel:DiyPaperTemplateModel
    var keyStickerModel:DiyStickerCategory
   
    init(templateModel: DiyPaperTemplateModel, keyStickerModel: DiyStickerCategory, alreadyEntereds: [DiyStickerModel] = [DiyStickerModel]()) {
        self.templateModel = templateModel
        self.keyStickerModel = keyStickerModel
        self.alreadyEntereds = alreadyEntereds
    }

    //####################################键盘相关的####################################//
    lazy var americanKeyboardNames: [[String]] = {
        return [
            ["Q","W","E","R","T","Y","U","I","O","P"],
            ["A","S","D","F","G","H","J","K","L",],
            ["Z","X","C","V","B","N","M","delete"]
        ]
    }()

    func getAmericanKeyboardStyleArray() -> [[DiyStickerModel]] {
        var resultArray = [[DiyStickerModel]]()
        for strings in americanKeyboardNames {
            var models = [DiyStickerModel]()
            for str in strings {
                if str == "delete"{//删除按钮
                    let model = DiyStickerModel()
                    model.name = kKeyDelete
                    model.imageUrl = "keyBoard_delete"
                    models.append(model)
                }else{
                    if let model = keyStickerModel.stickers.first(where: {$0.name == str}){
                        models.append(model)
                    }
                }
            }
            resultArray.append(models)
        }
        return resultArray
    }
    
    //####################################已经点击输入的键盘 model####################################//
    var alreadyEntereds = [DiyStickerModel]()
    
    func addAlreadyEnteredModel(model:DiyStickerModel,diyCanvasView:DiyPaperTemplateBaseView){
        if model.name == kKeyDelete {
            if let obj = alreadyEntereds.last {
                alreadyEntereds.removeLast()
                //obj.currentView?.removeFromSuperview()
                diyCanvasView.removeElementView(obj)
            }
        }else{
            alreadyEntereds.append(model)
            diyCanvasView.add(element: model)
        }
        
        updateBgText()
     

        if alreadyEntereds.count > 0{
            removeDecorateElements(diyCanvasView: diyCanvasView)
        }
        
    }
    
    
    
    
    func getEnteredText()->String{
        var text = ""
        for model in alreadyEntereds {
            if let name = model.name {
                text += name
            }
        }
        return text
    }
    
    

    
    //####################################diy视图相关的####################################//
    
    func updateBgText() {
        for model in templateModel.elements {
            if model.type == .fixedText, let fixedText = model.element as? DiyFixedTextElement{
                fixedText.text = getEnteredText()
            }
        }
    }
    private var isRemoveDecorate = false
    func removeDecorateElements(diyCanvasView:DiyPaperTemplateBaseView){
        
        if isRemoveDecorate == true {
            return
        }

        for model in templateModel.elements {
            if model.type == .decorate, let element = model.element as? DiyStaticElement{
                diyCanvasView.removeElementView(element)
                isRemoveDecorate = true
            }
        }
    }

}
