import {Map} from 'immutable';
import * as types from '../constants/ActionTypes';
import initialWorld from '../library/initialWorld';


const getStateWithNextDetail = (state, action) => {
  /* eslint indent: 0 */
  const getInfoDetail = (detail) => {
    console.log(detail.get(0).size, 'SIIIIIIIIZE');
    switch (detail.get(0).size) {
    case 2:
        return {
          START_POINT: 4,
          SIZE: 2
        };
    case 3:
        return {
          START_POINT: 3,
          SIZE: 3
        };
    case 4:
        return {
          START_POINT: 3,
          SIZE: 4
        };
    default:
        return {};
    }
  };

  const infoDetail = getInfoDetail(action.detail);

  const rangeFilter = (start, size) => x => !(start <= x && x < start + size);

  const rangeFilterDetail = rangeFilter(
    infoDetail.START_POINT,
    infoDetail.SIZE
  );

  const shadow = state.filter((row, y) => row.get('id') < 0);
  console.dir(action.detail.toString());
  const shadowWithDetail = shadow.map((row, y) => {
    let nextSizeDetail = 0; // detail x
    if (y >= infoDetail.SIZE) return row;

    return Map({
      id: row.get('id'),
      blocks: row.get('blocks').map((block, x) => {
        if (rangeFilterDetail(x)) return block;

        return block.set('value', action.detail.get(y).get(nextSizeDetail++));
      })
    });
  });

  return state.merge(shadowWithDetail);
};

const shiftDownBlock = (state, action) => {
  const flags = new Array(10).fill(false);

  return state.map((row, y) => row.get('blocks').map((block, x) => {
    if (action.options.some(val => val === block.get('value'))
      && state.has(y + 1)) {
      flags[x] = true;
      return block.set('value', 0);
    }
    if (!flags[x]) return block;

    flags[x] = !flags[x];
    return block.set('value', 2);
  }));
};


const world = (state = initialWorld(24, 10), action) => {
  switch (action.type) {
  case types.DOWN_BLOCK:
      return shiftDownBlock(state, action);
  case types.TRANSFORM_BLOCK:
      return state;
  case types.MOVE_DETAIL:
      return state;
  case types.NEXT_DETAIL:
      return getStateWithNextDetail(state, action);
  case types.ROTATE_DETAIL:
      return state;
  case types.COMPLETE_ROW:
      return state;
  case types.OVER_GAME:
      return state;
  case types.START_GAME:
      return state;
  default:
      return state;
  }
};

export default world;

@@ -248,60 +248,8 @@
 %3E %7B%0A
-    console.log(detail.get(0).size, 'SIIIIIIIIZE');%0A
     
@@ -867,49 +867,8 @@
 0);%0A
-  console.dir(action.detail.toString());%0A
   co
@@ -1398,32 +1398,59 @@
 .map((row, y) =%3E
+ row.set(%0A    'blocks',%0A   
  row.get('blocks
@@ -1472,24 +1472,26 @@
 x) =%3E %7B%0A    
+  
 if (action.o
@@ -1543,16 +1543,18 @@
 )%0A      
+  
 && state
@@ -1564,24 +1564,89 @@
 s(y + 1)) %7B%0A
+        const newBlock = block.set('value', flags%5Bx%5D ? 2 : 0);%0A  
       flags%5B
@@ -1666,77 +1666,101 @@
     
+  
 return 
-block.set('value', 0);%0A    %7D%0A    if (!flags%5Bx%5D) return
+newBlock;%0A      %7D%0A%0A      const newBlock = flags%5Bx%5D ? block.set('value', 2) :
  block;%0A
 %0A   
@@ -1755,21 +1755,22 @@
  block;%0A
-%0A
     
+  
 flags%5Bx%5D
@@ -1776,56 +1776,47 @@
 %5D = 
-!flags%5Bx%5D;%0A    return block.set('value', 2);
+false;%0A      return newBlock;%0A    %7D)
 %0A  
-%7D
 ));%0A

create reduce world shiftDownBlock()