'use strict';

var I                = require( 'immutable' );
var Marty            = require( 'marty' );
var faker            = require('faker');
var FormGenerator    = require( 'react-form-generator' );
var t                = FormGenerator.tools;
var TableConstants   = require( '../constants/TableFormConstants' );
var DetailsConstants = require( '../constants/DetailsFormConstants' );
// var DetailsFormStore = require( './DetailsFormStore' );

var ROWS_NUMBER   = 100;

module.exports = Marty.createStore({
    id: 'TableFormStore',
    
    handlers: {
        changePage      : TableConstants.CHANGE_TABLE_PAGE,
        addRow          : TableConstants.ADD_TABLE_ROW,
        editRow         : TableConstants.EDIT_TABLE_ROW,
        handleFormEvent : DetailsConstants.HANDLE_FORM_EVENT
    },
    
    
    getInitialState: function () { 
        var PAGE_SIZE = 10;

        this.__route = t.buildRouter(
            'btnCancel:click', [ openForm ],
            'btnSave:click',   [ openForm ]
        );

        return I.Map({
            paging: I.Map({
                total   : Math.ceil( this.__rows.count() / PAGE_SIZE ),
                current : 0,
                size    : PAGE_SIZE
            }),
            
            isVisible: true
        }); 
        

        function openForm ( prom ) {
            var self = this;

            prom.then(function ( val ) {
                if ( val ) {
                    var idx = self.__rows.findIndex(function ( el ) {
                        return el.get( 'id' ) === val.get( 'id' );
                    });
                    self.__rows = self.__rows.splice( idx, 1, val );
                }
                
                self.state = self.state.set( 'isVisible', true );

                /* Well, it's a kind of workaround
                   I'm working on this */
                self.action = null;

                self.hasChanged();
            });
        }
    },


    /* ====================================================== */
    /* ====================== ACTIONS ====================== */
    /* ====================================================== */
    changePage: function ( pageNum ) {
        var oldPaging = this.state.get( 'paging' );
        var newPaging = oldPaging.set( 'current', pageNum );

        this.state = this.state.set( 'paging', newPaging );
        this.hasChanged();
    },
    

    addRow: function ( row ) {
        this.state = this.state.set( 'isVisible', false );
        this.hasChanged();
    },

    editRow: function ( row ) {
        this.state = this.state.set( 'isVisible', false );
        this.hasChanged();
    },
    

    handleFormEvent: function ( path, e, __, prom ) {
        // log.debug( 'TableFormStore.handleFormEvent :: ', prom );
        // this.waitFor( DetailsFormStore );
        this.__route.call( this, path, prom );
    },


    /* ====================================================== */
    /* ====================== GETTERS ======================= */
    /* ====================================================== */

    getRows: function () {
        var paging = this.getPaging();
        var start  = paging.get( 'current' ) * paging.get( 'size' );
        var end    = start + paging.get( 'size' );
        return this.__rows.slice( start, end );
    },
    
    getPaging: function () {
        return this.state.get( 'paging' );
    },


    /* ====================================================== */
    /* ====================== HELPERS ======================= */
    /* ====================================================== */
    __rows: createRows( ROWS_NUMBER )
});


function createRows ( size ) {
    return I.Range( 0, size ).map(function ( idx ) { 
        return I.Map({
            id     : 'request_' + idx,
            author : faker.name.findName(),
            title  : faker.lorem.sentence()
        });
    }).toList();
}



@@ -1402,58 +1402,298 @@
     
-if ( val ) %7B%0A                    var idx = self.__
+var recordID, rowsNum, newVal, rows, editedIdx;%0A%0A                if ( val ) %7B%0A                    recordID = val.get( 'id' );%0A                    rows     = self.__rows;%0A                    rowsNum  = rows.count();%0A%0A                    if ( recordID ) %7B%0A                        editedIdx = 
 rows
@@ -1717,24 +1717,28 @@
 on ( el ) %7B%0A
+    
             
@@ -1775,33 +1775,30 @@
 ' ) === 
-val.get( 'id' );%0A
+recordID;%0A    
         
@@ -1837,29 +1837,19 @@
     
-self.__rows = self.__
+    rows = 
 rows
@@ -1857,17 +1857,23 @@
 splice( 
-i
+editedI
 dx, 1, v
@@ -1890,24 +1890,28 @@
             
+    
 %7D%0A          
@@ -1908,32 +1908,260 @@
                 
+ 
+   else %7B%0A                        newVal = val.set( 'id', 'record_' + rowsNum );%0A                        rows   = rows.push( newVal );%0A                    %7D%0A                %7D%0A                %0A                self.__rows = rows;
 %0A               
@@ -2172,16 +2172,17 @@
 f.state 
+ 
 = self.s
@@ -2181,24 +2181,45 @@
 = self.state
+%0A                    
 .set( 'isVis
@@ -2231,16 +2231,233 @@
 , true )
+%0A                    .mergeIn(%5B 'paging' %5D, %7B%0A                        total   : Math.ceil( rowsNum / PAGE_SIZE ),%0A                        current : 0,%0A                        size    : PAGE_SIZE%0A                    %7D)
 ;%0A%0A     
@@ -4475,13 +4475,12 @@
  're
-quest
+cord
 _' +

Add new record creation