//
//  UIViewController+Ex.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/30.
//

extension UIViewController {
    func showCustomAlert(
        title: String? = "",
        message: String?,
        deleteHandler: (() -> Void)? = nil,
        cancelHandler: (() -> Void)? = nil
    ) {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
        
        // 自定义标题
        if let title = title {
            let titleAttributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.boldSystemFont(ofSize: 20),
                .foregroundColor: UIColor.white
            ]
            let attributedTitle = NSAttributedString(string: title, attributes: titleAttributes)
            alert.setValue(attributedTitle, forKey: "attributedTitle")
        }
        
        // 自定义消息
        if let message = message {
            let messageAttributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.boldSystemFont(ofSize: 17),
                .foregroundColor: UIColor.white
            ]
            let attributedMessage = NSAttributedString(string: message, attributes: messageAttributes)
            alert.setValue(attributedMessage, forKey: "attributedMessage")
        }
        
        // 添加按钮
        let cancelAction = UIAlertAction(title: "Cancel".localized, style: .cancel) { _ in
            cancelHandler?()
        }
        alert.addAction(cancelAction)
        
        let deleteAction = UIAlertAction(title: "Delete".localized, style: .destructive) { _ in
            deleteHandler?()
        }
        alert.addAction(deleteAction)
        
        // 设置黑暗模式
        alert.overrideUserInterfaceStyle = .dark
        
        // 显示弹窗
        present(alert, animated: true, completion: nil)
    }
}
