<commit_before>//
//  IdentityWindowController.swift
//  BartlebysUI
//
//  Created by <NAME> on 29/12/2016.
//  Copyright © 2016 Chaosmos SAS. All rights reserved.
//

import Cocoa
import BartlebyKit

// MARK: - IdentifactionDelegate

public protocol IdentifactionDelegate{
    func userWantsToCloseIndentityController()
}

// MARK: - IdentityStepNavigation

public protocol IdentityStepNavigation{
    func didValidateStep(_ step:Int)
    func disableActions()
    func enableActions()

    // The progress indicator located between the buttons
    func enableProgressIndicator()
    func disableProgressIndicator()
}

// MARK: - IdentityStep

public protocol IdentityStep{
    var stepIndex:Int { get set }
    func proceedToValidation()
}



// MARK: - IdentityWindowController


/*
 To use the identity Controller

 1# Instantiate the IdentityWindowController
 2# Pass the document instance
 3# Register as IdentificationDelegate

 */
open class IdentityWindowController: NSWindowController,DocumentProvider,IdentityStepNavigation {


    override open var windowNibName: NSNib.Name? { return NSNib.Name("IdentityWindowController") }

    // MARK: DocumentDependent

    /// Returns a BartlebyDocument
    /// Generally used in  with `DocumentDependent` protocol
    ///
    /// - Returns: the document
    public func getDocument() -> BartlebyDocument?{
        return self.document as? BartlebyDocument
    }

    // You can set up a void IdentityWindowController by setting `
    // `IdentityWindowController.usesDefaultComponents = false`
    // It allows to reuse creational identified components
    // You should always roll back to `IdentityWindowController.usesDefaultComponents = true` afer usage
    public static var usesDefaultComponents:Bool = true

    // Document creation
    public var creationMode=false

    // Set to true when the key is not available in the local bowl
    public var activationMode=false{
        didSet{
            if activationMode==true{
                self.recoverTheKey()
            }
        }
    }


    public var identification:Identification?

    public var identificationIsValid=false

    public var identificationDelegate:IdentifactionDelegate?


    // MARK: - Update Password

    public var passwordCandidate:String=""


    // MARK: - Components

    // You can replace the components
    // By overriding the IdentityWindowController.

    @IBOutlet open var prepareUserCreation: PrepareUserCreationViewController!

    @IBOutlet open var createAnIsolatedUser: CreateAnIsolatedUser!

    @IBOutlet open var byPassActivation: ByPassActivationViewController!

    @IBOutlet open var confirmActivation: ConfirmActivationViewController!

    @IBOutlet open var setUpCollaborativeServer: SetupCollaborativeServerViewController!

    @IBOutlet open var revealPassword: RevealPasswordViewController!

    @IBOutlet open var validatePassword: ValidatePasswordViewController!

    @IBOutlet open var updatePassword: UpdatePasswordViewController!

    @IBOutlet open var updatePasswordConfirmation: ConfirmUpdatePasswordActivationCode!

    @IBOutlet open var recoverSugar: RecoverSugarViewController!

    @IBOutlet open var importBkey: ImportBKeyViewController!

    // MARK: - Outlets

    @IBOutlet weak var tabView: NSTabView!

    @IBOutlet weak var leftButton: NSButton!

    @IBOutlet weak var rightButton: NSButton!

    @IBOutlet weak var progressIndicator: NSProgressIndicator!


    // MARK: - Life cycle

    override open func windowDidLoad() {
        super.windowDidLoad()
        self.configureControllers()
        self.progressIndicator.isHidden=true
        if Bartleby.configuration.DEVELOPER_MODE{
            if let document=self.getDocument(){
                IdentitiesManager.dumpKeyChainedProfiles(document)
            }
        }
    }

    open func configureControllers() -> () {
        if IdentityWindowController.usesDefaultComponents{
            if let document=self.getDocument(){
                if document.metadata.currentUserUID == Default.NO_UID {
                    self.creationMode=true
                    // It is a new document
                    self.append(viewController: self.prepareUserCreation, selectImmediately: true)
                    self.append(viewController: self.setUpCollaborativeServer, selectImmediately: false)

                    // Secondary Authentication factor management
                    if document.metadata.secondaryAuthFactorRequired{
                        self.append(viewController: self.confirmActivation, selectImmediately: false)
                    }else{
                        self.append(viewController: self.byPassActivation, selectImmediately: false)
                    }
                    // Revelation of the password
                    self.append(viewController: self.revealPassword, selectImmediately: false)
                }else{
                    self.creationMode=false
                    if document.metadata.sugar == Default.NO_SUGAR{
                        // we need to import a bkey
                        self.append(viewController: self.importBkey, selectImmediately: true)
                        self.append(viewController: self.validatePassword, selectImmediately: false)
                    }else{
                        self.append(viewController: self.validatePassword, selectImmediately: true)
                    }
                }
            }
        }else{
             //IdentityWindowController.usesDefaultComponents == false
        }
    }


    /// Appends a view Controller to the stack
    ///
    /// - Parameters:
    ///   - viewController: an IdentityStepViewController children
    ///   - selectImmediately: display immediately the added view Controller
    public func append(viewController:IdentityStepViewController,selectImmediately:Bool){
        let viewControllerItem=NSTabViewItem(viewController:viewController)
        viewController.documentProvider=self
        viewController.stepDelegate=self
        viewController.stepIndex=self.tabView.tabViewItems.count
        self.tabView.addTabViewItem(viewControllerItem)
        if selectImmediately{
            self.currentStep=viewController.stepIndex
        }
    }<commit_msg>Distinguish Isolated Mode when the Bowl is void<commit_after>//
//  IdentityWindowController.swift
//  BartlebysUI
//
//  Created by <NAME> on 29/12/2016.
//  Copyright © 2016 Chaosmos SAS. All rights reserved.
//

import Cocoa
import BartlebyKit

// MARK: - IdentifactionDelegate

public protocol IdentifactionDelegate{
    func userWantsToCloseIndentityController()
}

// MARK: - IdentityStepNavigation

public protocol IdentityStepNavigation{
    func didValidateStep(_ step:Int)
    func disableActions()
    func enableActions()

    // The progress indicator located between the buttons
    func enableProgressIndicator()
    func disableProgressIndicator()
}

// MARK: - IdentityStep

public protocol IdentityStep{
    var stepIndex:Int { get set }
    func proceedToValidation()
}



// MARK: - IdentityWindowController


/*
 To use the identity Controller

 1# Instantiate the IdentityWindowController
 2# Pass the document instance
 3# Register as IdentificationDelegate

 */
open class IdentityWindowController: NSWindowController,DocumentProvider,IdentityStepNavigation {


    override open var windowNibName: NSNib.Name? { return NSNib.Name("IdentityWindowController") }

    // MARK: DocumentDependent

    /// Returns a BartlebyDocument
    /// Generally used in  with `DocumentDependent` protocol
    ///
    /// - Returns: the document
    public func getDocument() -> BartlebyDocument?{
        return self.document as? BartlebyDocument
    }

    // You can set up a void IdentityWindowController by setting `
    // `IdentityWindowController.usesDefaultComponents = false`
    // It allows to reuse creational identified components
    // You should always roll back to `IdentityWindowController.usesDefaultComponents = true` afer usage
    public static var usesDefaultComponents:Bool = true

    // Document creation
    public var creationMode=false

    // Set to true when the key is not available in the local bowl
    public var activationMode=false{
        didSet{
            if activationMode==true{
                self.recoverTheKey()
            }
        }
    }


    public var identification:Identification?

    public var identificationIsValid=false

    public var identificationDelegate:IdentifactionDelegate?


    // MARK: - Update Password

    public var passwordCandidate:String=""


    // MARK: - Components

    // You can replace the components
    // By overriding the IdentityWindowController.

    @IBOutlet open var prepareUserCreation: PrepareUserCreationViewController!

    @IBOutlet open var createAnIsolatedUser: CreateAnIsolatedUser!

    @IBOutlet open var byPassActivation: ByPassActivationViewController!

    @IBOutlet open var confirmActivation: ConfirmActivationViewController!

    @IBOutlet open var setUpCollaborativeServer: SetupCollaborativeServerViewController!

    @IBOutlet open var revealPassword: RevealPasswordViewController!

    @IBOutlet open var validatePassword: ValidatePasswordViewController!

    @IBOutlet open var updatePassword: UpdatePasswordViewController!

    @IBOutlet open var updatePasswordConfirmation: ConfirmUpdatePasswordActivationCode!

    @IBOutlet open var recoverSugar: RecoverSugarViewController!

    @IBOutlet open var importBkey: ImportBKeyViewController!

    // MARK: - Outlets

    @IBOutlet weak var tabView: NSTabView!

    @IBOutlet weak var leftButton: NSButton!

    @IBOutlet weak var rightButton: NSButton!

    @IBOutlet weak var progressIndicator: NSProgressIndicator!


    // MARK: - Life cycle

    override open func windowDidLoad() {
        super.windowDidLoad()
        self.configureControllers()
        self.progressIndicator.isHidden=true
        if Bartleby.configuration.DEVELOPER_MODE{
            if let document=self.getDocument(){
                IdentitiesManager.dumpKeyChainedProfiles(document)
            }
        }
    }

    open func configureControllers() -> () {
        if IdentityWindowController.usesDefaultComponents{
            if let document=self.getDocument(){
                if document.metadata.currentUserUID == Default.NO_UID {
                    self.creationMode=true
                    // It is a new document
                    self.append(viewController: self.prepareUserCreation, selectImmediately: true)
                    self.append(viewController: self.setUpCollaborativeServer, selectImmediately: false)

                    // Secondary Authentication factor management
                    if document.metadata.secondaryAuthFactorRequired{
                        self.append(viewController: self.confirmActivation, selectImmediately: false)
                    }else{
                        self.append(viewController: self.byPassActivation, selectImmediately: false)
                    }
                    // Revelation of the password
                    self.append(viewController: self.revealPassword, selectImmediately: false)
                }else{
                    self.creationMode=false
                    let isolatedMode = document.metadata.collaborationServerURL == nil
                    if document.metadata.sugar == Default.NO_SUGAR && isolatedMode{
                        // we need to import a bkey
                        self.append(viewController: self.importBkey, selectImmediately: true)
                        self.append(viewController: self.validatePassword, selectImmediately: false)
                    }else{
                        self.append(viewController: self.validatePassword, selectImmediately: true)
                    }
                }
            }
        }else{
             //IdentityWindowController.usesDefaultComponents == false
        }
    }


    /// Appends a view Controller to the stack
    ///
    /// - Parameters:
    ///   - viewController: an IdentityStepViewController children
    ///   - selectImmediately: display immediately the added view Controller
    public func append(viewController:IdentityStepViewController,selectImmediately:Bool){
        let viewControllerItem=NSTabViewItem(viewController:viewController)
        viewController.documentProvider=self
        viewController.stepDelegate=self
        viewController.stepIndex=self.tabView.tabViewItems.count
        self.tabView.addTabViewItem(viewControllerItem)
        if selectImmediately{
            self.currentStep=viewController.stepIndex
        }
    }
{'fraction_non_alphanumeric': 0.0670891089108911, 'fraction_numerical': 0.002376237623762376, 'mean_word_length': 2.6470248411322936, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '132121', 'n_tokens_mistral': 3215, 'n_tokens_neox': 2958, 'n_words': 806}