%% BARCODE READER

:- dynamic hasBarcodeReaderProperty/2.

scannedBarcode(Barcode,Results) :-
	%% need to fetch the current state.  Set up a state machine here.  Just do it simply for now.
	hasBarcodeReaderState(State),
	doActionForState(State,Barcode).


%% MODES

hasCommandBarcode(setMode(addInventory),'000000000001').
hasCommandBarcode(setMode(removeInventory),'000000000002').
hasCommandBarcode(setMode(moveInventory),'000000000003').
hasCommandBarcode(setMode(consumeInventory),'000000000004').

hasCommandBarcode(setMode(setInventoryLevel),'000000000005').


%% ACTIONS

hasCommandBarcode(setLocation(currentLocation),'000000000101').
hasCommandBarcode(setLocation(targetLocation),'000000000102').

hasCommandBarcode(resetState,'000000000110').

hasCommandBarcode(undoLastAction,'').


%% LOCATIONS

hasCommandBarcode(hasLocation(1),'000000000201'). 
hasCommandBarcode(hasLocation(2),'000000000202').
hasCommandBarcode(hasLocation(3),'000000000203').
hasCommandBarcode(hasLocation(4),'000000000204').
hasCommandBarcode(hasLocation(5),'000000000205').
hasCommandBarcode(hasLocation(6),'000000000206').
hasCommandBarcode(hasLocation(7),'000000000207').
hasCommandBarcode(hasLocation(8),'000000000208').
hasCommandBarcode(hasLocation(9),'000000000209').
hasCommandBarcode(hasLocation(10),'000000000210').
hasCommandBarcode(hasLocation(11),'000000000211'). 
hasCommandBarcode(hasLocation(12),'000000000212').
hasCommandBarcode(hasLocation(13),'000000000213').
hasCommandBarcode(hasLocation(14),'000000000214').
hasCommandBarcode(hasLocation(15),'000000000215').
hasCommandBarcode(hasLocation(16),'000000000216').
hasCommandBarcode(hasLocation(17),'000000000217').
hasCommandBarcode(hasLocation(18),'000000000218').
hasCommandBarcode(hasLocation(19),'000000000219').
hasCommandBarcode(hasLocation(20),'000000000220').

hasLocationLabel(1,'Andrews Fridge').
hasLocationLabel(2,'Andrews Freezer').
hasLocationLabel(3,'Andrews Foodcart').
hasLocationLabel(9,'Mid-level Fridge').
hasLocationLabel(10,'Mid-level Freezer').
hasLocationLabel(11,'Mid-level Pantry').
hasLocationLabel(12,'Mid-level Cabinets').
%% hasLocationLabel(13,'Mid-level X').
%% hasLocationLabel(14,'Mid-level X').
%% hasLocationLabel(15,'Mid-level X').
%% hasLocationLabel(16,'Mid-level X').
%% hasLocationLabel(17,'Mid-level X').
%% hasLocationLabel(18,'Mid-level X').
%% hasLocationLabel(19,'Mid-level X').
%% hasLocationLabel(20,'Mid-level X').
%% hasLocationLabel(21,'Mid-level X').
%% hasLocationLabel(22,'Mid-level X').
%% hasLocationLabel(23,'Mid-level X').
%% hasLocationLabel(24,'Mid-level X').
%% hasLocationLabel(25,'Mid-level X').
%% hasLocationLabel(26,'Mid-level X').
%% hasLocationLabel(27,'Mid-level X').
%% hasLocationLabel(28,'Mid-level X').
%% hasLocationLabel(29,'Mid-level X').
%% hasLocationLabel(30,'Mid-level X').


%% MODIFIERS

hasCommandBarcode(setModifier(negate),'0000000000300').

hasCommandBarcode(setModifier(number(0)),'0000000000310').
hasCommandBarcode(setModifier(number(1)),'0000000000311').
hasCommandBarcode(setModifier(number(2)),'0000000000312').
hasCommandBarcode(setModifier(number(3)),'0000000000313').
hasCommandBarcode(setModifier(number(4)),'0000000000314').
hasCommandBarcode(setModifier(number(5)),'0000000000315').
hasCommandBarcode(setModifier(number(6)),'0000000000316').
hasCommandBarcode(setModifier(number(7)),'0000000000317').
hasCommandBarcode(setModifier(number(8)),'0000000000318').
hasCommandBarcode(setModifier(number(9)),'0000000000319').

%% LOGIC

doActionForState(State,Barcode) :-
	hasCommandBarcode(setMode(CommandMode),Barcode),
	barcodeReaderRetractall(hasBarcodeReaderProperty(commandMode,_)),
	barcodeReaderAssert(hasBarcodeReaderProperty(commandMode,CommandMode)),
	!.
doActionForState(State,Barcode) :-
	hasCommandBarcode(setCommandModifier(CommandModifier),Barcode),
	barcodeReaderRetractall(hasBarcodeReaderProperty(commandModifier,_)),
	barcodeReaderAssert(hasBarcodeReaderProperty(commandModifier,CommandModifier)),
	!.
doActionForState(State,Barcode) :-	       
	hasBarcodeReaderProperty(commandModifier,number(N)),
	hasBarcodeReaderProperty(commandMode,CommandMode),
	doCommandForCommandModeNTimes(CommandMode,N,Barcode).

doCommandForCommandModeNTimes(addInventory,N,Barcode) :-
	barcodeReaderPermanentlyAssert(atTime(hasInventoryTransfer(Location1,Location2,Product,N))).


%% HELPER FUNCTIONS

barcodeReaderAssert(Assertion) :-
	assertz(Assertion).

barcodeReaderPermanentlyAssert(Assertion) :-
	assertz(Assertion).

barcodeReaderRetractall(Assertion) :-
	retractall(Assertion).


%% TESTS

%% testBarcodeSequence(['','']).