//
//  MainTabBarViewController.swift
//  Tusker
//
//  Created by Shadowfacts on 8/21/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import UIKit
import ComposeUI

class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
    
    weak var mastodonController: MastodonController!
    
    private var composePlaceholder: UIViewController!
    
    private var fastAccountSwitcher: FastAccountSwitcherViewController!
    private var fastSwitcherIndicator: FastAccountSwitcherIndicatorView!
    private var fastSwitcherConstraints: [NSLayoutConstraint] = []
    
    @available(iOS, obsoleted: 16.0)
    private var draftToPresentOnAppear: Draft?
    
    var selectedTab: Tab {
        return Tab(rawValue: selectedIndex)!
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .portrait
        } else {
            return .all
        }
    }
    
    init(mastodonController: MastodonController) {
        self.mastodonController = mastodonController
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
	}
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        stateRestorationLogger.info("MainTabBarViewController: viewDidLoad, selectedIndex=\(self.selectedIndex, privacy: .public)")

        self.delegate = self
        
        composePlaceholder = UIViewController()
        composePlaceholder.title = "Compose"
        composePlaceholder.tabBarItem.image = UIImage(systemName: "pencil")
        
        viewControllers = [
            embedInNavigationController(Tab.timelines.createViewController(mastodonController)),
            embedInNavigationController(Tab.notifications.createViewController(mastodonController)),
            composePlaceholder,
            embedInNavigationController(Tab.explore.createViewController(mastodonController)),
            embedInNavigationController(Tab.myProfile.createViewController(mastodonController)),
        ]
        
        fastAccountSwitcher = FastAccountSwitcherViewController()
        fastAccountSwitcher.delegate = self
        fastAccountSwitcher.view.translatesAutoresizingMaskIntoConstraints = false
        
        tabBar.addGestureRecognizer(fastAccountSwitcher.createSwitcherGesture())
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tabBarTapped))
        tapRecognizer.cancelsTouchesInView = false
        tabBar.addGestureRecognizer(tapRecognizer)
        
        if findMyProfileTabBarButton() != nil {
            fastSwitcherIndicator = FastAccountSwitcherIndicatorView()
            fastSwitcherIndicator.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(fastSwitcherIndicator)
        }
        
        tabBar.isSpringLoaded = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        stateRestorationLogger.info("MainTabBarViewController: viewWillAppear, selectedIndex=\(self.selectedIndex, privacy: .public)")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        stateRestorationLogger.info("MainTabBarViewController: viewDidAppear, selectedIndex=\(self.selectedIndex, privacy: .public)")
        
        if let draftToPresentOnAppear {
            self.draftToPresentOnAppear = nil
            compose(editing: draftToPresentOnAppear, animated: true)
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        // i hate that we have to do this so often :S
        // but doing it only in viewWillAppear makes it not appear initially
        // doing it in viewWillAppear inside a DispatchQueue.main.async works initially but then it disappears when long-pressed
        repositionFastSwitcherIndicator()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        repositionFastSwitcherIndicator()
    }
    
    func select(tab: Tab, dismissPresented: Bool) {
        if tab == .compose {
            compose(editing: nil)
        } else {
            // when switching tabs, dismiss the currently presented VC
            // otherwise the selected tab changes behind the presented VC
            if presentedViewController != nil && dismissPresented {
                dismiss(animated: true) {
                    self.selectedIndex = tab.rawValue
                }
            } else {
                stateRestorationLogger.info("MainTabBarViewController: selecting \(String(describing: tab), privacy: .public)")
                selectedIndex = tab.rawValue
            }
        }
    }
    
    override func show(_ vc: UIViewController, sender: Any?) {
        if let nav = selectedViewController as? UINavigationController {
            nav.pushViewController(vc, animated: true)
        } else {
            present(vc, animated: true)
        }
    }
    
    private func repositionFastSwitcherIndicator() {
        guard let myProfileButton = findMyProfileTabBarButton() else {
            return
        }
        NSLayoutConstraint.deactivate(fastSwitcherConstraints)
        let isPortrait = view.bounds.width < view.bounds.height
        if traitCollection.horizontalSizeClass == .compact && isPortrait {
            fastSwitcherConstraints = [
                fastSwitcherIndicator.centerYAnchor.constraint(equalTo: myProfileButton.centerYAnchor, constant: -4),
                // tab bar button image width is 30
                fastSwitcherIndicator.leftAnchor.constraint(equalTo: myProfileButton.centerXAnchor, constant: 15 + 2),
            ]
        } else {
            fastSwitcherConstraints = [
                fastSwitcherIndicator.centerYAnchor.constraint(equalTo: myProfileButton.centerYAnchor),
                fastSwitcherIndicator.trailingAnchor.constraint(equalTo: myProfileButton.trailingAnchor),
            ]
        }
        NSLayoutConstraint.activate(fastSwitcherConstraints)
    }
    
    private func findMyProfileTabBarButton() -> UIView? {
        let tabBarButtons = tabBar.subviews.filter { String(describing: type(of: $0)).lowercased().contains("button") }
        // sanity check that there is 1 button per VC
        guard tabBarButtons.count == viewControllers!.count,
              let myProfileButton = tabBarButtons.last else {
            return nil
        }
        return myProfileButton
    }
    
    @objc private func tabBarTapped(_ recognizer: UITapGestureRecognizer) {
        fastAccountSwitcher.hide()
    }
    
    @objc func handleComposeKeyCommand() {
        compose(editing: nil)
    }
    
    func embedInNavigationController(_ vc: UIViewController) -> UINavigationController {
        if let vc = vc as? UINavigationController {
            return vc
        } else {
            let nav = EnhancedNavigationViewController(rootViewController: vc)
            return nav
        }
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController == composePlaceholder {
            compose(editing: nil)
            return false
        }
        if viewController == viewControllers![selectedIndex],
           let nav = viewController as? UINavigationController,
           nav.viewControllers.count == 1,
           let scrollableVC = nav.viewControllers.first as? TabBarScrollableViewController {
            scrollableVC.tabBarScrollToTop()
            return false
        }
        return true
    }
    
    func setViewController(_ viewController: UIViewController, forTab tab: Tab) {
        viewControllers![tab.rawValue] = viewController
    }
    
    func viewController(for tab: Tab) -> UIViewController {
        return viewControllers![tab.rawValue]
    }
}

extension MainTabBarViewController {
    enum Tab: Int, Hashable, CaseIterable {
        case timelines
        case notifications
        case compose
        case explore
        case myProfile
        
        func createViewController(_ mastodonController: MastodonController) -> UIViewController {
            switch self {
            case .timelines:
                return TimelinesPageViewController(mastodonController: mastodonController)
            case .notifications:
                return NotificationsPageViewController(mastodonController: mastodonController)
            case .compose:
                return ComposeHostingController(draft: nil, mastodonController: mastodonController)
            case .explore:
                return ExploreViewController(mastodonController: mastodonController)
            case .myProfile:
                return MyProfileViewController(mastodonController: mastodonController)
            }
        }
    }
    
    func getTabController(tab: Tab) -> UIViewController? {
        if tab == .compose {
            return nil
        } else {
            // viewWControllers array is setup in viewDidLoad
            loadViewIfNeeded()
            return viewControllers![tab.rawValue]
        }
    }
}

extension MainTabBarViewController: FastAccountSwitcherViewControllerDelegate {
    func fastAccountSwitcherAddToViewHierarchy(_ fastAccountSwitcher: FastAccountSwitcherViewController) {
        view.addSubview(fastAccountSwitcher.view)
        NSLayoutConstraint.activate([
            fastAccountSwitcher.accountsStack.bottomAnchor.constraint(equalTo: fastAccountSwitcher.view.bottomAnchor),
            
            fastAccountSwitcher.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            fastAccountSwitcher.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            fastAccountSwitcher.view.topAnchor.constraint(equalTo: view.topAnchor),
            fastAccountSwitcher.view.bottomAnchor.constraint(equalTo: tabBar.topAnchor),
        ])
    }
    
    func fastAccountSwitcher(_ fastAccountSwitcher: FastAccountSwitcherViewController, triggerZoneContains point: CGPoint) -> Bool {
        guard let myProfileButton = findMyProfileTabBarButton() else {
            return false
        }
        let locationInButton = myProfileButton.convert(point, from: tabBar)
        return myProfileButton.bounds.contains(locationInButton)
    }
}

extension MainTabBarViewController: TuskerNavigationDelegate {
    var apiController: MastodonController! { mastodonController }
}

extension MainTabBarViewController: StateRestorableViewController {
    func stateRestorationActivity() -> NSUserActivity? {
        var activity: NSUserActivity?
        if let presentedNav = presentedViewController as? UINavigationController,
           let compose = presentedNav.viewControllers.first as? ComposeHostingController {
            let draft = compose.controller.draft
            activity = UserActivityManager.editDraftActivity(id: draft.id, accountID: draft.accountID)
        } else if let vc = (selectedViewController as! UINavigationController).topViewController as? StateRestorableViewController {
            activity = vc.stateRestorationActivity()
        }
        if activity == nil {
            stateRestorationLogger.fault("MainTabBarViewController: Unable to create state restoration activity, couldn't find StateRestorableViewController")
        }
        return activity
    }
}

extension MainTabBarViewController: TuskerRootViewController {
    func select(route: TuskerRoute, animated: Bool) {
        switch route {
        case .timelines:
            select(tab: .timelines, dismissPresented: true)
        case .notifications:
            select(tab: .notifications, dismissPresented: true)
        case .myProfile:
            select(tab: .myProfile, dismissPresented: true)
        case .explore:
            select(tab: .explore, dismissPresented: true)
        case .bookmarks:
            select(tab: .explore, dismissPresented: true)
            getNavigationController().pushViewController(BookmarksViewController(mastodonController: mastodonController), animated: animated)
        case .list(id: let id):
            select(tab: .explore, dismissPresented: true)
            if let list = mastodonController.getCachedList(id: id) {
                let nav = getNavigationController()
                _ = nav.popToRootViewController(animated: animated)
                nav.pushViewController(ListTimelineViewController(for: list, mastodonController: mastodonController), animated: animated)
            }
        }
    }
    
    func getNavigationDelegate() -> TuskerNavigationDelegate? {
        return self
    }
    
    func getNavigationController() -> NavigationControllerProtocol {
        return (selectedViewController as! UINavigationController)
    }
    
    func performSearch(query: String) {
        guard let exploreNavController = getTabController(tab: .explore) as? UINavigationController,
              let exploreController = exploreNavController.viewControllers.first as? ExploreViewController else {
            return
        }
        
        select(tab: .explore, dismissPresented: true)
        exploreNavController.popToRootViewController(animated: false)
        
        // setting searchController.isActive directly doesn't work until the view has loaded/appeared for the first time
        if exploreController.isViewLoaded {
            exploreController.searchController.isActive = true
        } else {
            exploreController.searchControllerStatusOnAppearance = true
            // we still need to load the view so that we can setup the search query
            exploreController.loadViewIfNeeded()
        }
        
        exploreController.searchController.searchBar.text = query
        exploreController.resultsController.performSearch(query: query)
    }
    
    func presentPreferences(completion: (() -> Void)?) {
        present(PreferencesNavigationController(mastodonController: mastodonController), animated: true, completion: completion)
    }
    
    func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
        guard presentedViewController == nil else {
            return .stop
        }
        guard let vc = viewController(for: selectedTab) as? StatusBarTappableViewController else {
            return .continue
        }
        return vc.handleStatusBarTapped(xPosition: xPosition)
    }
}

extension MainTabBarViewController: BackgroundableViewController {
    func sceneDidEnterBackground() {
        if let selectedVC = selectedViewController as? BackgroundableViewController {
            selectedVC.sceneDidEnterBackground()
        }
    }
}

extension MainTabBarViewController: AccountSwitchableViewController {
    var isFastAccountSwitcherActive: Bool {
        if let fastAccountSwitcher {
            return !fastAccountSwitcher.view.isHidden
        } else {
            return false
        }
    }
}
