//
//  Duckable+Root.swift
//  Tusker
//
//  Created by Shadowfacts on 11/7/22.
//  Copyright © 2022 Shadowfacts. All rights reserved.
//

import UIKit
import Duckable
import ComposeUI

@available(iOS 16.0, *)
extension DuckableContainerViewController: AccountSwitchableViewController {
    func stateRestorationActivity() -> NSUserActivity? {
        var activity = (child as? TuskerRootViewController)?.stateRestorationActivity()
        if let compose = duckedViewController as? ComposeHostingController,
           compose.controller.draft.hasContent {
            activity = UserActivityManager.addDuckedDraft(to: activity, draft: compose.controller.draft)
        }
        return activity
    }
    
    func compose(editing draft: Draft?, animated: Bool, isDucked: Bool) {
        (child as? TuskerRootViewController)?.compose(editing: draft, animated: animated, isDucked: isDucked)
    }
    
    func getNavigationDelegate() -> TuskerNavigationDelegate? {
        (child as? TuskerRootViewController)?.getNavigationDelegate()
    }
    
    func getNavigationController() -> NavigationControllerProtocol {
        (child as! TuskerRootViewController).getNavigationController()
    }
    
    func select(route: TuskerRoute, animated: Bool) {
        (child as? TuskerRootViewController)?.select(route: route, animated: animated)
    }
    
    func getTabController(tab: MainTabBarViewController.Tab) -> UIViewController? {
        return (child as? TuskerRootViewController)?.getTabController(tab: tab)
    }
    
    func performSearch(query: String) {
        (child as? TuskerRootViewController)?.performSearch(query: query)
    }
    
    func presentPreferences(completion: (() -> Void)?) {
        (child as? TuskerRootViewController)?.presentPreferences(completion: completion)
    }
    
    func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
        (child as? TuskerRootViewController)?.handleStatusBarTapped(xPosition: xPosition) ?? .continue
    }
    
    var isFastAccountSwitcherActive: Bool {
        (child as? AccountSwitchableViewController)?.isFastAccountSwitcherActive ?? false
    }
}
