<commit_before>package org.pocketcampus.plugin.directory;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.pocketcampus.R;
import org.pocketcampus.core.communication.RequestParameters;
import org.pocketcampus.core.communication.ServerRequest;
import org.pocketcampus.core.plugin.PluginBase;
import org.pocketcampus.core.plugin.PluginInfo;
import org.pocketcampus.core.plugin.PluginPreference;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DirectoryPlugin extends PluginBase implements OnClickListener{

	Button search_button;
	EditText first_name;
	EditText last_name;
	EditText sciper;
	
	List<Person> resultsList;
	State state;
	
	private enum State {SEARCH, RESULT_LIST, RESULT_DETAIL};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		displaySearch();
		

		search_button = (Button)findViewById(R.id.directory_search_button);
		search_button.setOnClickListener(this);
		first_name = (EditText)findViewById(R.id.directory_first_name_input);
		last_name = (EditText)findViewById(R.id.directory_last_name_input);
		sciper = (EditText)findViewById(R.id.directory_sciper_input);
		

	}


	@Override
	public void onClick(View v) {
		if( v.getId() == R.id.directory_search_button){
			String last = last_name.getText().toString();
			String first = first_name.getText().toString();
			String sci = sciper.getText().toString();

			search(first, last, sci);
//			resultsList = new ArrayList<Person>();
//			for(String s : COUNTRIES)
//			{
//				resultsList.add(new Person(s));
//			}
	
			
			displayResultList();

		}
	}
	
	@Override
	public void onBackPressed(){
		Toast.makeText(getApplicationContext(), "tu peux pas back, test la petite maison en haut",Toast.LENGTH_SHORT).show();
		switch(state){
			case RESULT_DETAIL:
				displayResultList();				
				break;
				
			case RESULT_LIST:
				displaySearch();
				break;
				
			case SEARCH:
				super.onBackPressed();
				break;
		}
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
//		LinearLayout l1 = (LinearLayout)findViewById(R.id.directory_ll1);
//		LinearLayout l2 = (LinearLayout)findViewById(R.id.directory_ll2);
		LinearLayout l3 = (LinearLayout)findViewById(R.id.directory_ll3);
		
		if(l3.getVisibility() == LinearLayout.INVISIBLE){
			l3.setVisibility(LinearLayout.VISIBLE);
		}else{
			l3.setVisibility(LinearLayout.INVISIBLE);
		}
		return super.onPrepareOptionsMenu(menu);
	}
	
	
	private void search(String first_name, String last_name, String sciper) {
		
		// Create a class for your request with...
		class DirectoryRequest extends ServerRequest {
			
			// ...what to do when the result is ready
			@SuppressWarnings("unchecked")
			@Override
			protected void onPostExecute(String result) {
				if(result != null) {
					resultsList = new ArrayList<Person>();
					
					Gson gson = new Gson();
					
					Type listType = new TypeToken<ArrayList<Person>>()  {}.getType();

					if(listType == null || result == null)
						Toast.makeText(getApplicationContext(), "pouet",Toast.LENGTH_SHORT).show();
					
					resultsList = (ArrayList<Person>)gson.fromJson(result, listType);
					
				} else {
					Toast.makeText(getApplicationContext(), "2 fast 4 U",Toast.LENGTH_SHORT).show();
				}
			}
		}
		
		// Create a RequestParameters object containing the parameters
		RequestParameters reqParams = new RequestParameters();
		reqParams.addParameter("firstName", first_name);
		reqParams.addParameter("lastName", last_name);
		reqParams.addParameter("sciper", sciper);
		
		//TODO add username and pwd via configuration
		reqParams.addParameter("username", "scheiben");
		reqParams.addParameter("password", "<PASSWORD>"); //TODO mettre vos userame pour test
		
		
		
		
		// Use a RequestHandler to execute your request.
		// You don't have to worry about which Servlet your talking to, it will automatically be the
		// one corresponding to the plugin you're in.
		// If you need to do a request from another class/Activity you can give it a RequestHandler instance.
		getRequestHandler().execute(new DirectoryRequest(), reqParams);
		
		
		// To handle the loading another way, you can just use the RequestHandler to give you the complete
		// request URL, including the server and servlet address.
		//System.out.println(getRequestHandler().getRequestUrl(reqParams));
	}
	
	private void displaySearch() {
		setContentView(R.layout.directory_search);
		state = State.SEARCH;
		setupActionBar(true);
	}
	
	public void displayResultList(){
		if(resultsList == null)
			return;

		setContentView(R.layout.directory_result);
		state = State.RESULT_LIST;
		setupActionBar(true);
		
		ListView l = (ListView) findViewById(R.id.directory_result_list);<commit_msg>DirectoryPlugin error messages<commit_after>package org.pocketcampus.plugin.directory;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.pocketcampus.R;
import org.pocketcampus.core.communication.RequestParameters;
import org.pocketcampus.core.communication.ServerRequest;
import org.pocketcampus.core.plugin.PluginBase;
import org.pocketcampus.core.plugin.PluginInfo;
import org.pocketcampus.core.plugin.PluginPreference;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DirectoryPlugin extends PluginBase implements OnClickListener{

	Button search_button;
	EditText first_name;
	EditText last_name;
	EditText sciper;
	
	List<Person> resultsList;
	State state;
	
	private enum State {SEARCH, RESULT_LIST, RESULT_DETAIL};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		displaySearch();
		

		search_button = (Button)findViewById(R.id.directory_search_button);
		search_button.setOnClickListener(this);
		first_name = (EditText)findViewById(R.id.directory_first_name_input);
		last_name = (EditText)findViewById(R.id.directory_last_name_input);
		sciper = (EditText)findViewById(R.id.directory_sciper_input);
		

	}


	@Override
	public void onClick(View v) {
		if( v.getId() == R.id.directory_search_button){
			String last = last_name.getText().toString();
			String first = first_name.getText().toString();
			String sci = sciper.getText().toString();

			search(first, last, sci);
//			resultsList = new ArrayList<Person>();
//			for(String s : COUNTRIES)
//			{
//				resultsList.add(new Person(s));
//			}
	
			
			displayResultList();

		}
	}
	
	@Override
	public void onBackPressed(){
		Toast.makeText(getApplicationContext(), "tu peux pas back, test la petite maison en haut",Toast.LENGTH_SHORT).show();
		switch(state){
			case RESULT_DETAIL:
				displayResultList();				
				break;
				
			case RESULT_LIST:
				displaySearch();
				break;
				
			case SEARCH:
				super.onBackPressed();
				break;
		}
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
//		LinearLayout l1 = (LinearLayout)findViewById(R.id.directory_ll1);
//		LinearLayout l2 = (LinearLayout)findViewById(R.id.directory_ll2);
		LinearLayout l3 = (LinearLayout)findViewById(R.id.directory_ll3);
		
		if(l3.getVisibility() == LinearLayout.INVISIBLE){
			l3.setVisibility(LinearLayout.VISIBLE);
		}else{
			l3.setVisibility(LinearLayout.INVISIBLE);
		}
		return super.onPrepareOptionsMenu(menu);
	}
	
	
	private void search(String first_name, String last_name, String sciper) {
		
		// Create a class for your request with...
		class DirectoryRequest extends ServerRequest {
			
			// ...what to do when the result is ready
			@SuppressWarnings("unchecked")
			@Override
			protected void onPostExecute(String result) {
				if(result != null) {
					resultsList = new ArrayList<Person>();
					
					Gson gson = new Gson();
					
					Type listType = new TypeToken<ArrayList<Person>>()  {}.getType();

					if(listType == null || result == null)
						Toast.makeText(getApplicationContext(), "a",Toast.LENGTH_SHORT).show();
					
					resultsList = (ArrayList<Person>)gson.fromJson(result, listType);
					
				} else {
					Toast.makeText(getApplicationContext(), "too fast?",Toast.LENGTH_SHORT).show();
				}
			}
		}
		
		// Create a RequestParameters object containing the parameters
		RequestParameters reqParams = new RequestParameters();
		reqParams.addParameter("firstName", first_name);
		reqParams.addParameter("lastName", last_name);
		reqParams.addParameter("sciper", sciper);
		
		//TODO add username and pwd via configuration
		reqParams.addParameter("username", "scheiben");
		reqParams.addParameter("password", "b"); //TODO mettre vos userame pour test
		
		
		
		
		// Use a RequestHandler to execute your request.
		// You don't have to worry about which Servlet your talking to, it will automatically be the
		// one corresponding to the plugin you're in.
		// If you need to do a request from another class/Activity you can give it a RequestHandler instance.
		getRequestHandler().execute(new DirectoryRequest(), reqParams);
		
		
		// To handle the loading another way, you can just use the RequestHandler to give you the complete
		// request URL, including the server and servlet address.
		//System.out.println(getRequestHandler().getRequestUrl(reqParams));
	}
	
	private void displaySearch() {
		setContentView(R.layout.directory_search);
		state = State.SEARCH;
		setupActionBar(true);
	}
	
	public void displayResultList(){
		if(resultsList == null)
			return;

		setContentView(R.layout.directory_result);
		state = State.RESULT_LIST;
		setupActionBar(true);
		
		ListView l = (ListView) findViewById(R.id.directory_result_list);
{'fraction_non_alphanumeric': 0.10936752463879054, 'fraction_numerical': 0.001913692469620132, 'mean_word_length': 4.384853168469861, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6818154', 'n_tokens_mistral': 3616, 'n_tokens_neox': 3049, 'n_words': 723}