package com.example.stockprophit;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class ProfileActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		//get username from shared preferences
		TextView tv = (TextView) findViewById(R.id.name_text);
		SharedPreferences s = getSharedPreferences("prefs", 0);
		String test = s.getString("username", "NOUSERNAME");
		tv.setText(test);
		
		String username = test + ".txt"; //create file name
			List<String> abc = new ArrayList<String>();
	        
	        try {
	        	//open the file
	            InputStream inputStream = openFileInput(username);
	             
	            if ( inputStream != null ) {
	            	//read from the file
	                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
	                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
	                String receiveString = "";
	                
	                
	                while ( (receiveString = bufferedReader.readLine()) != null ) {
	                	abc.add(receiveString);	//store received stings from file
	                }
	                inputStream.close();
	                bufferedReader.close();
	            }
	        }
	        
	        catch (FileNotFoundException e) { //catch exception
	            Log.e(SocialActivity.class.getName(), "File not found: " + e.toString());
	        } 
	        catch (IOException e) { //catch exception
	            Log.e(SocialActivity.class.getName(), "Can not read file: " + e.toString());
	        }
	        
	        Collections.reverse(abc); //reverse the string so most recent posts at top
	        
	        //use adapter to set the posts into the list view
	        ArrayAdapter<String> nfadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, abc);
	        ListView listview = (ListView) findViewById(R.id.user_recentpost);
	        listview.setAdapter(nfadapter);
		}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_profile, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	
	
}
