//
//  TSGenmojiViewModel.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//


import ObjectMapper

class TSGenmojiViewModel {
    var colDataArray:[TSComponent] = [TSComponent]()

    lazy var generateSectionModel: TSGenmojiCoLSectionModel = {
        let generateSectionModel = TSGenmojiCoLSectionModel()
        generateSectionModel.name = ""
        generateSectionModel.items = [TSGenmojiCoLItemModel()]
        return generateSectionModel
    }()
    
    //历史记录
    @UserDefault(key: "enmojiHistoryListString", defaultValue: "")
    private var enmojiHistoryListString: String
    
    
    lazy var listModelArray: [TSGenmojiModel] = {
        if let listModelArray = Mapper<TSGenmojiModel>().mapArray(JSONString: enmojiHistoryListString){
            return listModelArray
        }
        return []
    }()
    
    lazy var enmojiHistorySeciton: TSGenmojiCoLSectionModel = {
        let sectionModel = TSGenmojiCoLSectionModel()
        sectionModel.style = .history
        sectionModel.name = "History".localized
        for model in listModelArray {
            let itemModel = TSGenmojiCoLItemModel()
            itemModel.style = sectionModel.style
            itemModel.dataModel = model
            sectionModel.items.append(itemModel)
        }
        return sectionModel
    }()
    
    

    init() {
        combinedData()
    }
    
    func combinedData(){
        colDataArray.removeAll()
        colDataArray.append(generateSectionModel)
        if enmojiHistorySeciton.items.count > 0 {
            colDataArray.append(enmojiHistorySeciton)
        }
    }
    
}
 
extension TSGenmojiViewModel {
    //历史记录
    var modelArray:[TSGenmojiModel]{
        get{
            let sectionModel = TSGenmojiCoLSectionModel()
            sectionModel.style = .history
            sectionModel.name = "History".localized
        
            if let modelArray = Mapper<TSGenmojiModel>().mapArray(JSONString: enmojiHistoryListString){
                return modelArray
            }
            return []
        }
        set{
            if let jsonString = newValue.toJSONString() {
                enmojiHistoryListString = jsonString
            }
        }
    }
    
    //返回值,是否需要清空后刷新
    func saveModel(model:TSGenmojiModel)->Bool{
        listModelArray.insert(model, at: 0)
        if let jsonString = listModelArray.toJSONString() {
            enmojiHistoryListString = jsonString
        }
        var isNeed = false
        if enmojiHistorySeciton.items.count == 0 {
            colDataArray.append(enmojiHistorySeciton)
            isNeed = true
        }
          
        let colItemModel = TSGenmojiCoLItemModel()
        colItemModel.style = .history
        colItemModel.dataModel = model
        enmojiHistorySeciton.items.insert(colItemModel, at: 0)
        return isNeed
    }
    
    
    
    func removeAllHistoryList(){
        listModelArray.removeAll()
        if let jsonString = listModelArray.toJSONString() {
            enmojiHistoryListString = jsonString
        }
        
        enmojiHistorySeciton.items.removeAll()
        colDataArray.removeLast()
    }
    
}


