//
//  PhotoManager.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/24.
//




import Photos
import UIKit

let PhotoManagerShared = PhotoManager.shared
class PhotoManager {
    static let shared = PhotoManager()

    func saveImagesToUniqueAlbum(images: [UIImage], baseAlbumName: String, completion: @escaping (Bool, Error?) -> Void) {
        // 检查并请求权限
        PHPhotoLibrary.requestAuthorization { status in
            guard status == .authorized else {
                completion(false, NSError(domain: "PhotoLibrary", code: 1, userInfo: [NSLocalizedDescriptionKey: "未获得相册访问权限"]))
                return
            }

            // 获取所有相册名称
            let allAlbumNames = self.fetchAllAlbumNames()
            let uniqueAlbumName = self.generateUniqueAlbumName(baseName: baseAlbumName, existingNames: allAlbumNames)
            
            // 保存图片到生成的相册
            self.saveImagesToAlbum(images: images, albumName: uniqueAlbumName, completion: completion)
        }
    }

    // 获取所有相册名称
    private func fetchAllAlbumNames() -> [String] {
        var albumNames: [String] = []
        
        let userCollections = PHCollectionList.fetchTopLevelUserCollections(with: nil)
        userCollections.enumerateObjects { collection, _, _ in
            if let album = collection as? PHAssetCollection, let albumName = album.localizedTitle {
                albumNames.append(albumName)
            }
        }
        
        return albumNames
    }

    // 生成唯一的相册名称
    private func generateUniqueAlbumName(baseName: String, existingNames: [String]) -> String {
        var uniqueName = baseName
        var index = 1
        
        while existingNames.contains(uniqueName) {
            uniqueName = "\(baseName)-\(index)"
            index += 1
        }
        
        return uniqueName
    }

    // 保存图片到指定相册（之前提供的 saveImagesToAlbum 方法）
    private func saveImagesToAlbum(images: [UIImage], albumName: String, completion: @escaping (Bool, Error?) -> Void) {
        var albumPlaceholder: PHObjectPlaceholder?
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
        let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
        
        if let existingAlbum = collection.firstObject {
            saveImages(images: images, to: existingAlbum, completion: completion)
        } else {
            PHPhotoLibrary.shared().performChanges {
                let creationRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
                albumPlaceholder = creationRequest.placeholderForCreatedAssetCollection
            } completionHandler: { success, error in
                guard success, let placeholder = albumPlaceholder else {
                    completion(false, error)
                    return
                }
                
                let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
                if let newAlbum = fetchResult.firstObject {
                    self.saveImages(images: images, to: newAlbum, completion: completion)
                } else {
                    completion(false, NSError(domain: "PhotoLibrary", code: 2, userInfo: [NSLocalizedDescriptionKey: "创建相册失败"]))
                }
            }
        }
    }

    // 保存图片到指定相册
    private func saveImages(images: [UIImage], to album: PHAssetCollection, completion: @escaping (Bool, Error?) -> Void) {
        PHPhotoLibrary.shared().performChanges {
            let assetRequest = images.map { PHAssetChangeRequest.creationRequestForAsset(from: $0) }
            let assetPlaceholders = assetRequest.compactMap { $0.placeholderForCreatedAsset }
            
            guard !assetPlaceholders.isEmpty else { return }
            
            if let albumChangeRequest = PHAssetCollectionChangeRequest(for: album) {
                albumChangeRequest.addAssets(assetPlaceholders as NSArray)
            }
        } completionHandler: { success, error in
            completion(success, error)
        }
    }
}
