//
//  EnhancedTableViewController.swift
//  Tusker
//
//  Created by Shadowfacts on 11/10/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import UIKit
import SafariServices

class EnhancedTableViewController: UITableViewController {

    var dragEnabled = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if dragEnabled {
            tableView.dragDelegate = self
        }
    }
    
    // MARK: Table View Delegate

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? SelectableTableViewCell {
            cell.didSelectCell()
        }
    }

}

extension EnhancedTableViewController {
    
    override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        if let cell = tableView.cellForRow(at: indexPath) as? UITableViewCell & MenuPreviewProvider {
            let cellLocation = cell.convert(point, from: tableView)
            guard let (previewProvider, actionsProvider) = cell.getPreviewProviders(for: cellLocation, sourceViewController: self) else {
                return nil
            }
            let actionProvider: UIContextMenuActionProvider = { (_) in
                let suggested = self.getSuggestedContextMenuActions(tableView: tableView, indexPath: indexPath, point: point)
                return UIMenu(title: "", image: nil, identifier: nil, options: [], children: suggested + actionsProvider())
            }
            return UIContextMenuConfiguration(identifier: nil, previewProvider: previewProvider, actionProvider: actionProvider)
        } else {
            return nil
        }
    }
    
    // todo: replace this with the UIKit suggested actions, if possible
    @objc open func getSuggestedContextMenuActions(tableView: UITableView, indexPath: IndexPath, point: CGPoint) -> [UIAction] {
        return []
    }
    
    override func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
        if let viewController = animator.previewViewController {
            animator.preferredCommitStyle = .pop
            animator.addCompletion {
                if let customPresenting = viewController as? CustomPreviewPresenting {
                    customPresenting.presentFromPreview(presenter: self)
                } else {
                    self.show(viewController, sender: nil)
                }
            }
        }
    }
    
}

extension EnhancedTableViewController: UITableViewDragDelegate {
    func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        guard let cell = tableView.cellForRow(at: indexPath) as? DraggableTableViewCell else {
            return []
        }
        return cell.dragItemsForBeginning(session: session)
    }
}

extension EnhancedTableViewController: TabBarScrollableViewController {
    func tabBarScrollToTop() {
        tableView.scrollToTop()
    }
}

extension EnhancedTableViewController: StatusBarTappableViewController {
    func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
        tableView.scrollToTop()
        return .stop
    }
}
