package fishrungames.kobserver;

import java.util.ArrayList;
import java.util.Arrays;

import fishrungames.kobserver.JniWrapper;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;


public class ProfileActivity extends Activity
{
	
	ArrayAdapter<String> spinnerAdapter;
	
	@Override
	protected void onCreate(Bundle icicle)
	{

		super.onCreate(icicle);

		setContentView(R.layout.profile_layout);	
		
		Button backbtn = (Button) findViewById(R.id.backButton_profile);
		backbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), MainMenuActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });
		
		
		EditText lastNameEdit = (EditText) findViewById(R.id.lastNameEdit);
    	
    	EditText firstNameEdit = (EditText) findViewById(R.id.firstNameEdit);
    	
    	EditText middleNameEdit = (EditText) findViewById(R.id.middleNameEdit);
    	
    	EditText contactNumberEdit = (EditText) findViewById(R.id.contactNumberEdit);
    	
    	EditText uikNumberEdit = (EditText) findViewById(R.id.uikNumberEdit);
    	
    	lastNameEdit.setText(JniWrapper.LastName);
    	firstNameEdit.setText(JniWrapper.FirstName);
    	middleNameEdit.setText(JniWrapper.MiddleName);
    	contactNumberEdit.setText(JniWrapper.ContactNumber);
    	uikNumberEdit.setText(Integer.toString(JniWrapper.UikNumber));


    	
    	if (JniWrapper.MapState >= 2)
    	{
    		CheckBox alarmCheckBox = (CheckBox) findViewById(R.id.alarmCheckBox);
    		alarmCheckBox.setChecked(true);
        	
    		
    	}
		

		Spinner statusSpinner = (Spinner) findViewById(R.id.statusSpinner);
		
		ArrayList<String> strings = 
		        new ArrayList<String>(Arrays.asList("Наблюдатель", "Мобильная группа"));
		
		spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, strings);

		statusSpinner.setAdapter(spinnerAdapter);
		
		statusSpinner.setSelection(JniWrapper.Status);
		
		
    	
		Button updateUserinfoButton = (Button) findViewById(R.id.updateUserinfoButton);
		updateUserinfoButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //Intent myIntent = new Intent(view.getContext(), MainMenuActivity.class);
                //startActivityForResult(myIntent, 0);
            	
            	//LocationManager m = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            	
            	EditText lastNameEdit = (EditText) findViewById(R.id.lastNameEdit);
            	
            	EditText firstNameEdit = (EditText) findViewById(R.id.firstNameEdit);
            	
            	EditText middleNameEdit = (EditText) findViewById(R.id.middleNameEdit);
            	
            	EditText contactNumberEdit = (EditText) findViewById(R.id.contactNumberEdit);
            	
            	EditText uikNumberEdit = (EditText) findViewById(R.id.uikNumberEdit);
            	
            	Spinner statusSpinner = (Spinner) findViewById(R.id.statusSpinner);
            	
            	int mapState;
            	
            	int status = statusSpinner.getSelectedItemPosition();
            	
            	if (status == 1)
            	{
            		mapState = 0;
            	}
            	else
            	{
            		CheckBox alarmCheckBox = (CheckBox) findViewById(R.id.alarmCheckBox);
                	
            		if (alarmCheckBox.isChecked())
            		{
            			mapState = 2;
            		}
            		else
            		{
            			mapState = 1;
            		}
            	}
            	
            	JniWrapper.LastName = lastNameEdit.getText().toString();
            	JniWrapper.FirstName = firstNameEdit.getText().toString();
            	JniWrapper.MiddleName = middleNameEdit.getText().toString();
            	JniWrapper.ContactNumber = contactNumberEdit.getText().toString();
            	JniWrapper.MapState = mapState;
            	
            	JniWrapper.UikNumber = Integer.parseInt(uikNumberEdit.getText().toString());
            	JniWrapper.Status = status;
            	JniWrapper.PosX = 30.215800f;
            	JniWrapper.PosY = 60.005200f;
            	
            	
            	JniWrapper.UpdateUserInfo(JniWrapper.LastName,
            			JniWrapper.FirstName,
            			JniWrapper.MiddleName,
            			JniWrapper.ContactNumber,
            			JniWrapper.MapState,
            			JniWrapper.UikNumber,
            			JniWrapper.Status,
            			JniWrapper.PosX,
            			JniWrapper.PosY);
            }
        });

	}
}