//
//  ImportFilesManager.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/9/10.
//

import Foundation
import TZImagePickerController
import TSVideoKit

class ImportFilesManager: NSObject, TZImagePickerControllerDelegate , UIDocumentPickerDelegate {
    
    static let shared = ImportFilesManager()

    func openPhotoLibrary(parent: UIViewController) {
        guard let vc = TZImagePickerController(maxImagesCount: 9, delegate: self) else { return }
        vc.allowPickingImage = false
        vc.allowPreview = false
        vc.allowTakeVideo = true
        vc.allowTakePicture = false
        vc.allowCameraLocation = false
        vc.allowPickingMultipleVideo = true
        vc.allowPickingOriginalPhoto = false
        vc.naviBgColor = UIColor.white
        vc.naviTitleColor = UIColor.black
        vc.pickerDelegate = self
        parent.present(vc, animated: true)
    }
    
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
        DispatchQueue.global().async {
            for asset in assets {
                if let mAsset = asset as? PHAsset {
                    TZImageManager.default().requestVideoURL(with: mAsset) { videoUrl in
                        if let url = videoUrl {
                            self.copyFileToUrl(url: url)
                        }
                    } failure: { _ in
                    }
                }
            }
        }
    }
    
    
    func copyFileToUrl(url: URL) {
        let fileId = UUID().uuidString
        let path = TSVideoOperator.shared.configuration.fileDir.appendingPathComponent(fileId)
        let destinationURL = uniqueFileURL(at: path, originalURL: url)
        do {
            try FileManager.default.copyItem(at: url, to: destinationURL)
            Task {
                await self.importVideo(videoId: fileId, from: destinationURL)
            }

        } catch {
            print(error.localizedDescription)
        }
    }

    private func importVideo(videoId: String, from url: URL) async {
        let asset = AVAsset(url: url)
        let image = await asset.generateThumbnail()
        
        await MainActor.run {
            let title = url.lastPathComponent
            let pathExt = url.pathExtension.lowercased()
            let vPath = videoId + "/" + title

            let audios = ["mp3", "wav", "m4a"]
            var isRing: Bool = false
            if audios.contains(pathExt) {
                isRing = true
            }
            let imageData = image?.jpegData(compressionQuality: 0.8)
            let defaultData = UIImage.init(named: "ic_default")?.jpegData(compressionQuality: 0.8)
            let finalData = imageData ?? defaultData
            TSVideoOperator.shared.dataManager.createVideo(videoId: videoId, videoUrl: nil, audioStream: nil, videoStream: nil, videoTitle: title, artist: "UnKnown".localized(), artwork: finalData, online: false,customTag: isRing, status: .cached, vPath: vPath) { _ in
                NotificationCenter.default.post(name: .K_ImportSuccessNotifaction, object: nil)
            }
        }
    }
    
    func uniqueFileURL(at directory: URL, originalURL: URL) -> URL {
        let fileManager = FileManager.default

        // 检查目录是否存在，如果不存在则创建目录
        if !fileManager.fileExists(atPath: directory.path) {
            do {
                try fileManager.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil)
            } catch {
                // 这里可以选择抛出错误或处理错误
            }
        }

        let originalFilename = originalURL.deletingPathExtension().lastPathComponent
        let fileExtension = originalURL.pathExtension
        var uniqueURL = directory.appendingPathComponent(originalFilename).appendingPathExtension(fileExtension)

        var counter = 1
        while fileManager.fileExists(atPath: uniqueURL.path) {
            uniqueURL = directory.appendingPathComponent("\(originalFilename)-\(counter)").appendingPathExtension(fileExtension)
            counter += 1
        }

        return uniqueURL
    }
    
    func openFileDocument(parent: UIViewController,completion: ((Bool, String?, URL?) -> Void)?) {
        let documentTypes = ["public.mp3", "public.movie"]
        // 音频 mp3，wav，m4a
        // .MP3; .AAC; .WAV; .WMA; .CDA; .FLAC; .M4A; .MID; .MKA; .MP2; .MPA; .MPC; .APE; .OFR; .OGG; .RA; .WV; .TTA; .AC3; .DTS
        // 视频 mp4，avi, wmv，mov，mpg(mpeg)
        let documentVC = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
        //        Appropriate
        //        let documentVC = UIDocumentPickerViewController(forOpeningContentTypes: [.aiff, .audio, .avi, .wav, .mp3, .movie])
        documentVC.delegate = self
        documentVC.modalPresentationStyle = .fullScreen
        parent.present(documentVC, animated: true)
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        if let first = urls.first {
            copyFileToUrl(url: first)
        }
    }
}
