import { HIT_FACE } from './minion';
import { Record } from 'immutable';

const ADD_MAX_MANA = 'ADD_MANA';
const ADD_SPENDABLE_MANA = 'ADD_SPENDABLE_MANA';
const FILL_MAX_MANA = 'FILL_MAX_MANA';

export function addMaxMana({ target, amount = 1 }) {
  return {
    target,
    amount,
    type: ADD_MAX_MANA,
  };
}

export function addSpendableMana({ target, amount = 1 }) {
  return {
    target,
    amount,
    type: ADD_SPENDABLE_MANA,
  };
}

export function fillMaxMana({ target }) {
  return {
    target,
    type: FILL_MAX_MANA,
  };
}

export function addAndFillMana({ target }) {
  return dispatch => {
    dispatch(addMaxMana({ target }));
    dispatch(fillMaxMana({ target }));
  };
}

const manaReducerInitialState = new Record({
  max: 0,
  spendableMana: 0,
});

function manaReducer(state = manaReducerInitialState(), action) {
  switch (action.type) {
    case ADD_MAX_MANA:
      return state.update('max', max => max + action.amount);
    case ADD_SPENDABLE_MANA:
      return state.update('spendableMana', spendableMana => spendableMana + action.amount);
    case FILL_MAX_MANA:
      return state.set('spendableMana', state.get('max'));
    default:
      return state;
  }
}

const initialState = new Record({
  health: 30,
  mana: manaReducer(undefined, {}),
});

export default function characterReducer(state = initialState(), action) {
  switch (action.type) {
    case HIT_FACE:
      return state.update('health', health => health - action.damage);
    case ADD_MAX_MANA:
    case ADD_SPENDABLE_MANA:
    case FILL_MAX_MANA:
      return state.update('mana', mana => manaReducer(mana, action));
    default:
      return state;
  }
}

@@ -187,16 +187,49 @@
 X_MANA';
+%0Aconst SPEND_MANA = 'SPEND_MANA';
 %0A%0Aexport
@@ -721,24 +721,137 @@
 ));%0A  %7D;%0A%7D%0A%0A
+export function spendMana(%7B target, amount %7D) %7B%0A  return %7B%0A    target,%0A    amount,%0A    type: SPEND_MANA,%0A  %7D;%0A%7D%0A%0A
 const manaRe
@@ -1294,24 +1294,137 @@
 et('max'));%0A
+    case SPEND_MANA:%0A      return state.update('spendableMana', spendableMana =%3E spendableMana - action.amount);%0A
     default:
@@ -1778,24 +1778,24 @@
 DABLE_MANA:%0A
-
     case FIL
@@ -1798,32 +1798,53 @@
  FILL_MAX_MANA:%0A
+    case SPEND_MANA:%0A
       return sta

Add spendMana action