'use strict';

import {getBrowserFingerprint} from "./utils";

let auth = function () {
    return {
        login: (username, password) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.signIn({},{requestBody: {username, password}}))
                .then(response => {
                    this.userId = response.data.userId;
                    this.username = response.data.username;
                    return this.__privates.auth.login(response.data.idToken, response.data.accessToken, response.data.refreshToken)
                })
        },

        providerLogin: (domain, token, displayname, deviceId) => {

            if(!token && typeof this.__privates.providerAuth === 'function') {
                ({token, displayname} = this.__privates.providerAuth());
            }

            return this.__privates.auth.__client
                .then(client => client.apis.auth.providerSignIn({},{requestBody:{domain, token, ...(displayname && {displayname}), ...(deviceId && {deviceId})}}))
                .then(response => {
                    this.userId = response.data.userId;
                    this.username = response.data.username;
                    return this.__privates.auth.login(response.data.idToken, response.data.accessToken, response.data.refreshToken)
                })
        },

        getProviderAuth: () => {
            return this.__privates.providerAuth;
        },

        deviceLogin: ( usePrecise = false, salt = 'ThisIsSaltyAF') => {
            return getBrowserFingerprint(this.__instanceType, usePrecise, salt)
                .then( deviceId => Promise.all([deviceId, this.__privates.auth.__client]))
                .then(([deviceId, client]) => client.apis.auth.deviceSignIn({},{requestBody:{deviceId, domain: location.hostname}}))
                .then(response => {
                    this.userId = response.data.userId;
                    this.username = response.data.username;
                    return this.__privates.auth.login(response.data.idToken, response.data.accessToken, response.data.refreshToken)
                })
        },

        logout: () => {
            return this.__privates.auth.logout();
        },

        isLoggedIn: () => {
            return this.__privates.auth.isLoggedIn();
        },

        setLanguage: (language) => {
            return this.__privates.auth.setLanguage(language);
        },

        getLanguage: () => {
            return this.__privates.auth.__language;
        },

        signUp: (username, password, email, phone) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.signUp({},{requestBody:{username, password, email, phone}}))
        },

        confirmSignUp: (confirmationCode, username, displayname, isPublic = true ) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.confirmSignUp({},{requestBody:{confirmationCode, username, displayname, isPublic}}))
                .then(response => {
                    this.userId = response.data.userId;
                    this.username = response.data.username;
                    return this.__privates.auth.login(response.data.idToken, response.data.accessToken, response.data.refreshToken)
                })
        },

        resendConfirmationCode: (username) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.resendConfirmationCode({},{requestBody:{username}}))
        },

        forgotPassword: (username) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.forgotPassword({},{requestBody:{username}}))
        },

        confirmForgotPassword: (username, confirmationCode, password) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.confirmForgotPassword({},{requestBody:{password, confirmationCode, username}}))
        },

        changePassword: (oldPassword, password) => {
            const accessToken = localStorage.getItem(this.__privates.auth.ACCESS_TOKEN);

            return this.__privates.auth.__client
                .then(client => client.apis.auth.changePassword({},{requestBody:{oldPassword, password, accessToken}}))
        },

        socialAuth: (data = {}) => {
            return this.__privates.auth.__client
                .then(client => client.apis.auth.socialAuth(data))
        },

        $on: (key, callback, that) => {
            return this.__privates.auth.on(key, callback, that || this);
        },
        $off: (key, callback, that) => {
            return this.__privates.auth.off(key, callback, that || this);
        },
        $clear: () => {
            return this.__privates.auth.clear();
        }
    }
};

export default auth;

