package com.example.stockprophit;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import java.io.*;

public class SocialActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_social); // create page using activity_social template
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);


	}

	@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_social, 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);
	}

	public void goHome(View view) // go to home screen
	{
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}
		
	public void goFavorites(View view) //go to favorites page
    {
    	Intent intent = new Intent(this, FavoritesActivity.class);
    	startActivity(intent);
    }
    
    public void goTopFive(View view) // go to top 5 page
    {
    	Intent intent = new Intent(this, TopFiveActivity.class);
    	startActivity(intent);
    }
    
    public void goFollowers(View view) // go to followers page
    {
    	Intent intent = new Intent(this, FollowersActivity.class);
    	startActivity(intent);
    }
    
    public void goFollowing(View view) // go to following page
    {
    	Intent intent = new Intent(this, FollowingActivity.class);
    	startActivity(intent);
    }
    
    public void gosocialsearch(View view) // go to the search page
    {
    	Intent intent = new Intent(this, SocialSearch.class);
    	startActivity(intent);
    }
    
    public void gomyprofile(View view) // go to my profile
    {
    	Intent intent = new Intent(this, ProfileActivity.class);
    	startActivity(intent);
    }
    
    public void post_submit(View view) //submit post
    {
    	EditText editText = (EditText)findViewById(R.id.edt_post);
    	String post = editText.getText().toString(); //set string to entry in box
    	
    	editText.setText(""); //reset box
    	
    	//get user name from shared preferences
		SharedPreferences s = getSharedPreferences("prefs", 0);
		String test = s.getString("username", "NOUSERNAME");
    	
    	try {
    		//open file writer
    		FileOutputStream fos = openFileOutput("posts.txt", Context.MODE_APPEND);
    		OutputStreamWriter osw = new OutputStreamWriter(fos);
    		BufferedWriter bf = new BufferedWriter(osw);
    		
    		bf.append(test); 	//append the username
    		bf.append(": "); 	//append the :
    		bf.write(post); 	//append the post
    		bf.newLine(); 		//set to next line
    		
    		bf.close();
    		osw.close();
    		fos.close();
    	} 
    	catch (Exception e) { //catch exception
    		editText.setText(e.getMessage());
    	}
    
    	String textfile = test + ".txt"; //use username to get file name
	    
	    	try {
	    		//open file writer 
	    		FileOutputStream fos = openFileOutput(textfile, Context.MODE_APPEND);
	    		OutputStreamWriter osw = new OutputStreamWriter(fos);
	    		BufferedWriter bf = new BufferedWriter(osw);
	    		
	    		bf.append(test); 	//append the username
	    		bf.append(": ");	//append :
	    		bf.write(post);		//append the post
	    		bf.newLine();		//set to next line
	    		
	    		bf.close();
	    		osw.close();
	    		fos.close();
	    	} 
	    	catch (Exception e) {
	    		editText.setText(e.getMessage());
	    	}
	    
    }
}
