//
//  historyManager.swift
//  wallpaper_project


import Foundation

class HistoryManager {
    static let shared = HistoryManager()
    private let historyKey = "com.example.app.history"

    // 获取浏览历史
       var history: [WP_4KModel] {
           get {
               if let data = UserDefaults.standard.data(forKey: historyKey),
                  let history = try? JSONDecoder().decode([WP_4KModel].self, from: data) {
                   return history
               }
               return []
           }
           set {
               let data = try? JSONEncoder().encode(newValue)
               UserDefaults.standard.set(data, forKey: historyKey)
           }
       }

       // 添加浏览历史项
       func addHistoryItem(item: WP_4KModel) {
           // 检查是否已存在相同标题的记录
           if history.contains(where: { $0.title == item.title }) {
               return
           }
           var updatedHistory = history
           updatedHistory.append(item)
           history = updatedHistory
       }

       // 清除浏览历史
       func clearHistory() {
           UserDefaults.standard.removeObject(forKey: historyKey)
       }
}


class liveManager {
    static let shared = liveManager()
    private let historyKey = "com.example.app.live"

    // 获取浏览历史
       var history: [WP_4KModel] {
           get {
               if let data = UserDefaults.standard.data(forKey: historyKey),
                  let history = try? JSONDecoder().decode([WP_4KModel].self, from: data) {
                   return history
               }
               return []
           }
           set {
               let data = try? JSONEncoder().encode(newValue)
               UserDefaults.standard.set(data, forKey: historyKey)
           }
       }

       // 添加浏览历史项
       func addHistoryItem(item: WP_4KModel) {
           // 检查是否已存在相同标题的记录
           if history.contains(where: { $0.title == item.title }) {
               return
           }
           var updatedHistory = history
           updatedHistory.append(item)
           history = updatedHistory
       }

       // 清除浏览历史
       func clearHistory() {
           UserDefaults.standard.removeObject(forKey: historyKey)
       }
}



class HistoryjpgManager {
    static let shared = HistoryjpgManager()
    private let historyKey = "com.example.app.jpg"

    // 获取浏览历史
       var history: [WallpaperData] {
           get {
               if let data = UserDefaults.standard.data(forKey: historyKey),
                  let history = try? JSONDecoder().decode([WallpaperData].self, from: data) {
                   return history
               }
               return []
           }
           set {
               let data = try? JSONEncoder().encode(newValue)
               UserDefaults.standard.set(data, forKey: historyKey)
           }
       }

       // 添加浏览历史项
       func addHistoryItem(item: WallpaperData) {
           // 检查是否已存在相同标题的记录
           if history.contains(where: { $0.previewThumb == item.previewThumb }) {
               return
           }
           var updatedHistory = history
           updatedHistory.append(item)
           history = updatedHistory
       }

       // 清除浏览历史
       func clearHistory() {
           UserDefaults.standard.removeObject(forKey: historyKey)
       }
}


class scjpgManager {
    static let shared = scjpgManager()
    private let historyKey = "com.example.app.scjpg"

    // 获取浏览历史
       var history: [WallpaperData] {
           get {
               if let data = UserDefaults.standard.data(forKey: historyKey),
                  let history = try? JSONDecoder().decode([WallpaperData].self, from: data) {
                   return history
               }
               return []
           }
           set {
               let data = try? JSONEncoder().encode(newValue)
               UserDefaults.standard.set(data, forKey: historyKey)
           }
       }

       // 添加浏览历史项
       func addHistoryItem(item: WallpaperData) {
           // 检查是否已存在相同标题的记录
           if history.contains(where: { $0.previewThumb == item.previewThumb }) {
               return
           }
           var updatedHistory = history
           updatedHistory.append(item)
           history = updatedHistory
       }

       // 清除浏览历史
       func clearHistory() {
           UserDefaults.standard.removeObject(forKey: historyKey)
       }
}

class sc4kManager {
    static let shared = sc4kManager()
    private let historyKey = "com.example.app.sc4k"

    // 获取浏览历史
       var history: [WP_4KModel] {
           get {
               if let data = UserDefaults.standard.data(forKey: historyKey),
                  let history = try? JSONDecoder().decode([WP_4KModel].self, from: data) {
                   return history
               }
               return []
           }
           set {
               let data = try? JSONEncoder().encode(newValue)
               UserDefaults.standard.set(data, forKey: historyKey)
           }
       }

       // 添加浏览历史项
       func addHistoryItem(item: WP_4KModel) {
           // 检查是否已存在相同标题的记录
           if history.contains(where: { $0.title == item.title }) {
               return
           }
           var updatedHistory = history
           updatedHistory.append(item)
           history = updatedHistory
       }

       // 清除浏览历史
       func clearHistory() {
           UserDefaults.standard.removeObject(forKey: historyKey)
       }
}


// 存储登录状态
func setLoggedIn(_ value: Bool) {
    UserDefaults.standard.set(value, forKey: "isLoggedIn")
}

/// 检查用户是否已登录
func isLoggedIn() -> Bool {
    if UserDefaults.standard.object(forKey: "isLoggedIn") != nil {
        return UserDefaults.standard.bool(forKey: "isLoggedIn")
    } else {
        // 默认返回未登录状态
        return false
    }
}

//func issave(_ value: Bool) {
//    UserDefaults.standard.set(value, forKey: "issave")
//}
//
//func issure(_ value: Bool) {
//    UserDefaults.standard.set(value, forKey: "issure")
//}
