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

import Cocoa

internal class GeneralPreferencesViewController: NSViewController {
    
    // MARK: IBOutlets
    
    @IBOutlet internal weak var artworkPathControl: NSPathControl!
    @IBOutlet internal weak var chooseArtworkButton: NSButton!
    
    override internal func viewDidLoad() {
        super.viewDidLoad()
        artworkPathControl.URL = Preferences.sharedPreferences.artworkTarget
    }
    
    @IBAction internal func saveArtworkStateChanged(sender: AnyObject) {
        if Preferences.sharedPreferences.saveArtwork && Preferences.sharedPreferences.artworkTarget == nil {
            chooseArtworkPath(sender)
        }
    }
    
    @IBAction internal func chooseArtworkPath(sender: AnyObject) {
        let openPanel = NSOpenPanel()
        openPanel.canChooseDirectories = true
        openPanel.canChooseFiles = false
        openPanel.canCreateDirectories = true
        openPanel.prompt = NSLocalizedString("Choose…", comment: "Button title in an open dialog prompting the user to choose a directory")
        openPanel.beginSheetModalForWindow(view.window!) {
            result in
            if result == NSModalResponseOK {
                Preferences.sharedPreferences.artworkTarget = openPanel.URL!.filePathURL!
            } else if Preferences.sharedPreferences.artworkTarget == nil {
                Preferences.sharedPreferences.saveArtwork = false
            }
        }
    }
    
}

internal class TagsPreferencesViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSMenuDelegate {
    
    // MARK: Types
    
    private struct TableViewConstants {
        
        static let textTableCellViewIdentifier = "textCell"
        
        static let popupTableCellViewIdentifier = "popupCell"
        
        static let tagTableColumnIdentifier = "tagColumn"
        
        static let savingBehaviorTableColumnIdentifier = "savingBehaviorColumn"
        
    }
    
    // MARK: Properties
    
    @IBOutlet weak var tableView: NSTableView!
    
    // MARK: Table View
    
    internal func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return Track.Tag.allTags.count
    }
    
    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let tag = Track.Tag.allTags[row]
        if tableColumn?.identifier == TableViewConstants.tagTableColumnIdentifier {
            let view = tableView.makeViewWithIdentifier(TableViewConstants.textTableCellViewIdentifier, owner: nil) as? NSTableCellView
            view?.textField?.stringValue = tag.localizedName
            return view
        } else if tableColumn?.identifier == TableViewConstants.savingBehaviorTableColumnIdentifier {
            let popupButton = tableView.makeViewWithIdentifier(TableViewConstants.popupTableCellViewIdentifier, owner: nil) as? NSPopUpButton
            popupButton?.removeAllItems()
            if tag.isReturnedBySearchAPI {
                popupButton?.addItemWithTitle(NSLocalizedString("Save", comment: "Menu item title for a tag that is going to be saved"))
            }
            if tag.clearable {
                popupButton?.addItemWithTitle(NSLocalizedString("Clear", comment: "Menu item title for a tag that is going to be cleared"))
            }
            popupButton?.addItemWithTitle(NSLocalizedString("Ignore", comment: "Menu item title for a tag that is not going to be saved"))
            
            var selectedIndex: Int
            switch Preferences.sharedPreferences.tagSavingBehaviors[tag]! {
            case .Save:
                selectedIndex = 0
            case .Clear:
                selectedIndex = 1
                if !tag.isReturnedBySearchAPI {
                    --selectedIndex
                }
            case .Ignore:
                selectedIndex = 2
                if !tag.isReturnedBySearchAPI {
                    --selectedIndex
                }
                if !tag.clearable {
                    --selectedIndex
                }
            }
            popupButton?.selectItemAtIndex(selectedIndex)
            return popupButton
        }
        return nil
    }
    
    @IBAction private func savingBehaviorChanged(sender: NSPopUpButton) {
        let tag = Track.Tag.allTags[tableView.rowForView(sender)]
        let selectedIndex = sender.indexOfItem(sender.selectedItem!)
        var savingBehavior = Preferences.sharedPreferences.tagSavingBehaviors[tag]!
        switch selectedIndex {
        case 0:
            if tag.isReturnedBySearchAPI {
                savingBehavior = .Save
            } else if tag.clearable {
                savingBehavior = .Clear
            } else {
                savingBehavior = .Ignore
            }
        case 1:
            if tag.isReturnedBySearchAPI {
                if tag.clearable {
                    savingBehavior = .Clear
                } else {
                    savingBehavior = .Ignore
                }
            }
            savingBehavior = .Ignore
        case 2:
            savingBehavior = .Ignore
        default:
            break
        }
        Preferences.sharedPreferences.tagSavingBehaviors[tag] = savingBehavior
    }
    
}