<commit_before>//
//  FeedListViewController.swift
//  Evergreen
//
//  Created by <NAME> on 11/1/17.
//  Copyright © 2017 Ranchero Software. All rights reserved.
//

import Cocoa
import RSTree
import RSCore

final class FeedListViewController: NSViewController {

	@IBOutlet var outlineView: NSOutlineView!
	private var sidebarCellAppearance: SidebarCellAppearance!
	private let treeControllerDelegate = FeedListTreeControllerDelegate()
	lazy var treeController: TreeController = {
		TreeController(delegate: treeControllerDelegate)
	}()

	// MARK: NSViewController

	override func viewDidLoad() {

		sidebarCellAppearance = SidebarCellAppearance(theme: appDelegate.currentTheme, fontSize: AppDefaults.shared.sidebarFontSize)
		NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
	}

	override func viewWillLayout() {
		super.viewWillLayout()
	}
	// MARK: - Notifications

	@objc func faviconDidBecomeAvailable(_ note: Notification) {

		configureAvailableCells()
	}
}

// MARK: - NSOutlineViewDataSource

extension FeedListViewController: NSOutlineViewDataSource {

	func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
		
		return nodeForItem(item as AnyObject?).numberOfChildNodes
	}

	func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {

		return nodeForItem(item as AnyObject?).childNodes![index]
	}

	func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {

		return nodeForItem(item as AnyObject?).canHaveChildNodes
	}

	private func nodeForItem(_ item: AnyObject?) -> Node {

		if item == nil {
			return treeController.rootNode
		}
		return item as! Node
	}
}

// MARK: - NSOutlineViewDelegate

extension FeedListViewController: NSOutlineViewDelegate {

	func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {

		let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "FeedListCell"), owner: self) as! SidebarCell

		let node = item as! Node
		configure(cell, node)

		return cell
	}

	func outlineViewSelectionDidChange(_ notification: Notification) {

//		// TODO: support multiple selection
//
//		let selectedRow = self.outlineView.selectedRow
//
//		if selectedRow < 0 || selectedRow == NSNotFound {
//			postSidebarSelectionDidChangeNotification(nil)
//			return
//		}
//
//		if let selectedNode = self.outlineView.item(atRow: selectedRow) as? Node {
//			postSidebarSelectionDidChangeNotification([selectedNode.representedObject])
//		}
	}

	private func configure(_ cell: SidebarCell, _ node: Node) {

		cell.cellAppearance = sidebarCellAppearance
		cell.objectValue = node
		cell.name = nameFor(node)
		cell.image = imageFor(node)
		cell.shouldShowImage = node.representedObject is FeedListFeed
	}

	func imageFor(_ node: Node) -> NSImage? {

		guard let feed = node.representedObject as? FeedListFeed else {
			return nil
		}
		return appDelegate.faviconDownloader.favicon(withHomePageURL: feed.homePageURL)
	}

	func nameFor(_ node: Node) -> String {

		if let displayNameProvider = node.representedObject as? DisplayNameProvider {
			return displayNameProvider.nameForDisplay<commit_msg>Use a standard folder icon the Feed Directory sidebar.<commit_after>//
//  FeedListViewController.swift
//  Evergreen
//
//  Created by <NAME> on 11/1/17.
//  Copyright © 2017 Ranchero Software. All rights reserved.
//

import Cocoa
import RSTree
import RSCore

final class FeedListViewController: NSViewController {

	@IBOutlet var outlineView: NSOutlineView!
	private var sidebarCellAppearance: SidebarCellAppearance!
	private let treeControllerDelegate = FeedListTreeControllerDelegate()
	lazy var treeController: TreeController = {
		TreeController(delegate: treeControllerDelegate)
	}()

	// MARK: NSViewController

	override func viewDidLoad() {

		sidebarCellAppearance = SidebarCellAppearance(theme: appDelegate.currentTheme, fontSize: AppDefaults.shared.sidebarFontSize)
		NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
	}

	override func viewWillLayout() {
		super.viewWillLayout()
	}
	// MARK: - Notifications

	@objc func faviconDidBecomeAvailable(_ note: Notification) {

		configureAvailableCells()
	}
}

// MARK: - NSOutlineViewDataSource

extension FeedListViewController: NSOutlineViewDataSource {

	func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
		
		return nodeForItem(item as AnyObject?).numberOfChildNodes
	}

	func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {

		return nodeForItem(item as AnyObject?).childNodes![index]
	}

	func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {

		return nodeForItem(item as AnyObject?).canHaveChildNodes
	}

	private func nodeForItem(_ item: AnyObject?) -> Node {

		if item == nil {
			return treeController.rootNode
		}
		return item as! Node
	}
}

// MARK: - NSOutlineViewDelegate

extension FeedListViewController: NSOutlineViewDelegate {

	func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {

		let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "FeedListCell"), owner: self) as! SidebarCell

		let node = item as! Node
		configure(cell, node)

		return cell
	}

	func outlineViewSelectionDidChange(_ notification: Notification) {

//		// TODO: support multiple selection
//
//		let selectedRow = self.outlineView.selectedRow
//
//		if selectedRow < 0 || selectedRow == NSNotFound {
//			postSidebarSelectionDidChangeNotification(nil)
//			return
//		}
//
//		if let selectedNode = self.outlineView.item(atRow: selectedRow) as? Node {
//			postSidebarSelectionDidChangeNotification([selectedNode.representedObject])
//		}
	}

	private func configure(_ cell: SidebarCell, _ node: Node) {

		cell.cellAppearance = sidebarCellAppearance
		cell.objectValue = node
		cell.name = nameFor(node)
		cell.image = imageFor(node)
		cell.shouldShowImage = true
	}

	func imageFor(_ node: Node) -> NSImage? {

		if let _ = node.representedObject as? FeedListFolder {
			return NSImage(named: NSImage.Name.folder)
		}
		else if let feed = node.representedObject as? FeedListFeed {
			return appDelegate.faviconDownloader.favicon(withHomePageURL: feed.homePageURL)
		}
		return nil
	}

	func nameFor(_ node: Node) -> String {

		if let displayNameProvider = node.representedObject as? DisplayNameProvider {
			return displayNameProvider.nameForDisplay
{'fraction_non_alphanumeric': 0.09461274436651246, 'fraction_numerical': 0.0029846291598268917, 'mean_word_length': 4.822762814943528, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6431385', 'n_tokens_mistral': 2167, 'n_tokens_neox': 1951, 'n_words': 493}