#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "survey.h"

#include "manhat-lib/shared_survey_util.h"
#include "manhat-lib/shared_survey_read_param.h"
#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_survey_modify.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_page.h"
#include "manhat-lib/shared_cs_util.h"




void
set_data( SURVEY_DATA *data)
{
   char *str;

   cs_set_int_value("data.id", data->id);
   str = replace_disallowed_html_code(data->caption);
   cs_set_value("data.caption", str);
   free(str);

} 
void
set_modify_data( SURVEY_DATA *data)
{
   cs_set_int_value("modify", 1);
   set_data(data);
}
void
set_delete_data(SURVEY_DATA *data)
{
   cs_set_int_value("delete", 1);
   set_data(data);
} 




void
do_action( SESSION *user,
		int is_admin, const char *key, const char *count, int pre_id)
{
   SURVEY_LIST *list;
   char file_path[MAX_PATH +1];
   char survey_path[MAX_PATH +1];
   SURVEY_DATA *data;
   int before_me =0;
   char *str_m=0, *str_d=0, *str_cp=0, *str_cut=0, *str_paste;

   str_m = hdf_get_value(global_cgi->hdf, "Query.modify", 0);
   str_d = hdf_get_value(global_cgi->hdf, "Query.delete", 0);
   str_cp = hdf_get_value(global_cgi->hdf, "Query.copy", 0);
   str_cut = hdf_get_value(global_cgi->hdf, "Query.cut", 0);
   str_paste = hdf_get_value(global_cgi->hdf, "Query.paste", 0);

   if(str_m || str_d || str_cp || str_cut)
   {
      get_survey_path(user, survey_path, count);
      snprintf(file_path, MAX_PATH +1, "%s/%s",survey_path, SURVEY_XML_FNAME);
      list= survey_data_parser(file_path);
      data = get_one_survey_data(list, pre_id);
     if(data)
     {
       if(str_m)
       {
	   set_modify_data( data);
	   insert_survey_spell_check_button();   /* shared_survey_util.c */
	   cs_load_lang("shared_survey_form");    /* shared_cs_util.c */
	   cs_cgi_display("survey_text_form", 1);
       }
       else if(str_d)
       {
	  set_delete_data( data);
          cs_load_lang("shared_survey_form");    /* shared_cs_util.c */
          cs_cgi_display("survey_text_form", 1);
       }
       else if(str_cp  || str_cut)
       {
           write_to_clipboard( count, data, user);
           if(str_cut)
             delete_cut_action( list, user,  count,  pre_id, &before_me);
           printf("Location: %s?admin=%d&id=%s&count=%s#%d\n\n",
                   "survey_new", is_admin, key, count, before_me == 0 ? pre_id : before_me);

       }

     }
     else
      cs_critical_error(ERR_SURVEY_NO_DATA, "");
     free_survey_data(data);
     free_survey_data_list(list);
   }
   else if(str_paste)
   {
      paste_action( user, is_admin, key,  count,   pre_id);
      printf("Location: %s?admin=%d&id=%s&count=%s#%d\n\n",
               "survey_new", is_admin, key, count, pre_id);
   }
   else
     printf("Location: %s?admin=%d&id=%s&count=%s&pre_id=%d\n\n",
             "survey_text_form", is_admin, key, count, pre_id);
}


int
main()
{
   int is_admin;
   char key[MAX_KEY +1];
   char count[MAX_PATH +1];
   int  pre_id;
   SESSION user;

   cs_cgi_init();  /* shared_cs_util.c */
   read_com_parameters(&is_admin, key, count, &pre_id);
   verify_key_path(&user, key, is_admin);
   do_action( &user, is_admin, key,  count,  pre_id);
   cs_cgi_destroy();
   return 0;
}
