import Immutable from 'immutable';
import io from 'socket.io-client';

import ajax from '../lib/ajax';
import Dispatcher from '../dispatcher/Dispatcher';
import createMessage from '../lib/createMessage';

export default {

    fetchAll(channel) {
        console.log('ACTION', 'fetch all messages for channel:', channel);

        ajax.get('/messages/' + channel).then(
            messages => {
                Dispatcher.dispatch({
                    type: 'receive_messages',
                    channel: channel,
                    messages: Immutable.fromJS(messages)
                });
            },
            err => {
                Dispatcher.dispatch({
                    type: 'receive_messages_failed',
                    channel: channel,
                    error: err
                });
            });
    },

    create(channel, message) {
        console.log('ACTION', 'saving message:', message.get('fields').toJS());

        ajax.post('/message/' + channel, message.get('fields').toJS()).then(
            res => {
                let fields = Immutable.fromJS(res);
                console.log('ACTION', 'save successful:', res);

                Dispatcher.dispatch({
                    type: 'save_message_success',
                    channel: channel,
                    message: message.set('fields', fields)
                });
            },
            err => {
                console.log('ACTION', 'save failed:', err);

                Dispatcher.dispatch({
                    type: 'save_message_failed',
                    error: err,
                    channel: channel,
                    message: message
                });
            });
    },

    connect() {
        const socket = io();
        socket.on('message', data => {
            console.log('ACTION', 'receive WS', data);
            Dispatcher.dispatch({
                type: 'receive_message',
                channel: data.channel,
                message: createMessage(data.message)
            });
        });
    }

};


@@ -224,24 +224,31 @@
     
-fetchAll(channel
+create(channel, message
 ) %7B%0A
@@ -282,49 +282,54 @@
 ', '
-fetch all messages for channel:', channel
+saving message:', message.get('fields').toJS()
 );%0A%0A
@@ -341,18 +341,19 @@
    ajax.
-ge
+pos
 t('/mess
@@ -355,17 +355,16 @@
 /message
-s
 /' + cha
@@ -367,24 +367,196 @@
  channel
-).then(%0A
+, message.get('fields').toJS()).then(%0A            res =%3E %7B%0A                console.log('ACTION', 'save successful:', res);%0A%0A                let fields = Immutable.fromJS(res);%0A    
         
@@ -566,22 +566,50 @@
  message
-s
  =
-%3E %7B
+ message.set('fields', fields);%0A
 %0A       
@@ -658,37 +658,34 @@
          type: '
-recei
+sa
 ve_messages',%0A  
@@ -678,16 +678,23 @@
 _message
+_succes
 s',%0A    
@@ -758,37 +758,17 @@
 sage
-s
 : 
-Immutable.fromJS(
 message
-s)
 %0A   
@@ -812,32 +812,93 @@
        err =%3E %7B%0A
+                console.log('ACTION', 'save failed:', err);%0A%0A
                 
@@ -938,37 +938,34 @@
          type: '
-recei
+sa
 ve_messages_fail
@@ -958,17 +958,16 @@
 _message
-s
 _failed'
@@ -960,32 +960,64 @@
 essage_failed',%0A
+                    error: err,%0A
                 
@@ -1058,26 +1058,32 @@
         
-error: err
+message: message
 %0A       
@@ -1123,39 +1123,32 @@
 %7D,%0A%0A    
-create(channel, message
+fetchAll(channel
 ) %7B%0A    
@@ -1178,54 +1178,49 @@
 ', '
-saving message:', message.get('fields').toJS()
+fetch all messages for channel:', channel
 );%0A%0A
@@ -1224,35 +1224,34 @@
 ;%0A%0A        ajax.
-pos
+ge
 t('/message/' + 
@@ -1237,32 +1237,33 @@
 ax.get('/message
+s
 /' + channel, me
@@ -1262,38 +1262,8 @@
 nnel
-, message.get('fields').toJS()
 ).th
@@ -1282,133 +1282,21 @@
     
-r
+m
 es
+sages
  =%3E %7B
-%0A                let fields = Immutable.fromJS(res);%0A                console.log('ACTION', 'save successful:', res);%0A
 %0A   
@@ -1349,34 +1349,37 @@
          type: '
-sa
+recei
 ve_message_succe
@@ -1364,39 +1364,32 @@
 'receive_message
-_succes
 s',%0A            
@@ -1445,37 +1445,35 @@
 sage
+s
 : 
-message.set('fields', field
+Immutable.fromJS(message
 s)%0A 
@@ -1531,69 +1531,8 @@
 %3E %7B%0A
-                console.log('ACTION', 'save failed:', err);%0A%0A
     
@@ -1584,34 +1584,37 @@
          type: '
-sa
+recei
 ve_message_faile
@@ -1599,32 +1599,33 @@
 'receive_message
+s
 _failed',%0A      
@@ -1622,40 +1622,8 @@
 d',%0A
-                    error: err,%0A
     
@@ -1676,32 +1676,26 @@
         
-message: message
+error: err
 %0A       
@@ -1727,24 +1727,25 @@
 %7D);%0A    %7D,%0A%0A
+%0A
     connect(

create before fetchAll