//
//  AppDelegate.swift
//  writeas
//
//  Created by Matt Baer on 12/29/15.
//  Copyright © 2015 A Bunch Tell. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var vc: ViewController?

    @IBOutlet weak var nightModeItem: NSMenuItem!
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
        if nightModeItem.state == NSOnState {
            vc?.toggle(isNight: true)
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        vc?.saveDocument()
    }
    
    func applicationDidBecomeActive(_ notification: Notification) {
        if vc == nil {
            vc = NSApplication.shared().mainWindow?.contentViewController as! ViewController
        }
    }

    @IBAction func toggleNightMode(_ sender: NSMenuItem) {
        let isOff = sender.state == NSOffState
        vc?.toggle(isNight: isOff)
        sender.state = isOff ? NSOnState : NSOffState
        UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
    }

    @IBAction func saveDocument(_ sender: AnyObject) {
        vc?.saveDocument()
    }

    @IBAction func publishDoc(_ sender: Any) {
        vc?.publish()
    }
}

