import { Meteor } from 'meteor/meteor';
import { Accounts } from "meteor/accounts-base";

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';

var log = new Logger();
(new LoggerFile(log)).enable();

Meteor.methods({
    'createUser1' (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`createUser() => start createUser `, item, Meteor.user().username) : '';
                let userExists = Meteor.users.findOne({ username: item.username });
                if (userExists == undefined || userExists == null) {
                    Accounts.createUser({
                        ...item
                    });
                    Meteor.settings.enableDebug ? log.debug(`createUser() => username created `, item, Meteor.user().username) : '';
                } else {
                    Meteor.settings.enableDebug ? log.debug(`createUser() => username exists `, item, Meteor.user().username) : '';
                }

                return true
            } catch (e) {
                log.error(`createUser() => error creating user at meteor`, e, Meteor.user().username);
                return false;
            }
        }
    },
    "editUser1" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`editUser() => start`, item, Meteor.user().username) : '';
                Meteor.users.update({ _id: item._id }, { $set: { "profile.fullName": item.fullName } }, { upsert: true });
                Meteor.settings.enableDebug ? log.debug(`editUser() => user edited `, item, Meteor.user().username) : '';
                return true
            } catch (e) {

                log.error(`editUser() => error editing user`, e, Meteor.user().username);
                return false;
            }
        }
    },
    "setPassword" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`setPassword() => start setPassword`, item, Meteor.user().username) : '';
                Accounts.setPassword(item._id, item.password, { logout: true })
                Meteor.settings.enableDebug ? log.debug(`setPassword() => user password have been changed `, item, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`setPassword() => error setPassword`, e, Meteor.user().username);
                return false;
            }
        }
    },
    "deleteUser1" (item) {

        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`deleteUser() => start delete user`, item, Meteor.user().username) : '';
                Meteor.users.remove({ _id: item._id });
                Meteor.settings.enableDebug ? log.debug(`deleteUser() => user have been removed `, item, Meteor.user().username) : '';
                return true
            } catch (e) {

                log.error(`deleteUser() => error user removing`, e, Meteor.user().username);
                return false;
            }
        }
    },
    "editUserProfile" (item) {

        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`editUserProfile() => start`, item, Meteor.user().username) : '';
                Meteor.users.update({ _id: this.userId }, { $set: {...item } }, { upsert: true });
                Meteor.settings.enableDebug ? log.debug(`editUserProfile() => user profile have been edited`, item, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`editUserProfile() => error user profile editing`, e, Meteor.user().username);
                return false;
            }
        }
    }
});
Meteor.settings.enableDebug ? log.debug(`endof usersjs`) : '';