/*
 * input map code
 *
 * Copyright 2003 Jacob Robbins
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>

#include "../include.h"

void little_callback_test_1(void* target_object, int target_index, void* source_event, size_t source_offset){
  
  printf("\nNOW IN CALLBACK #1\n");

}

void little_callback_test_2(void* target_object, int target_index, void* source_event, size_t source_offset){
  
  printf("\nNOW IN CALLBACK #2\n");

}



/*test the input map object's functionality for rough accuracy verification*/

int main(int argc, char** argv){
  char ch,ch2;
  int i,j;
  event_map_action_t* act;
  event_submap_t* sbmap;
  event_input_map_t* local_input_map;
  my_midi_event_t ev1;
  input_map_callback_t  cbarr[2];
  cbarr[0] = little_callback_test_1;
  cbarr[1] = little_callback_test_2;

  printf("\n\n\n testing the input map object for rough accuracy verification..\n\n");
  
  /*Create local input map*/

  if (!(local_input_map =  event_input_map_create())){
    printf("could not make an input map, aborting\n");
    exit(3);
  }

  event_input_map_set_event_type(local_input_map, 2);

  printf("\n made input map with event type: %d  ", event_input_map_get_event_type(local_input_map));
  printf("and owner object %p\n",event_input_map_get_owner_object(local_input_map));
  print_input_map(local_input_map);





  /*make submaps*/
  
  i = EVENT_PARAMETER_DATA_TYPE_UCHAR;
  printf("\n\n appending a submap w/ data type %d\n",i);
  if ((event_input_map_append_submap(local_input_map,EVENT_PARAMETER_DATA_TYPE_UCHAR,SUBMAP_TEST_TYPE_EXACT))<0) {
    printf("could not append submap, aborting\n");
    exit(3);
  }
  print_input_map(local_input_map);

  
  i = EVENT_PARAMETER_DATA_TYPE_CHAR;
  printf("\n\n inserting a submap at position 0 w/ data type %d, test type range\n",i);
  if ((event_input_map_insert_submap(local_input_map,0,EVENT_PARAMETER_DATA_TYPE_UCHAR,SUBMAP_TEST_TYPE_RANGE))<0) {
    printf("could not insert submap, aborting\n");
    exit(3);
  }
  print_input_map(local_input_map);



  i = EVENT_PARAMETER_DATA_TYPE_INT;
  printf("\n\n inserting a submap at position 1 w/ data type %d\n",i);
  if ((event_input_map_insert_submap(local_input_map,1,EVENT_PARAMETER_DATA_TYPE_INT,SUBMAP_TEST_TYPE_EXACT))<0) {
    printf("could not insert submap, aborting\n");
    exit(3);
  }
  print_input_map(local_input_map);

  
  j = event_input_map_get_submap_array_size(local_input_map);
  i = EVENT_PARAMETER_DATA_TYPE_FLOAT;
  printf("\n\n inserting a submap at one past last position (%d) w/ data type %d\n",j,i);
  printf(" (note that the 'float' data type MUST be used with a 'range' test type)\n");
  if ((event_input_map_insert_submap(local_input_map,j,EVENT_PARAMETER_DATA_TYPE_FLOAT,SUBMAP_TEST_TYPE_RANGE))<0) {
    printf("could not insert submap, aborting\n");
    exit(3);
  }
  print_input_map(local_input_map);


  /*remove a submap*/

  printf("\n\n removing submap at position 1 \n");
  if ((event_input_map_remove_submap(local_input_map,1))<0) {
    printf("could not remove submap, aborting\n");
    exit(3);
  }
  print_input_map(local_input_map);


  /*add some tests to first submap*/

  printf("\n\n adding tests to first submap\n");
  sbmap = event_input_map_get_submap(local_input_map, 0);
  ch = 0; ch2 = 64;
  event_submap_append_test(sbmap,(void*)&ch,(void*)&ch2);
  ch = 65; ch2 = 100;
  event_submap_append_test(sbmap,(void*)&ch,(void*)&ch2);
  ch = 101; ch2 = 127;
  event_submap_append_test(sbmap,(void*)&ch,(void*)&ch2);
  print_input_map(local_input_map);


  /*add actions to first submap's tests*/

  printf("\n\n adding actions to first submap's tests\n");
  act = event_submap_append_test_action(sbmap, 0);
  event_map_action_set_type(act, EVENT_MAP_ACTION_TYPE_CALLBACK_FUNCTION);
  act = event_submap_append_test_action(sbmap, 1);
  event_map_action_set_type(act, EVENT_MAP_ACTION_TYPE_CALLBACK_FUNCTION);
  act = event_submap_append_test_action(sbmap, 1);
  event_map_action_set_type(act, EVENT_MAP_ACTION_TYPE_CALLBACK_FUNCTION);
  event_map_action_set_function_index(act,1);
  print_input_map(local_input_map);



  /* static test of little callbacks */

  printf("\n\n doing static testing of simple sample callback functions\n");
  ev1.status = 1; ev1.data1 = 50; ev1.data2 = 60;
  route_event(&ev1, &local_input_map, 1, 0, cbarr, 2);
  ev1.status = 65;
  route_event(&ev1, &local_input_map, 1, 0, cbarr, 2);



  printf("\n\n that is all of this test, be sure to try out the alsa rawmidi input test too");


  /*deallocate local input map*/
  printf("\n\ncleaning up\n");
  event_input_map_destroy(local_input_map);
  printf("\n\ndone \n\n");
  return 0;
}



