% @since: 2014-01-15, @author: H Roumani, roumani@cse.yorku.ca %
classdef DatabaseCreate < c4e.RfidListener
   
   % The following attributes hold the object's state.
   % They are available to all methods in this class.
   properties
      rfid;
      database;
      % add your attributes here
      numberkeys = 0;

   end
    
   % The following functions exhibit the object's behavior
   methods
   
       function this = DatabaseCreate()  % Object Constructor, rename but do not modify
         this.database = c4e.simpleDB();  % c4e.simpleDB() eventually...
         engine = c4e.Engine(125, 100);
         engine.addPhidgetListener(this);     
         engine.start();
         this.rfid = engine.getRFIDPhidget();
         this.main();
         engine.stop();
       end   
      
      function main(this) % Called upon object creation
         disp('Press ENTER to start ...'); pause;
         if (this.rfid ~= [] && this.rfid.isAttached())
             disp('rfid board attached');
            this.rfid.setAntennaOn(true);
            % Use the rfid board, e.g. to turn on its LED
            this.rfid.setLEDOn(true);
         end
         while(this.database.size() < 3)
             pause(1) % pause the main for a second
         end
         this.database.print();
         fileID = fopen('database.csv', 'w')
         for i =1:this.database.size()
             z = this.database.get(i);
             fprintf('rfid %s pin %s\n', z.getRFID(), z.getPassword());
             fprintf(fileID, '%s,%s\n', z.getRFID(), z.getPassword());
         end
         fclose(fileID);
         disp('Press ENTER to terminate ...'); pause;
      end
          
      % ----------------------------------- These are event handlers
      function attached(this, event)
         fprintf('Attach event at %d for phidget with serial number: %d\n', event.getWhen(), event.getId());
      end
      function detached(this, event)
         fprintf('Detach event at %d for phidget with serial number: %d\n', event.getWhen(), event.getId());
      end

      function rfidGain(this, event)
         fprintf('RFID tag gain with protocol #%d and tag %s at %d\n', event.getId(), char(event.getTag()), event.getWhen());
         newkey = char(event.getTag());
         fprintf('as a string %s\n', newkey);
         if(this.database.containsKey(newkey))
             fprintf('Already have tag %s in the database\n', newkey);
         else
             fprintf('a new key %s\n', newkey);
             pin = input('enter the pin:', 's');
             fprintf('pin is %s\n', pin);
             this.database.put(newkey, pin);
             this.numberkeys = this.numberkeys + 1;
             fprintf('number of keys %d\n', this.numberkeys);
         end
      end
      function rfidLoss(this, event)
         fprintf('RFID tag lost with protocol #%d and tag %s at %d\n', event.getId(), char(event.getTag()), event.getWhen());
      end
      function doChange(this, event)
          fprintf('doChange ');
      end

   end
end
