//
//  UIImageView+Ex.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//
import Kingfisher
extension UIImageView {

    /// 创建并配置 UIImageView
    /// - Parameters:
    ///   - imageName: 图片名称
    ///   - contentMode: 内容模式，默认为 `.scaleAspectFit`
    ///   - backgroundColor: 背景颜色，默认为透明
    /// - Returns: 配置完成的 UIImageView 实例
    static func createImageView(imageName: String,
                                contentMode: UIView.ContentMode = .scaleAspectFit,
                                backgroundColor: UIColor = .clear,
                                corner: CGFloat = 0.0) -> UIImageView {
        let imageView = UIImageView()
        imageView.image = UIImage(named: imageName)
        imageView.contentMode = contentMode
        imageView.backgroundColor = backgroundColor
        imageView.cornerRadius = corner
        return imageView
    }
    
    /// 异步创建并加载图片的 UIImageView
    /// - Parameters:
    ///   - imageName: 本地占位图片名称
    ///   - urlString: 图片的 URL 字符串
    ///   - contentMode: 内容模式，默认为 `.scaleAspectFit`
    ///   - backgroundColor: 背景颜色，默认为透明
    ///   - showLoading: 是否显示加载动画，默认为 `true`
    ///   - completion: 图片加载成功后的回调
    static func createAsyncImageView(urlString: String?,
                                     placeholder:UIImage?,
                                     contentMode: UIView.ContentMode = .scaleAspectFit,
                                     backgroundColor: UIColor = .clear,
                                     showLoading: Bool = false,
                                     completion: ((UIImage?) -> Void)? = nil) -> UIImageView {
        let imageView = UIImageView()
        imageView.setAsyncImage(urlString: urlString,
                                placeholder:placeholder,
                                contentMode:contentMode,
                                backgroundColor:backgroundColor,
                                showLoading:showLoading,
                                completion:completion)
        return imageView
    }

    
    
    func setAsyncImage(urlString: String?,
                     placeholder: UIImage? = nil,
                     contentMode: UIView.ContentMode = .scaleAspectFill,
                 backgroundColor: UIColor = .clear,
                       showLoading: Bool = false,
                      completion: ((UIImage?) -> Void)? = nil){
        let imageView = self
        imageView.contentMode = contentMode
        imageView.backgroundColor = backgroundColor
        
        guard let urlString = urlString, let url = URL(string: urlString) else {
            completion?(nil)
            return
        }
        

        if urlString.contains("http") {
            kf.indicatorType = showLoading ? .activity : .none
            imageView.kf.setImage(with: url,
                 placeholder: placeholder,
                     options: nil,
               progressBlock: nil) { result in
                
                if let image = try? result.get().image {
                    completion?(image)
                }else{
                    completion?(nil)
                }
            }
            
        }else if urlString.contains("/") {
            imageView.image = placeholder
            imageView.image = UIImage(contentsOfFile: urlString.fillCachePath)
        }else {
            imageView.image = placeholder
            if let image = UIImage(named: urlString) {
                imageView.image = image
            }
        }
    }

}

extension UIImageView {
    
    static func createRightArrow() -> UIImageView {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "white_right_arrow")
        return imageView
    }
}

extension UIImageView {
    func setImage(_ image: UIImage?, duration: CFTimeInterval = 0.2, animated: Bool = true) {
        if let image = image {
            if animated {
                UIView.transition(
                    with: self,
                    duration: duration,
                    options: [.transitionCrossDissolve, .curveEaseInOut, .allowUserInteraction]
                ) {
                    self.image = image
                }
            }else {
                self.image = image
            }
        }
    }
}
