{"size": 4229, "ext": "js", "max_stars_repo_path": "web-server/services/dataService.js", "max_stars_repo_name": "yangyaw1/online_maze", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "web-server/services/dataService.js", "max_issues_repo_name": "yangyaw1/online_maze", "max_issues_repo_issues_event_min_datetime": "2020-07-17T16:10:45.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-18T04:47:53.000Z", "max_forks_repo_path": "web-server/services/dataService.js", "max_forks_repo_name": "yangyaw1/online_maze", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 26.9363057325, "max_line_length": 80, "alphanum_fraction": 0.4225585245}
// const mazes = [{
//     id: 1,
//     graph: '0000001100010000111101101',
//     width: 5,
//     height: 5,
//     start: 7,
//     end: 24,
//     size: 'small'
// }];

// const users = [
//   {
//       uname: 'yyw',
//       uid: 1,
//       password: 'yyw'
//   },
//   {
//       uname: 'kkr',
//       uid: 2,
//       password: 'kkr'
//   }];
var redisClient = require('../modules/redisClient');
const TIMENOUT_IN_SECONDS = 3600;

const {mazemodel, usermodel} = require("../models/dataModel");
var crypto = require("crypto");

   
const getusers = function() {
    return new Promise((resolve, reject) => {
        usermodel.find({}, (err, users) => {
            if (err) {
                reject(err);
            } else {
                resolve(users);
            }
        });
    });
};

const getuser = function(uname) {
    return new Promise((resolve, reject) => {
        usermodel.findOne({uname: uname}, (err, user) => {
            if (err) {
                reject(err);
            } else {
                resolve(user);
            }
        });
    });
};

const adduser = function(newuser) {
    return new Promise((resolve, reject) => {
                usermodel.count({}, (err, count) => {
                    if(err){
                        reject('unexpected error!');
                    } else {
                       newuser.uid = count + 1;
                       newuser.salt = crypto.randomBytes(16).toString('base64');
                       newuser.hashedpw = crypto.createHash('sha1')
                               .update(newuser.password + newuser.salt)
                               .digest('base64');
                       delete newuser.password;
                       const mongouser = new usermodel(newuser);
                       mongouser.save();
                       resolve(mongouser);
                    }
                });
            });
};

const saveProcess = function(mazeid, uname, savedata){
    const key = mazeid + '#' + uname;
    const value = JSON.stringify(savedata);
    redisClient.set(key, value, redisClient.redisPrint);
};

const restoreProcess = function(mazeid, uname){
    const key = mazeid + '#' + uname;
    
    return new Promise((resolve, reject) => {
      redisClient.get(key, function(data){
           if(data){
               redisClient.del(key, function(res){
                   if(res){
                      resolve(JSON.parse(data)); 
                   } else {
                       reject('cannot delete key');
                   }
               });
           } else {
               reject('No restroed data');
           }
        }); 
    });
};

const getmazes = function() {
    return new Promise((resolve, reject) => {
        mazemodel.find({}, (err, mazes) => {
            if (err) {
                reject(err);
            } else {
                resolve(mazes);
            }
        });
    });
};

const getmaze = function(id) {
    return new Promise((resolve, reject) => {
        mazemodel.findOne({id: id}, (err, maze) => {
            if (err) {
                reject(err);
            } else {
                resolve(maze);
            }
        });
    });
};

const addmaze = function(maze) {
    return new Promise((resolve, reject) => {
        mazemodel.findOne({graph: maze.graph}, (err, data) => {
            if(err){
                reject(err);
            } else{   
                if(data) {
                    reject('Maze already exists!');
                } else {
                    mazemodel.count({}, (error, count) => {
                        if (error) {
                            reject(error);
                        } else {
                            maze.id = count + 1;
                            const mongomaze = new mazemodel(maze);
                            mongomaze.save();
                            resolve('Created!');
                        }
                    });
                }
            }
        });
    });
};

module.exports = {
    getusers: getusers,
    getuser: getuser,
    adduser: adduser,
    getmazes: getmazes,
    getmaze: getmaze,
    addmaze: addmaze,
    saveProcess: saveProcess,
    restoreProcess: restoreProcess
};