//
//  OpenInSafariActivity.swift
//  Tusker
//
//  Created by Shadowfacts on 9/5/19.
//  Copyright © 2019 Shadowfacts. All rights reserved.
//

import UIKit
import SafariServices

class OpenInSafariActivity: UIActivity {

    override class var activityCategory: UIActivity.Category {
        return .action
    }
    override var activityType: UIActivity.ActivityType? {
        return .openInSafari
    }
    
    override var activityTitle: String? {
        return NSLocalizedString("Open in Safari", comment: "open in safari activity title")
    }
    override var activityImage: UIImage? {
        return UIImage(systemName: "safari")
    }
    
    override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        for case is URL in activityItems {
            return true
        }
        return false
    }
    
    override func perform() {
        activityDidFinish(true)
    }
    
    static func completionHandler(navigator: TuskerNavigationDelegate, url: URL) -> UIActivityViewController.CompletionWithItemsHandler {
        return { (activityType, _, _, _) in
            if activityType == .openInSafari {
                MainActor.runUnsafely {
                    navigator.selected(url: url, allowResolveStatuses: false, allowUniversalLinks: false)
                }
            }
        }
    }
    
}
