import Promise from 'bluebird';
import elasticsearch from 'elasticsearch';
import {
    v1 as uuid
}
from 'node-uuid';
import config from './config';

const dbClient = new elasticsearch.Client({
    host: config.elasticSearch,
    log: 'info'
});


class Database {

    constructor() {

        this.user = {
            create: this.createUser,
            validate: this.validateUser
        };

        dbClient.ping(err => {
            if (err)
                return console.error(err)
            this.initIndices()
        });
    }

    initIndices() {
        return new Promise((resolve, reject) => {
            dbClient.indices.create('users', {
                "mappings": {
                    "profile": {
                        "properties": {
                            "email": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "first": {
                                "type": "string"
                            },
                            "last": {
                                "type": "string"
                            },
                            "middle": {
                                "type": "string"
                            },
                            "full": {
                                "type": "string"
                            },
                            "password": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "phone": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "photo": {
                                "properties": {
                                    "thumb": {
                                        "type": "string"
                                    },
                                    "time": {
                                        "type": "double"
                                    }
                                }
                            },
                            "time": {
                                "type": "double"
                            }
                        }
                    }
                }
            }, err => {
                if (err) reject(err)
                else resolve();
            })
        });
    }


    validateUser() {


    }

    createUser(params) {
        return this.create('users', 'profile', uuid(), params);
    }

    create(index, type, id, body) {
        return new Promise((resolve, reject) => {
            dbClient.create({
                index: index,
                type: type,
                id: id,
                body: body
            }, (error, response) => {
                if (error)
                    return reject(error);
                resolve(response);
            });
        });
    }


}

const database = new Database();


export
default database;
@@ -379,16 +379,53 @@
 dateUser
+,%0A            update: this.updateUser
 %0A       
@@ -553,16 +553,17 @@
 ndices()
+;
 %0A       
@@ -2550,125 +2550,733 @@
  %7D%0A%0A
-%0A    validateUser() %7B%0A%0A%0A    %7D%0A%0A    createUser(params) %7B%0A        return this.create('users', 'profile', uuid(), params
+    updateUser(params) %7B%0A%0A%0A    %7D%0A%0A    validateUser(params) %7B%0A        return this.search('users', 'profile', %7B%0A            query: %7B%0A                match: %7B%0A                    email: params.email,%0A                    password: params.password%0A                %7D%0A            %7D,%0A        %7D);%0A    %7D%0A%0A    createUser(params) %7B%0A        return this.create('users', 'profile', uuid(), params);%0A    %7D%0A%0A%0A%0A%0A%0A    search(index, body) %7B%0A        return new Promise((resolve, reject) =%3E %7B%0A            dbClient.search(%7B%0A                index: index,%0A                body: body%0A            %7D, (error, response) =%3E %7B%0A                if (error)%0A                    return reject(error);%0A                resolve(response);%0A            %7D);%0A        %7D
 );%0A 

change up database a little