//
//  TSTextGeneralPictureVM.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/20.
//

import ObjectMapper



let kRandomTextArray:[String] = [
    "Ultra-detailed northern lights over a glacial lake, mirrored reflections with vibrant turquoise and magenta hues, surreal celestial atmosphere, digital painting style with soft glow effects.",
    "Neon-lit cyberpunk night market in a rainy Asian metropolis, holographic food stalls and floating drones, teal-orange color grading, retro-futuristic architecture with glowing Kanji signs",
    "Macro photography of rainbow candy landscape with gummy bear mountains, chocolate river and lollipop trees, hyper-realistic textures under golden hour lighting",
    "Traditional Chinese ink painting style phoenix soaring through misty mountain peaks, dynamic black brushstrokes with crimson accents on rice paper texture",
    "Bioluminescent underwater metropolis with glass dome structures, schools of glowing fish weaving through coral skyscrapers, deep-sea blue palette with neon coral highlights",
    "1950s retro sci-fi movie poster style wallpaper featuring a rocket orbiting Saturn, vintage orange-teal color scheme with halftone textures and hand-drawn typography",
    "Minimalist wallpaper with overlapping translucent 3D geometric shapes in gradient pastel colors, clean negative space, soft shadows and metallic accents, 4K abstract design.",
    "Steampunk floating library with brass gears and clockwork mechanisms, leather-bound books flying through amber-lit fog, Victorian-era architecture blended with retro-futurism",
    "Zen rock garden transformed into cosmic landscape, black sand waves surrounding cratered moon-like stones, starry galaxy reflected in miniature, monochromatic with silver foil accents",
    "Futuristic AI core visualization with flowing golden data streams through transparent quantum circuits, dark background with floating holographic equations, cybernetic glow effect"
]


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

    
    lazy var ptpEntranceSectionModel: TSGenmojiCoLSectionModel = {
        let ptpEntranceSectionModel = TSGenmojiCoLSectionModel()
        ptpEntranceSectionModel.style = .ptpEntrance
        ptpEntranceSectionModel.name = ""
        
        let itemModel = TSGenmojiCoLItemModel()
        itemModel.style = .ptpEntrance
        ptpEntranceSectionModel.items = [itemModel]
        
        return ptpEntranceSectionModel
    }()
    
    
    lazy var generateSectionModel: TSGenmojiCoLSectionModel = {
        let generateSectionModel = TSGenmojiCoLSectionModel()
        generateSectionModel.style = .textPicGenerate
        generateSectionModel.name = ""
        
        let itemModel = TSGenmojiCoLItemModel()
        itemModel.style = .textPicGenerate
        generateSectionModel.items = [itemModel]
        
        return generateSectionModel
    }()
    
    //历史记录
    @UserDefault(key: "textPicHistoryListString", 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 = .textPicHistory
        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(ptpEntranceSectionModel)
        colDataArray.append(generateSectionModel)
        
        if enmojiHistorySeciton.items.count > 0 {
            colDataArray.append(enmojiHistorySeciton)
        }
    }
    
}
 
extension TSTextGeneralPictureVM {
    //历史记录
    var modelArray:[TSGenmojiModel]{
        get{
            let sectionModel = TSGenmojiCoLSectionModel()
            sectionModel.style = .textPicHistory
            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 = .textPicHistory
        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()
    }
    
}





