//
//  AccountManager.swift
//  wallpaper_project


import Foundation
import UIKit

// 定义一个结构体，用于表示账户信息
struct Account:Decodable,Encodable {
    let username: String
    let password: String
}


class AccountManager {
    static let accountsKey = "Accounts"
    static let currentAccountKey = "CurrentAccount"
    
    static func getAccounts() -> [Account] {
        guard let data = UserDefaults.standard.data(forKey: accountsKey) else {
            return []
        }
        
        do {
            let accounts = try JSONDecoder().decode([Account].self, from: data)
            return accounts
        } catch {
            print("Error decoding accounts: \(error)")
            return []
        }
    }
    
    static func saveAccount(account: Account) {
        var accounts = getAccounts()
        accounts.append(account)
        
        do {
            let data = try JSONEncoder().encode(accounts)
            UserDefaults.standard.set(data, forKey: accountsKey)
        } catch {
            print("Error encoding accounts: \(error)")
        }
    }
    
    static func deleteAccount(at index: Int) {
        var accounts = getAccounts()
        accounts.remove(at: index)
        
        do {
            let data = try JSONEncoder().encode(accounts)
            UserDefaults.standard.set(data, forKey: accountsKey)
        } catch {
            print("Error encoding accounts: \(error)")
        }
    }
    
    static func updateAccount(at index: Int, with newAccount: Account) {
        var accounts = getAccounts()
        accounts[index] = newAccount
        
        do {
            let data = try JSONEncoder().encode(accounts)
            UserDefaults.standard.set(data, forKey: accountsKey)
        } catch {
            print("Error encoding accounts: \(error)")
        }
    }
    
    static func createDefaultAccountIfNeeded() {
        if getAccounts().isEmpty {
            let defaultAccount = Account(username: "13730645123", password: "123456")
            saveAccount(account: defaultAccount)
        }
    }
    static func validateAccount(username: String, password: String) -> Bool {
        let accounts = getAccounts()
        for account in accounts {
            if account.username == username && account.password == password {
                return true
            }
        }
        return false
    }
    
    static var currentAccount: Account? {
            get {
                guard let data = UserDefaults.standard.data(forKey: currentAccountKey) else {
                    return nil
                }
                do {
                    let account = try JSONDecoder().decode(Account.self, from: data)
                    return account
                } catch {
                    print("Error decoding current account: \(error)")
                    return nil
                }
            }
            set {
                guard let newValue = newValue else {
                    UserDefaults.standard.removeObject(forKey: currentAccountKey)
                    return
                }
                do {
                    let data = try JSONEncoder().encode(newValue)
                    UserDefaults.standard.set(data, forKey: currentAccountKey)
                } catch {
                    print("Error encoding current account: \(error)")
                }
            }
        }
        
        static func login(username: String, password: String) -> Bool {
            let accounts = getAccounts()
            for account in accounts {
                if account.username == username && account.password == password {
                    currentAccount = account
                    return true
                }
            }
            return false
        }
        
        static func logout() {
            UserDefaults.standard.removeObject(forKey: currentAccountKey)
        }
    
    static func logoutSpecificAccount(username: String) {
        var accounts = getAccounts()
        if let index = accounts.firstIndex(where: { $0.username == username }) {
            accounts.remove(at: index)
            do {
                let data = try JSONEncoder().encode(accounts)
                UserDefaults.standard.set(data, forKey: accountsKey)
            } catch {
                print("Error encoding accounts: \(error)")
            }
        } else {
            print("Account not found")
        }
    }

}
