import { combineReducers } from 'redux';

import {
  WEAVE_ON, WEAVE_OFF,
  THREADING_ON, THREADING_OFF,
  TREADLING_ON, TREADLING_OFF,
  TIE_UP_ON, TIE_UP_OFF,
  SAVE_SETTINGS} from '../constants';

function initNewState(state) {
  return Object.assign({}, state,
    {
      weaves: [...state.weaves],
      numberOfWeaves: state.numberOfWeaves,
      threadings: [...state.threadings],
      treadlings: [...state.treadlings],
      tieUp: [...state.tieUp],
      settings: Object.assign({}, state.settings)
    }
  );
}

function initialState() {
  const settings = {
    ro: 30,
    ry: 15,
    threadingSize: 4,
    treadlingSize: 4,
    elementSize: 20
  };

  const tieUp = [];
  for (let i = 0; i < settings.treadlingSize; i++) tieUp.push([]);

  const newState = {
    weaves: [],
    numberOfWeaves: 0,
    threadings: [],
    treadlings: [],
    tieUp: tieUp,
    settings: settings
  };

  const store4State = localStorage.getItem('store4State');

  if (store4State) {
    const persistedState = JSON.parse(store4State);
    return persistedState.switcher4;
  } else {
    return newState;
  }
}

function switcherWeaveOn(newState, action) {
  newState.numberOfWeaves++;

  if (newState.weaves[action.row] === undefined
      || newState.weaves[action.row] === null)
    newState.weaves[action.row] = [];

  newState.weaves[action.row][action.col] = true;
  return newState;
}

function switcherWeaveOff(newState, action) {
  newState.numberOfWeaves--;

  if (newState.weaves[action.row] === undefined
      || newState.weaves[action.row] === null)
    newState.weaves[action.row] = [];

  newState.weaves[action.row][action.col] = false;
  return newState;
}

function switcherTieUpOn(newState, action) {
  newState.tieUp[action.row][action.col] = true;
  return newState;
}

function switcherTieUpOff(newState, action) {
  newState.tieUp[action.row][action.col] = false;
  return newState;
}

function switcherThreadingOn(newState, action) {
  newState.threadings[action.col] = action.row;
  return newState;
}

function switcherThreadingOff(newState, action) {
  newState.threadings[action.col] = undefined;
  return newState;
}

function switcherTreadlingOn(newState, action) {
  newState.treadlings[action.col] = action.row;
  return newState;
}

function switcherTreadlingOff(newState, action) {
  newState.treadlings[action.col] = undefined;
  return newState;
}

function saveSettings(newState, action) {
  newState.settings = Object.assign({}, action.settings);
  return newState;
}



function switcher4(state, action) {
  switch (action.type) {
    case WEAVE_ON: return switcherWeaveOn(initNewState(state), action);
    case WEAVE_OFF: return switcherWeaveOff(initNewState(state), action);
    case TIE_UP_ON: return switcherTieUpOn(initNewState(state), action);
    case TIE_UP_OFF: return switcherTieUpOff(initNewState(state), action);
    case THREADING_ON: return switcherThreadingOn(initNewState(state), action);
    case THREADING_OFF: return switcherThreadingOff(initNewState(state), action);
    case TREADLING_ON: return switcherTreadlingOn(initNewState(state), action);
    case TREADLING_OFF: return switcherTreadlingOff(initNewState(state), action);
    case SAVE_SETTINGS: return saveSettings(initNewState(state), action);
    default: return initialState();
  }
}

const reducer4 = combineReducers({switcher4});

export default reducer4;

@@ -503,17 +503,33 @@
 ettings)
+,%0A      diff: %7B%7D
 %0A
-
     %7D%0A  
@@ -903,16 +903,30 @@
 settings
+,%0A    diff: %7B%7D
 %0A  %7D;%0A%0A 
@@ -1783,32 +1783,104 @@
 on.col%5D = true;%0A
+  newState.diff.tieUp = %7Bcol: action.col, row: action.row, flag: true%7D;%0A
   return newStat
@@ -1973,32 +1973,105 @@
 n.col%5D = false;%0A
+  newState.diff.tieUp = %7Bcol: action.col, row: action.row, flag: false%7D;%0A
   return newStat
@@ -2165,32 +2165,96 @@
 %5D = action.row;%0A
+  newState.diff.threadings = %7Bcol: action.col, row: action.row%7D%0A
   return newStat
@@ -2348,32 +2348,94 @@
 l%5D = undefined;%0A
+  newState.diff.threadings = %7Bcol:action.col, row: undefined%7D%0A
   return newStat
@@ -2537,16 +2537,81 @@
 on.row;%0A
+  newState.diff.treadlings = %7Bcol: action.col, row: action.row%7D;%0A
   return
@@ -2721,16 +2721,80 @@
 efined;%0A
+  newState.diff.treadlings = %7Bcol: action.col, row: undefined%7D;%0A
   return
@@ -2907,16 +2907,49 @@
 tings);%0A
+  newState.diff.settings = true;%0A
   return
@@ -2963,13 +2963,11 @@
 te;%0A
+
 %7D%0A%0A
-%0A%0A
 func

Add diff state