//
//  TrackTableCellView.swift
//  Harmony
//
//  Created by Kim Wittenburg on 21.01.15.
//  Copyright (c) 2015 Das Code Kollektiv. All rights reserved.
//

import Cocoa
import AppKitPlus

/// A table cell view to display information for a `Track`. This class should
/// only be initialized from a nib or storyboard.
public class TrackTableCellView: AdvancedTableCellView {
    
    // MARK: Types
    
    private struct Images {
        
        /// Caches the tick image for track cells so that it does not need to be
        /// reloaded every time a cell is configured.
        static let tickImage = NSImage(named: "Tick")?.imageByMaskingWithColor(NSColor.clearColor())
        
        /// Caches the gray tick image for track cells so that it does not need to be
        /// reloaded every time a cell is configured.
        static let grayTickImage = NSImage(named: "Tick")?.imageByMaskingWithColor(NSColor.lightGrayColor())
        
    }
    
    // MARK: Properties
    
    /// An outlet do display the track number. This acts as a secondary label.
    /// The text color is automatically adjusted based on the `backgroundStyle`
    /// of the receiver.
    ///
    /// Intended to be used as accessory view
    @IBOutlet public lazy var trackNumberTextField: NSTextField? = {
        let trackNumberTextField = NSTextField()
        trackNumberTextField.bordered = false
        trackNumberTextField.drawsBackground = false
        trackNumberTextField.selectable = false
        trackNumberTextField.lineBreakMode = .ByTruncatingTail
        trackNumberTextField.font = NSFont.systemFontOfSize(0)
        trackNumberTextField.textColor = NSColor.secondaryLabelColor()
        trackNumberTextField.translatesAutoresizingMaskIntoConstraints = false
        return trackNumberTextField
    }()
    
    /// Intended to be used as accessory view.
    ///
    /// This property replaces `imageView`.
    @IBOutlet public lazy var savedImageView: NSImageView! = {
        let secondaryImageView = NSImageView()
        secondaryImageView.imageScaling = .ScaleProportionallyDown
        secondaryImageView.translatesAutoresizingMaskIntoConstraints = false
        return secondaryImageView
        }()
    
    // MARK: Intitializers
    
    override public func setupView() {
        leftAccessoryView = trackNumberTextField
        super.setupView()
    }
    
    // MARK: Overrides
    
    override public var backgroundStyle: NSBackgroundStyle {
        get {
            return super.backgroundStyle
        }
        set {
            super.backgroundStyle = newValue
            let trackNumberCell = self.trackNumberTextField?.cell as? NSTextFieldCell
            trackNumberCell?.backgroundStyle = newValue
            
            switch newValue {
            case .Light:
                trackNumberTextField?.textColor = NSColor.secondaryLabelColor()
            case .Dark:
                trackNumberTextField?.textColor = NSColor.secondarySelectedControlColor()
            default:
                break
            }
        }
    }
    
    override public var imageView: NSImageView? {
        set {
            savedImageView = newValue
        }
        get {
            return savedImageView
        }
    }
    
    // MARK: Methods
    
    /// Sets up the receiver to display `track`.
    public func setupForTrack(track: Track) {
        style = track.album.hasSameArtistNameAsTracks ? .Simple : .CompactSubtitle
        
        textField?.stringValue = track.name
        if track.associatedTracks.isEmpty {
            textField?.textColor = NSColor.disabledControlTextColor()
        } else if track.associatedTracks.count > 1 || track.associatedTracks[0].name != track.name {
            textField?.textColor = NSColor.redColor()
        } else {
            textField?.textColor = NSColor.controlTextColor()
        }
        secondaryTextField?.stringValue = track.artistName
        trackNumberTextField?.stringValue = "\(track.trackNumber)"
        if track.associatedTracks.isEmpty {
            imageView?.image = TrackTableCellView.Images.grayTickImage
        } else {
            imageView?.image = TrackTableCellView.Images.tickImage
        }
        if track.saved {
            let aspectRatioConstraint = NSLayoutConstraint(
                item: savedImageView,
                attribute: .Width,
                relatedBy: .Equal,
                toItem: savedImageView,
                attribute: .Height,
                multiplier: 1,
                constant: 0)
            let widthConstraint = NSLayoutConstraint(
                item: savedImageView,
                attribute: .Width,
                relatedBy: .Equal,
                toItem: nil,
                attribute: .Width,
                multiplier: 1,
                constant: 17)
            setRightAccessoryView(savedImageView, withConstraints: [aspectRatioConstraint, widthConstraint])
        } else {
            rightAccessoryView = nil
        }
    }

}
