//
//  UIAlertController+Visibility.swift
//  Tusker
//
//  Created by Shadowfacts on 10/25/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import UIKit
import Pachyderm

extension UIAlertController {
    
    convenience init(currentVisibility: Visibility?, completion: @escaping (Visibility?) -> Void) {
        self.init(title: "Post Visibility", message: nil, preferredStyle: .actionSheet)
        
        for visibility in Visibility.allCases {
            let action = UIAlertAction(title: visibility.displayName, style: .default) { (_) in
                completion(visibility)
            }
            if visibility == currentVisibility {
                action.setValue(true, forKey: "checked")
            }
            action.setValue(UIImage(systemName: visibility.imageName), forKey: "image")
            addAction(action)
        }
        
        addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
            completion(nil)
        }))
    }
    
}
