import Avatars from '@dicebear/avatars';
import sprites from '@dicebear/avatars-bottts-sprites';
import fs from 'fs';
import path from 'path';
var app = require('electron').remote.app
let options = {};
function createAvatarFile(uid, avatar){
return new Promise((resolve, reject) => {
let userCode = uid.split("").splice(0, 9).join("")
const userPath = path.join(`${app.getPath('userData')}`, `/${userCode}`)
let avatarLoc = path.join(userPath, '/', 'avatar.svg')
if (!fs.existsSync(userPath)) {
console.log('creating Folder')
fs.mkdir(userPath, {recursive:true}, (err)=>{
if (err) {
console.log(err)
reject(err)
} else {
resolve(fs.writeFileSync(avatarLoc, avatar))
}
})
}
});
}
function MakeAvatar (key) {
return new Promise((resolve, reject)=>{
let avatars = new Avatars(sprites(options));
let avatar = avatars.create(key);
if (avatar){
resolve(avatar)
} else {
reject()
}
})
}
export default function avatarMaker(key, uid) {
return new Promise((resolve, reject)=>{
MakeAvatar(key)
.then(avatar =>{
createAvatarFile(uid, avatar)
resolve(avatar)
}).catch(err=>{
if (err) reject(err)
})
})
}
// TODO: retrieve avatar from storage for display in UI