import React from 'react';
import assign from 'object-assign';
import request from 'superagent';

var EventEmitter = require('events').EventEmitter;

// notee
import NoteeDispatcher from '../dispatcher/NoteeDispatcher';
import Constants from '../constants/NoteeConstants';

function user_create(content) {
    request
        .post("/notee/api/users")
        .field('user[name]', content.user.name)
        .field('user[email]', content.user.email)
        .field('user[password]', content.user.password)
        .field('user[password_confirm]', content.user.password_confirm)
        .field('user[profile]', content.user.profile)
        .field('user[role]', content.user.role)
        .attach("user[profile_img]", content.user.profile_img)
        .end(function(err, res){
            if (err || !res.body) {
                UserStore.emitChange(Constants.USER_CREATE_FAILED);
                return false;
            }
            UserStore.emitChange(Constants.USER_CREATE);
        })
}

function user_update(content) {
    request
        .put("/notee/api/users/" + content.params_id)
        .field('user[name]', content.user.name)
        .field('user[email]', content.user.email)
        .field('user[profile]', content.user.profile)
        .field('user[role]', content.user.role)
        .attach("user[profile_img]", content.user.profile_img)
        .end(function(err, res){
            if(err || !res.body){
                UserStore.emitChange(Constants.USER_UPDATE_FAILED);
                return false;
            }
            UserStore.emitChange(Constants.USER_UPDATE);
        })
}

function user_delete(notee_src){
    request
        .del("/notee/api/users/" + notee_src)
        .end(function(err, res){
            if(err || !res.body){
                UserStore.emitChange(Constants.USER_DELETE_FAILED);
                return false;
            }
            UserStore.emitChange(Constants.USER_DELETE);
        })
}

var UserStore = assign({}, EventEmitter.prototype, {

    loadUser: function(user_id, callback) {
        var url = "/notee/api/users/" + user_id;
        request.get(url, (err, res) => {
            if(err){return;}
            if(!res.body){return;}
            callback(res.body.user);
        })
    },

    loadUsers: function(callback) {
        request.get('/notee/api/users', (err, res) => {
            if(err){return;}
            if(!res.body){return;}
            callback(res.body.users);
        });
    },

    loadRoles: function(callback) {
        var url = "/notee/api/roles";
        request.get(url, (err, res) => {
            if(err){return;}
            if(!res.body){return;}
            callback(res.body.roles);
        })
    },

    emitChange: function(change_event) {
        this.emit(change_event);
    },

    addChangeListener: function(change_event, callback) {
        this.on(change_event, callback);
    }

});

NoteeDispatcher.register(function(action) {

    switch(action.type) {
        // user
        case Constants.USER_CREATE:
            user_create(action.content);
            break;
        case Constants.USER_UPDATE:
            user_update(action.content);
            break;
        case Constants.USER_DELETE:
            user_delete(action.user_id);
            break;
    }
});

module.exports = UserStore;

@@ -1591,32 +1591,570 @@
 ;%0A        %7D)%0A%7D%0A%0A
+function user_password_update(content) %7B%0A    request%0A        .put(%22/notee/api/users/mypage%22)%0A        .field('user%5Bnow_password%5D', content.now_password)%0A        .field('user%5Bpassword%5D', content.password)%0A        .field('user%5Bpassword_confirm%5D', content.password_confirm)%0A        .end(function(err, res)%7B%0A            if(err %7C%7C !res.body)%7B%0A                UserStore.emitChange(Constants.USER_PASSWORD_UPDATE_FAILED);%0A                return false;%0A            %7D%0A            UserStore.emitChange(Constants.USER_PASSWORD_UPDATE);%0A        %7D)%0A%7D%0A%0A
 function user_de
@@ -2170,16 +2170,16 @@
 e_src)%7B%0A
-
     requ
@@ -3232,24 +3232,272 @@
  %7D)%0A    %7D,%0A%0A
+    loadUserByToken: function(callback) %7B%0A        var url = %22/notee/api/users/mypage%22;%0A        request.get(url, (err, res) =%3E %7B%0A            if(err)%7Breturn;%7D%0A            if(!res.body)%7Breturn;%7D%0A            callback(res.body.user);%0A        %7D)%0A    %7D,%0A%0A
     emitChan
@@ -3892,24 +3892,24 @@
 SER_UPDATE:%0A
-
             
@@ -3948,32 +3948,146 @@
          break;%0A
+        case Constants.USER_PASSWORD_UPDATE:%0A            user_password_update(action.content);%0A            break;%0A
         case Con

add update_password method & loadUserByToken method