<commit_before>//
//  VideoDetailsViewController.swift
//  WWDC
//
//  Created by <NAME> on 18/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Cocoa
import KVOController
import WWDCPlayer
import WWDCAppKit
import EventKit

class VideoDetailsViewController: NSViewController {
    
    var videoControllers: [NSWindowController] = []
    var slideControllers: [PDFWindowController] = []
    
    @IBOutlet weak var topBarHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var titleTopConstraint: NSLayoutConstraint!
    
    var searchTerm: String? {
        didSet {
            updateTranscriptsViewController()
        }
    }
    
    private var transcriptSearchResultsVC: TranscriptSearchResultsController?
    
    @IBOutlet weak var transcriptControllerContainerView: NSView!
    
    
    var selectedCount = 1
    var multipleSelection: Bool {
        get {
            return selectedCount > 1
        }
    }
    var session: Session? {
        didSet {
            updateUI()
        }
    }
    
    @IBOutlet weak var liveIndicatorView: RoundRectLabel! {
        didSet {
            liveIndicatorView.tintColor = Theme.WWDCTheme.liveColor
            liveIndicatorView.title = "LIVE"
            
            if #available(OSX 10.11, *) {
                liveIndicatorView.font = NSFont.systemFontOfSize(11.0, weight: NSFontWeightMedium)
            } else {
                liveIndicatorView.font = NSFont.systemFontOfSize(11.0)
            }
        }
    }
    @IBOutlet weak var titleLabel: NSTextField!
    @IBOutlet weak var subtitleLabel: NSTextField!
    @IBOutlet weak var descriptionLabel: NSTextField!
    @IBOutlet weak var downloadController: DownloadProgressViewController!
    @IBOutlet weak var actionButtonsController: ActionButtonsViewController!
    @IBOutlet weak var topBarBackgroundView: GRWindowMovingView!
    @IBOutlet weak var topBarSeparatorView: SeparatorView!
    
    @IBOutlet weak var watchLiveButton: NSButton! {
        didSet {
            if #available(OSX 10.11, *) {
                let pStyle = NSMutableParagraphStyle()
                pStyle.alignment = NSCenterTextAlignment
                
                let attrs = [
                    NSFontAttributeName: NSFont.controlContentFontOfSize(13.0),
                    NSForegroundColorAttributeName: NSColor.whiteColor(),
                    NSParagraphStyleAttributeName: pStyle
                ]
                watchLiveButton.attributedTitle = NSAttributedString(string: "Watch Live", attributes: attrs)
                
                watchLiveButton.appearance = NSAppearance(named: "LiveButton")
                watchLiveButton.sizeToFit()
            }
        }
    }
    @IBOutlet weak var reminderButton: NSButton!
    
    private func updateUI() {
        if let session = session {
            guard !session.invalidated else { return }
        }
        
        if multipleSelection {
            handleMultipleSelection()
            return
        }
        
        watchLiveButton.hidden = true
        reminderButton.hidden = true
        downloadController.view.hidden = false
        
        actionButtonsController.session = session
        setupActionCallbacks()
        
        titleLabel.textColor = NSColor.labelColor()
        
        if let session = self.session {
            actionButtonsController.view.hidden = false
            titleLabel.stringValue = session.title
            subtitleLabel.stringValue = "\(session.track) | Session \(session.id)"
            descriptionLabel.stringValue = session.summary
            descriptionLabel.hidden = false
            
            restoreColors()
            
            downloadController.session = session
            downloadController.downloadFinishedCallback = { [unowned self] in
                self.updateUI()
            }
            
            if let schedule = session.schedule where session.isScheduled {
                showScheduledState(schedule)
            }
        } else {
            actionButtonsController.view.hidden = true
            titleLabel.stringValue = "No session selected"
            subtitleLabel.stringValue = "Select a session to see It here"
            descriptionLabel.hidden = true
            downloadController.session = nil
            
            restoreColors()
        }
        
        setupTranscriptResultsViewIfNeeded()
        updateTranscriptsViewController()
    }
    
    private func showScheduledState(schedule: ScheduledSession) {
        guard let track = schedule.track else { return }
        
        watchLiveButton.hidden = false
        reminderButton.hidden = false
        actionButtonsController.view.hidden = true
        
        topBarBackgroundView.backgroundColor = NSColor(hexString: track.darkColor)
        topBarSeparatorView.backgroundColor = NSColor.blackColor()
        titleLabel.textColor = NSColor(hexString: track.titleColor)
        subtitleLabel.textColor = NSColor(hexString: track.color)
        
        updateLiveState()
        updateReminderButton()
    }
    
    @objc private func updateReminderButton() {
        guard let schedule = session?.schedule else { return }
        
        reminderButton.enabled = true
        
        calendarHelper.hasReminderForScheduledSession(schedule) { [weak self] hasReminder in
            if hasReminder {
                self?.reminderButton.title = "Remove Reminder"
                self?.reminderButton.tag = 1
            } else {
                self?.reminderButton.title = "Create Reminder"
                self?.reminderButton.tag = 0
            }
        }
    }
    
    private lazy var startDateFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        
        formatter.locale = NSLocale(localeIdentifier: "en")
        formatter.dateFormat = "EEEE 'at' "
        
        return formatter
    }()
    
    private lazy var startTimeFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        
        formatter.timeStyle = .ShortStyle
        
        return formatter
    }()
    
    @objc private func updateLiveState(note: NSNotification? = nil) {
        guard let schedule = session?.schedule else {
            watchLiveButton.enabled = false
            liveIndicatorView.hidden = true
            return
        }
        
        let tooltip: String
        if schedule.isLive {<commit_msg>Fixed issue with threading when getting authorization to use reminders<commit_after>//
//  VideoDetailsViewController.swift
//  WWDC
//
//  Created by <NAME> on 18/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Cocoa
import KVOController
import WWDCPlayer
import WWDCAppKit
import EventKit

class VideoDetailsViewController: NSViewController {
    
    var videoControllers: [NSWindowController] = []
    var slideControllers: [PDFWindowController] = []
    
    @IBOutlet weak var topBarHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var titleTopConstraint: NSLayoutConstraint!
    
    var searchTerm: String? {
        didSet {
            updateTranscriptsViewController()
        }
    }
    
    private var transcriptSearchResultsVC: TranscriptSearchResultsController?
    
    @IBOutlet weak var transcriptControllerContainerView: NSView!
    
    
    var selectedCount = 1
    var multipleSelection: Bool {
        get {
            return selectedCount > 1
        }
    }
    var session: Session? {
        didSet {
            updateUI()
        }
    }
    
    @IBOutlet weak var liveIndicatorView: RoundRectLabel! {
        didSet {
            liveIndicatorView.tintColor = Theme.WWDCTheme.liveColor
            liveIndicatorView.title = "LIVE"
            
            if #available(OSX 10.11, *) {
                liveIndicatorView.font = NSFont.systemFontOfSize(11.0, weight: NSFontWeightMedium)
            } else {
                liveIndicatorView.font = NSFont.systemFontOfSize(11.0)
            }
        }
    }
    @IBOutlet weak var titleLabel: NSTextField!
    @IBOutlet weak var subtitleLabel: NSTextField!
    @IBOutlet weak var descriptionLabel: NSTextField!
    @IBOutlet weak var downloadController: DownloadProgressViewController!
    @IBOutlet weak var actionButtonsController: ActionButtonsViewController!
    @IBOutlet weak var topBarBackgroundView: GRWindowMovingView!
    @IBOutlet weak var topBarSeparatorView: SeparatorView!
    
    @IBOutlet weak var watchLiveButton: NSButton! {
        didSet {
            if #available(OSX 10.11, *) {
                let pStyle = NSMutableParagraphStyle()
                pStyle.alignment = NSCenterTextAlignment
                
                let attrs = [
                    NSFontAttributeName: NSFont.controlContentFontOfSize(13.0),
                    NSForegroundColorAttributeName: NSColor.whiteColor(),
                    NSParagraphStyleAttributeName: pStyle
                ]
                watchLiveButton.attributedTitle = NSAttributedString(string: "Watch Live", attributes: attrs)
                
                watchLiveButton.appearance = NSAppearance(named: "LiveButton")
                watchLiveButton.sizeToFit()
            }
        }
    }
    @IBOutlet weak var reminderButton: NSButton!
    
    private func updateUI() {
        if let session = session {
            guard !session.invalidated else { return }
        }
        
        if multipleSelection {
            handleMultipleSelection()
            return
        }
        
        watchLiveButton.hidden = true
        reminderButton.hidden = true
        downloadController.view.hidden = false
        
        actionButtonsController.session = session
        setupActionCallbacks()
        
        titleLabel.textColor = NSColor.labelColor()
        
        if let session = self.session {
            actionButtonsController.view.hidden = false
            titleLabel.stringValue = session.title
            subtitleLabel.stringValue = "\(session.track) | Session \(session.id)"
            descriptionLabel.stringValue = session.summary
            descriptionLabel.hidden = false
            
            restoreColors()
            
            downloadController.session = session
            downloadController.downloadFinishedCallback = { [unowned self] in
                self.updateUI()
            }
            
            if let schedule = session.schedule where session.isScheduled {
                showScheduledState(schedule)
            }
        } else {
            actionButtonsController.view.hidden = true
            titleLabel.stringValue = "No session selected"
            subtitleLabel.stringValue = "Select a session to see It here"
            descriptionLabel.hidden = true
            downloadController.session = nil
            
            restoreColors()
        }
        
        setupTranscriptResultsViewIfNeeded()
        updateTranscriptsViewController()
    }
    
    private func showScheduledState(schedule: ScheduledSession) {
        guard let track = schedule.track else { return }
        
        watchLiveButton.hidden = false
        reminderButton.hidden = false
        actionButtonsController.view.hidden = true
        
        topBarBackgroundView.backgroundColor = NSColor(hexString: track.darkColor)
        topBarSeparatorView.backgroundColor = NSColor.blackColor()
        titleLabel.textColor = NSColor(hexString: track.titleColor)
        subtitleLabel.textColor = NSColor(hexString: track.color)
        
        updateLiveState()
        updateReminderButton()
    }
    
    @objc private func updateReminderButton() {
        dispatch_async(dispatch_get_main_queue()) {
            guard let schedule = self.session?.schedule else { return }
            
            self.reminderButton.enabled = true
            
            self.calendarHelper.hasReminderForScheduledSession(schedule) { [weak self] hasReminder in
                if hasReminder {
                    self?.reminderButton.title = "Remove Reminder"
                    self?.reminderButton.tag = 1
                } else {
                    self?.reminderButton.title = "Create Reminder"
                    self?.reminderButton.tag = 0
                }
            }
        }
    }
    
    private lazy var startDateFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        
        formatter.locale = NSLocale(localeIdentifier: "en")
        formatter.dateFormat = "EEEE 'at' "
        
        return formatter
    }()
    
    private lazy var startTimeFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        
        formatter.timeStyle = .ShortStyle
        
        return formatter
    }()
    
    @objc private func updateLiveState(note: NSNotification? = nil) {
        guard let schedule = session?.schedule else {
            watchLiveButton.enabled = false
            liveIndicatorView.hidden = true
            return
        }
        
        let tooltip: String
        if schedule.isLive {
{'fraction_non_alphanumeric': 0.06737697378506823, 'fraction_numerical': 0.00475241453319025, 'mean_word_length': 1.9214061800268696, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3295401', 'n_tokens_mistral': 3311, 'n_tokens_neox': 3243, 'n_words': 714}