<commit_before>//
//  SearchLyricsViewController.swift
//
//  This file is part of LyricsX
//  Copyright (C) 2017 <NAME> - https://github.com/ddddxxx/LyricsX
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import Cocoa
import LyricsProvider
import MusicPlayer
import Crashlytics

class SearchLyricsViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
    
    var imageCache = NSCache<NSURL, NSImage>()
    
    @objc dynamic var searchArtist = ""
    @objc dynamic var searchTitle = "" {
        didSet {
            searchButton.isEnabled = searchTitle.count > 0
        }
    }
    @objc dynamic var selectedIndex = NSIndexSet()
    
    let lyricsManager = LyricsProviderManager()
    var searchTask: LyricsSearchTask?
    var searchResult: [Lyrics] = []
    
    @IBOutlet weak var artworkView: NSImageView!
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var searchButton: NSButton!
    @IBOutlet weak var progressIndicator: NSProgressIndicator!
    @IBOutlet var lyricsPreviewTextView: NSTextView!
    
    @IBOutlet weak var hideLrcPreviewConstraint: NSLayoutConstraint?
    @IBOutlet var normalConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.setDraggingSourceOperationMask(.copy, forLocal: false)
        normalConstraint.isActive = false
        
        autoFillSearchFieldAndSearch()
    }
    
    func autoFillSearchFieldAndSearch() {
        let track = AppController.shared.playerManager.player?.currentTrack
        let artist = track?.artist ?? ""
        let title = track?.title ?? ""
        if (searchArtist, searchTitle) != (artist, title) {
            (searchArtist, searchTitle) = (artist, title)
            searchAction(nil)
        }
    }
    
    @IBAction func searchAction(_ sender: Any?) {
        searchTask?.cancel()
        searchResult = []
        
        progressIndicator.startAnimation(nil)
        progressIndicator.isHidden = false
        let track = AppController.shared.playerManager.player?.currentTrack
        let duration = track?.duration ?? 0
        let title = track?.title ?? ""
        let artist = track?.artist ?? ""
        let req = LyricsSearchRequest(searchTerm: .info(title: title, artist: artist),
                                      title: title,
                                      artist: artist,
                                      duration: duration,<commit_msg>fix: cannot modify search term<commit_after>//
//  SearchLyricsViewController.swift
//
//  This file is part of LyricsX
//  Copyright (C) 2017 <NAME> - https://github.com/ddddxxx/LyricsX
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import Cocoa
import LyricsProvider
import MusicPlayer
import Crashlytics

class SearchLyricsViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
    
    var imageCache = NSCache<NSURL, NSImage>()
    
    @objc dynamic var searchArtist = ""
    @objc dynamic var searchTitle = "" {
        didSet {
            searchButton.isEnabled = searchTitle.count > 0
        }
    }
    @objc dynamic var selectedIndex = NSIndexSet()
    
    let lyricsManager = LyricsProviderManager()
    var searchTask: LyricsSearchTask?
    var searchResult: [Lyrics] = []
    
    @IBOutlet weak var artworkView: NSImageView!
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var searchButton: NSButton!
    @IBOutlet weak var progressIndicator: NSProgressIndicator!
    @IBOutlet var lyricsPreviewTextView: NSTextView!
    
    @IBOutlet weak var hideLrcPreviewConstraint: NSLayoutConstraint?
    @IBOutlet var normalConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.setDraggingSourceOperationMask(.copy, forLocal: false)
        normalConstraint.isActive = false
        
        autoFillSearchFieldAndSearch()
    }
    
    func autoFillSearchFieldAndSearch() {
        let track = AppController.shared.playerManager.player?.currentTrack
        let artist = track?.artist ?? ""
        let title = track?.title ?? ""
        if (searchArtist, searchTitle) != (artist, title) {
            (searchArtist, searchTitle) = (artist, title)
            searchAction(nil)
        }
    }
    
    @IBAction func searchAction(_ sender: Any?) {
        searchTask?.cancel()
        searchResult = []
        
        progressIndicator.startAnimation(nil)
        progressIndicator.isHidden = false
        let track = AppController.shared.playerManager.player?.currentTrack
        let duration = track?.duration ?? 0
        let title = track?.title ?? ""
        let artist = track?.artist ?? ""
        let req = LyricsSearchRequest(searchTerm: .info(title: searchTitle, artist: searchArtist),
                                      title: title,
                                      artist: artist,
                                      duration: duration,
{'fraction_non_alphanumeric': 0.08331985115677075, 'fraction_numerical': 0.0022650056625141564, 'mean_word_length': 2.5305539691604797, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743528', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1610, 'n_words': 514}