'use strict';

import { actions } from './actions.js';


const [
  move_forward, move_backward, 
  turn_left, turn_right,
  place, trigger,
  pretend_frozen, unfreeze,
  take_flag,
  ...rest
] = actions;


test("move forward", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(move_forward.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] },
  ]);
  expect(move_forward.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] },
  ]);
  expect(move_forward.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] },
  ]);
  expect(move_forward.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] },
  ]);

  expect(move_forward.propose(null, n, [0])).toEqual([]);
  expect(move_forward.propose(null, s, [-1])).toEqual([]);
  expect(move_forward.propose(null, e, [0])).toEqual([]);
  expect(move_forward.propose(null, w, [-1])).toEqual([]);
});


test("move backward", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(move_backward.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] },
  ]);
  expect(move_backward.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] },
  ]);
  expect(move_backward.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] },
  ]);
  expect(move_backward.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] },
  ]);

  expect(move_backward.propose(null, n, [0])).toEqual([]);
  expect(move_backward.propose(null, s, [-1])).toEqual([]);
  expect(move_backward.propose(null, e, [0])).toEqual([]);
  expect(move_backward.propose(null, w, [-1])).toEqual([]);
});


test("turn_left", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(turn_left.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] },
  ]);
  expect(turn_left.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] },
  ]);
  expect(turn_left.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] },
  ]);
  expect(turn_left.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] },
  ]);

  expect(turn_left.propose(null, n, [0])).toEqual([]);
  expect(turn_left.propose(null, s, [-1])).toEqual([]);
  expect(turn_left.propose(null, e, [0])).toEqual([]);
  expect(turn_left.propose(null, w, [-1])).toEqual([]);
});


test("turn_right", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(turn_right.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] },
  ]);
  expect(turn_right.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] },
  ]);
  expect(turn_right.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] },
  ]);
  expect(turn_right.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] },
  ]);

  expect(turn_right.propose(null, n, [0])).toEqual([]);
  expect(turn_right.propose(null, s, [-1])).toEqual([]);
  expect(turn_right.propose(null, e, [0])).toEqual([]);
  expect(turn_right.propose(null, w, [-1])).toEqual([]);
});


test("place", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(place.propose(null, n, [1])).toEqual([
    { lattice_changes: [
      { 
        x: 0, y: -1, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);
  expect(place.propose(null, s, [1])).toEqual([
    { lattice_changes: [
      { 
        x: 0, y: 1, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);
  expect(place.propose(null, e, [1])).toEqual([
    { lattice_changes: [
      { 
        x: 1, y: 0, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);
  expect(place.propose(null, w, [1])).toEqual([
    { lattice_changes: [
      { 
        x: -1, y: 0, from: 'empty', to: 'mutable', 
        flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } 
      }
    ]}
  ]);

  expect(place.propose(null, n, [0])).toEqual([]);
  expect(place.propose(null, s, [-1])).toEqual([]);
  expect(place.propose(null, e, [0])).toEqual([]);
  expect(place.propose(null, w, [-1])).toEqual([]);
});


test("trigger", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(trigger.propose(null, n, [1])).toEqual([
    { lattice_changes: [{
      x: 0, y: -1, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);
  expect(trigger.propose(null, s, [1])).toEqual([
    { lattice_changes: [{
      x: 0, y: 1, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);
  expect(trigger.propose(null, e, [1])).toEqual([
    { lattice_changes: [{
      x: 1, y: 0, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);
  expect(trigger.propose(null, w, [1])).toEqual([
    { lattice_changes: [{
      x: -1, y: 0, from: 'mutable', to: 'active',
      flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] },
    }]},
  ]);

  expect(trigger.propose(null, n, [0])).toEqual([]);
  expect(trigger.propose(null, s, [-1])).toEqual([]);
  expect(trigger.propose(null, e, [0])).toEqual([]);
  expect(trigger.propose(null, w, [-1])).toEqual([]);
 });


test("pretend frozen", () => {
  const agent = { id: 2, x: 0, y: 0, flags: { orientation: 'n' } };

  expect(pretend_frozen.propose(null, agent, [1])).toEqual([{
    agent_changes: [{
      agent_id: 2,
      flags: { pretend_frozen: true },
    }]
  }]);

  expect(pretend_frozen.propose(null, agent, [0])).toEqual([{
    agent_changes: [{
      agent_id: 2,
      flags: { pretend_frozen: false },
    }]
  }]);
});


test("unfreeze", () => {
  const agent1 = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const agent2 = { id: 1, x: 0,  y: -1 };
  const agent3 = { id: 10, x: -1, y: 0 };
  const agents = [ agent1, agent2, agent3 ];

  expect(unfreeze.propose({agents}, agent1, [1])).toEqual([{
    agent_changes: [{
      agent_id: 1,
      flags: { frozen: false, emit: [ 0, 1, 0, 0, 0, 0, 0, 0 ] },
    }]
  }]);

  agent1.flags.orientation = 'w';

  expect(unfreeze.propose({agents}, agent1, [1])).toEqual([{
    agent_changes: [{
      agent_id: 10,
      flags: { frozen: false, emit: [ 0, 1, 0, 0, 0, 0, 0, 0 ] },
    }]
  }]);

  agent1.flags.orientation = 's';
  expect(unfreeze.propose({agents}, agent1, [1])).toEqual([]);

  expect(unfreeze.propose({agents}, agent1, [0])).toEqual([]);
});


test("take flag", () => {
  const agent = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const other1 = { id: 1, x: 0, y: -1, flags: { flag: true } };
  const other2 = { id: 2, x: 0, y: 1, flags: { flag: false } };

  const world = { agents: [ agent, other1, other2 ] };

  expect(take_flag.propose(world, agent, [1])).toEqual([
    { agent_changes: [
      { agent_id: 1, flags: { flag: false } },
      { agent_id: 0, flags: { flag: true  } },
    ]},
  ]);

  agent.flags.orientation = 's';
  expect(take_flag.propose(world, agent, [1])).toEqual([
    { 
      lattice_changes: [
        { 
          x: 0, y: 1, from: 'flag', to: 'empty',
          flags: { emit: [ 0, 0, 1, 0, 0, 0, 0, 0 ] },
        },
      ],
      agent_changes: [
        { agent_id: 0, flags: { flag: true  } },
      ]
    },
  ]);

  expect(take_flag.propose(world, agent, [0])).toEqual([]);
});