//
//  InlineTrendsViewController.swift
//  Tusker
//
//  Created by Shadowfacts on 6/24/20.
//  Copyright © 2020 Shadowfacts. All rights reserved.
//

import UIKit

class InlineTrendsViewController: UIViewController {

    weak var mastodonController: MastodonController!
    
    var resultsController: SearchResultsViewController!
    var searchController: UISearchController!
    
    var searchControllerStatusOnAppearance: Bool? = nil
    
    init(mastodonController: MastodonController) {
        self.mastodonController = mastodonController
        
        super.init(nibName: nil, bundle: nil)
        
        title = NSLocalizedString("Explore", comment: "explore tab title")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        resultsController = SearchResultsViewController(mastodonController: mastodonController)
        resultsController.exploreNavigationController = self.navigationController
        searchController = MastodonSearchController(searchResultsController: resultsController)
        searchController.obscuresBackgroundDuringPresentation = true
        searchController.hidesNavigationBarDuringPresentation = false
        definesPresentationContext = true

        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
        if #available(iOS 16.0, *) {
            navigationItem.preferredSearchBarPlacement = .stacked
        }
        
        let trends = TrendsViewController(mastodonController: mastodonController)
        trends.view.translatesAutoresizingMaskIntoConstraints = false
        addChild(trends)
        view.addSubview(trends.view)
        NSLayoutConstraint.activate([
            trends.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            trends.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            trends.view.topAnchor.constraint(equalTo: view.topAnchor),
            trends.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
        trends.didMove(toParent: self)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // this is a workaround for the issue that setting isActive on a search controller that is not visible
        // does not cause it to automatically become active once it becomes visible
        // see FB7814561
        if let active = searchControllerStatusOnAppearance {
            searchController.isActive = active
            searchControllerStatusOnAppearance = nil
        }
    }
    
}

extension InlineTrendsViewController: StateRestorableViewController {
    func stateRestorationActivity() -> NSUserActivity? {
        if searchController.isActive {
            return UserActivityManager.searchActivity(query: searchController.searchBar.text, accountID: mastodonController.accountInfo!.id)
        } else {
            return nil
        }
    }
}
