//
//  SettingViewController.swift
//  KdanAuto
//
//  Created by 朱东勇 on 2022/11/21.
//

import Foundation
import AppKit

enum PathType {
    case Origin;
    case Result;
    case Check;
}

public protocol SettingViewControllerDelegate : NSObjectProtocol {
    @MainActor func settingViewDidFinished()
}

class SettingViewController : NSViewController, NSTextFieldDelegate, NSWindowDelegate {
    static var vc : SettingViewController!
    @IBOutlet var window : NSWindow!
    
    public var delegate : SettingViewControllerDelegate!
    
    @IBOutlet var originPathTF : NSTextField!
    
    @IBOutlet var originPathBtn : NSButton!
    
    class func shared() -> SettingViewController {
        if vc == nil {
            vc = SettingViewController()
            
            Bundle.main.loadNibNamed("SettingViewController", owner: vc, topLevelObjects: nil)
        }
        
        return vc
    }
    
    override func viewDidLoad() {
        
    }
    
    override func viewWillAppear() {
        self.originPathTF.stringValue = DataModel.shared.directoryPath() ?? "";
    }
    
    // IBAction
    @IBAction func pathSelected(_ sender:NSButton) {
        var openPanel = NSOpenPanel()
        
        openPanel.canChooseFiles = false
        openPanel.canChooseDirectories = true
        
        var pt:PathType = .Origin
        
        
        if openPanel.runModal() == NSApplication.ModalResponse.OK {
            let path = openPanel.directoryURL?.relativePath
            
            DataModel.shared.setDirectoryPath(path!);
            self.originPathTF.stringValue = path!;
        }
        
    }
    
    // Show
    public func show() {
        window.setIsVisible(true);
    }
    
    //Window Delegate
    func windowShouldClose(_ sender: NSWindow) -> Bool {
        if delegate != nil {
            delegate.settingViewDidFinished()
        }
        
        sender.setIsVisible(false);
        
        return false;
    }
    
}
