import * as path from "path";

import { AuthType } from "../enums/AuthType";
import { ConnectionType } from "../enums/ConnectionType";
import { NodeTrustedMailcore2NativeBinding } from "../types/NodeTrustedMailcore2NativeBinding";
import { IMAPAsyncSessionNativeBinding } from '../types';

// tslint:disable-next-line: no-var-requires
let bindings: NodeTrustedMailcore2NativeBinding;
try {
    bindings = require(path.join(__dirname, "../../lib/binding/trusted_mailcore2.node"));
} catch(e) {
    bindings = require(path.join(__dirname, "../../lib/binding/trusted_mailcore2_no_crypto.node"));
}

// tslint:disable-next-line
const { IMAPAsyncSession: _IMAPAsyncSession } = bindings;

/**
 * IMAPAsyncSession Class
 *
 * @public
 */
class IMAPAsyncSession extends bindings.IMAPAsyncSession {
    protected handle: IMAPAsyncSessionNativeBinding;

    constructor() {
        super()

        const handle = new _IMAPAsyncSession();
        this.handle = handle;
    }

    /**
     * Set hostname
     *
     * @memberof IMAPAsyncSession
     */
    set hostname(hostname: string) {
        this.handle.hostname = hostname;
    }

    /**
     * Get hostname
     *
     * @type {string}
     * @memberof IMAPAsyncSession
     */
    get hostname(): string {
        return this.handle.hostname;
    }

    /**
     * Set port
     *
     * @memberof IMAPAsyncSession
     */
    set port(port: number) {
        this.handle.port = port;
    }

    /**
     * Get port
     *
     * @type {number}
     * @memberof IMAPAsyncSession
     */
    get port(): number {
        return this.handle.port;
    }

    /**
     * Set string
     *
     * @memberof IMAPAsyncSession
     */
    set username(username: string) {
        this.handle.username = username;
    }

    /**
     * Get string
     *
     * @type {string}
     * @memberof IMAPAsyncSession
     */
    get username(): string {
        return this.handle.username;
    }

    /**
     * Set password
     *
     * @memberof IMAPAsyncSession
     */
    set password(password: string) {
        this.handle.password = password;
    }

    /**
     * Get password
     *
     * @type {string}
     * @memberof IMAPAsyncSession
     */
    get password(): string {
        return this.handle.password;
    }

    /**
     * Set checkCertificateEnabled
     *
     * @memberof IMAPAsyncSession
     */
    set checkCertificateEnabled(isEnabled: boolean) {
        this.handle.checkCertificateEnabled = isEnabled;
    }

    /**
     * Get checkCertificateEnabled
     *
     * @type {boolean}
     * @memberof IMAPAsyncSession
     */
    get checkCertificateEnabled(): boolean {
        return this.handle.checkCertificateEnabled;
    }

    /**
     * Set OAuth2Token
     *
     * @memberof IMAPAsyncSession
     */
    set OAuth2Token(OAuth2Token: string) {
        this.handle.OAuth2Token = OAuth2Token;
    }

    /**
     * Get OAuth2Token
     *
     * @type {string}
     * @memberof IMAPAsyncSession
     */
    get OAuth2Token(): string {
        return this.handle.OAuth2Token;
    }

    /**
     * Set authType
     *
     * @memberof IMAPAsyncSession
     */
    set authType(authType: AuthType) {
        this.handle.authType = authType;
    }

    /**
     * Get authType
     *
     * @type {AuthType}
     * @memberof IMAPAsyncSession
     */
    get authType(): AuthType {
        return this.handle.authType;
    }

    /**
     * Set connectionType
     *
     * @memberof IMAPAsyncSession
     */
    set connectionType(connectionType: ConnectionType) {
        this.handle.connectionType = connectionType;
    }

    /**
     * Get connectionType
     *
     * @type {ConnectionType}
     * @memberof IMAPAsyncSession
     */
    get connectionType(): ConnectionType {
        return this.handle.connectionType;
    }

    /**
     * Set strictGost flag (to force using GOST for TLS)
     *
     * @memberof IMAPSession
     */
    set strictGost(value: Boolean) {
        this.handle.strictGost = value;
    }

    /**
     * Get strictGost flag (to force using GOST for TLS)
     *
     * @type {Boolean}
     * @memberof IMAPSession
     */
    get strictGost(): Boolean {
        return this.handle.strictGost;
    }

    /**
     * Validate exist account
     *
     * @param {string} from address
     * @returns {number} 0 - success, other - failure
     *
     * @memberof IMAPAsyncSession
     */
    public async checkAccount(from: string): Promise<number> {
        return this.handle.checkAccount(from);
    }

    /**
     * Disconnect connection
     *
     * @memberof IMAPAsyncSession
     */
    public disconnect(): void {
        this.handle.disconnect();
    }

    /**
     * Validate connection status
     *
     * @returns {boolean}
     *
     * @memberof IMAPAsyncSession
     */
    public isDisconnected(): boolean {
        return this.handle.isDisconnected();
    }

    public fetchAllFolders(): any {
        return this.handle.fetchAllFolders();
    }
}

export { IMAPAsyncSession }
