//
//  FaceFiltersUtils.swift
//  WoWonder
//
//  Created by iMac on 29/04/25.
//  Copyright © 2025 ScriptSun. All rights reserved.
//

import Foundation
import Alamofire

struct FaceModel {
    var image: String
    var name: String
    var type: String
    var url: String
}

public class FaceFiltersUtils {
    
    static let fileManager = FileManager.default
    
    static func faceFiltersList() -> [FaceModel] {
        return [
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/022/288/319/non_2x/cancel-3d-icon-free-png.png", name: "none", type: "mask", url: ""),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/047/606/497/non_2x/designer-sunglasses-isolated-on-transparent-background-free-png.png", name: "aviators", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481431/aviators_hr4rly"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/053/343/527/non_2x/unique-ceramic-smile-decoration-for-home-decor-png.png", name: "bigmouth", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481429/bigmouth_hkiuch"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/043/345/649/non_2x/dalmatian-puppy-sitting-patiently-illustration-on-transparent-background-free-png.png", name: "dalmatian", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481431/dalmatian_uyaq8q"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/032/325/324/non_2x/red-roses-bouquet-of-garden-flowers-floral-arrangement-isolated-on-transparent-background-file-cut-out-ai-generated-png.png", name: "flowers", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481443/flowers_f7ryu6"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/008/851/566/non_2x/cute-koala-3d-illustration-png.png", name: "koala", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481437/koala_tmlpvk"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/039/213/315/non_2x/ai-generated-lion-on-transparent-background-ai-generated-png.png", name: "lion", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481436/lion_gsyvps"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/053/647/278/non_2x/expressive-image-of-a-teenage-boy-conveying-disgust-and-displeasure-perfect-for-social-media-and-youth-centric-content-png.png", name: "smallface", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481438/smallface_yttccn"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/044/570/898/non_2x/close-up-of-a-hand-holding-a-lit-cigarette-against-a-transparent-background-png.png", name: "teddycigar", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481442/teddycigar_exyldu"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/052/654/167/non_2x/young-man-in-gray-hoodie-thinking-deeply-indoors-png.png", name: "kanye", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481434/kanye_es58tb"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/051/959/791/non_2x/abstract-3d-rendering-of-a-woman-s-facial-expressions-capturing-various-emotions-and-perspectives-in-a-modern-artistic-style-png.png", name: "tripleface", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481439/tripleface_mjvqzu"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/013/149/631/non_2x/watercolor-elements-women-s-travel-blindfold-png.png", name: "sleepingmask", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481438/sleepingmask_lrpvvq"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/053/647/327/non_2x/a-playful-teenage-girl-rolling-her-eyes-in-a-fun-and-expressive-way-perfect-for-conveying-youthful-attitude-in-social-media-or-marketing-campaigns-png.png", name: "fatify", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481431/fatify_y1etts"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/021/595/653/non_2x/woman-with-masking-her-face-with-mud-in-beauty-spa-illustration-painted-with-watercolor-skin-care-beauty-treatment-png.png", name: "mudmask", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481436/mudmask_okwzbn"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/024/476/615/non_2x/watercolor-funny-pug-dog-wearing-sunglasses-ai-generated-png.png", name: "pug", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481438/pug_odzmnd"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/050/280/917/non_2x/3d-style-illustration-of-asia-young-man-in-office-worker-uniform-long-hair-he-is-stressed-isolated-on-transparent-background-png.png", name: "slash", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481442/slash_ifncer"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/053/647/262/non_2x/a-lighthearted-and-humorous-portrait-of-a-teenage-boy-grimacing-with-a-funny-face-perfect-for-social-media-and-youth-related-content-creation-png.png", name: "twistedface", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481439/twistedface_s6lk5c"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/044/811/950/non_2x/grumpy-faced-animal-grumpy-faced-animal-clipart-perfect-for-crafting-card-making-and-more-this-adorable-collection-features-a-variety-of-animals-sporting-hilariously-grumpy-expressions-png.png", name: "grumpycat", type: "mask", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481433/grumpycat_v7lamn"),
            
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/024/728/931/non_2x/3d-illustration-of-a-cloud-with-rain-png.png", name: "rain", type: "face", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481436/rain_psh5kn"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/053/715/291/non_2x/a-mesmerizing-digital-illustration-of-a-swirling-vortex-in-shades-of-blue-and-white-depicting-movement-and-energy-in-an-abstract-design-png.png", name: "blizzard", type: "face", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481430/blizzard_ulm4wu"),
            
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/047/246/581/non_2x/film-roll-3d-illustration-png.png", name: "filmcolorperfection", type: "background", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481431/filmcolorperfection_s5vhul"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/044/450/773/non_2x/retro-television-with-transparent-background-png.png", name: "tv80", type: "background", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481439/tv80_ohjshi"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/024/831/271/non_2x/colorful-comic-radial-speed-lines-png.png", name: "drawingmanga", type: "background", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481431/drawingmanga_o2gkyk"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/050/478/778/non_2x/brown-watercolor-texture-on-white-cut-out-stock-png.png", name: "sepia", type: "background", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481437/sepia_ggvuow"),
            
            FaceModel(image: "https://static.vecteezy.com/system/resources/previews/048/220/297/non_2x/yamamoto-bleach-free-png.png", name: "bleachbypass", type: "background", url: "https://res.cloudinary.com/dyvrnfxhp/raw/upload/v1740481428/bleachbypass_m3ogfk")
        ]
    }
    
    static func getFilterPath(filterName: String, filterUrl: String, completion: @escaping (String?) -> Void) {
        if filterName == "none" {
            completion(nil)
            return
        }
        
        guard let url = URL(string: filterUrl) else {
            print("Invalid URL: \(filterUrl)")
            completion(nil)
            return
        }
        
        let fileName = url.lastPathComponent
        
        saveFileAsync(folder: getFolderPath(), fileName: fileName, url: filterUrl, type: "filter") { path in
            completion(path)
        }
    }
    
    static func saveFileAsync(folder: String, fileName: String, url: String, type: String, completion: @escaping (String?) -> Void) {
        let folderURL = URL(fileURLWithPath: folder, isDirectory: true)
        
        // Ensure folder exists
        if !fileManager.fileExists(atPath: folderURL.path) {
            do {
                try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
            } catch {
                print("Failed to create directory: \(error.localizedDescription)")
                completion(nil)
                return
            }
        }
        
        let fileURL = folderURL.appendingPathComponent(fileName).appendingPathExtension("deepar")
        
        // If file already exists, return it
        if fileManager.fileExists(atPath: fileURL.path) {
            completion(fileURL.path)
            return
        }
        
        let destination: DownloadRequest.Destination = { _, _ in
            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }
        
        AF.download(url, to: destination).response { response in
            switch response.result {
            case .success(let filePath):
                print("File downloaded to: \(filePath?.path ?? "No path")")
                completion(fileURL.path)
            case .failure(let error):
                print("Download error: \(error.localizedDescription)")
                completion(nil)
            }
        }
    }
    
    static func getDocumentDirectoryPath() -> URL {
        return fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
    }
    
    static func getFolderPath() -> String {
        let path = getDocumentDirectoryPath()
            .appendingPathComponent("Wowonder Messenger")
            .appendingPathComponent("File")
        return path.path
    }
    
}
