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



Meteor.methods({
    'createUser1' (item) {
        this.unblock();
        if (this.userId) {
            try {

                let userExists = Meteor.users.findOne({ username: item.username });
                if (userExists == undefined || userExists == null)
                    Accounts.createUser({
                        ...item
                        // username: item.username,
                        // password: item.password,
                        // profile:{

                        // }
                    });

                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "editUser1" (item) {
        this.unblock();
        if (this.userId) {
            try {

                Meteor.users.update({ _id: item._id }, { $set: { "profile.fullName": item.fullName } }, { upsert: true })
                    // Accounts.setPassword(item._id, item.password, { logout: true })
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "setPassword" (item) {
        this.unblock();
        if (this.userId) {
            try {

                Accounts.setPassword(item._id, item.password, { logout: true })
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "deleteUser1" (item) {

        this.unblock();
        if (this.userId) {
            try {

                Meteor.users.remove({ _id: item._id });
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "editUserProfile" (item) {

        this.unblock();
        if (this.userId) {
            try {

                // db.collection.update(  { _id:...} , { $set: { some_key.param2 : new_info  } }
                Meteor.users.update({ _id: this.userId }, { $set: {...item } }, { upsert: true })
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    }
});