  // written by:Frank Bohn and Eric Lee
  // tested by:Frank Bohn and Eric Lee
  // debugged by:Frank Bohn and Eric Lee

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.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.support.v4.app.NavUtils;

public class FollowingActivity extends Activity {
	
	public final static String EXTRA_MESSAGE3 = "com.example.stockprophit.MESSAGE3";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_following); //create page with activity_following template
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		
		Resources res = getResources();
		String[] fav = res.getStringArray(R.array.UserNames);
		ArrayList<String> followList = new ArrayList<String>(Arrays.asList(fav));
		int count = followList.size();
		int flag[] = new int[count];
		
		String ret = "";
		SharedPreferences s = getSharedPreferences("prefs", 1);
		String name = s.getString("username", "NONAME");
		String followingfile = name + "following.txt"; //use user name to get file name
	
		try {
			//open file
            InputStream inputStream = openFileInput(followingfile);
             
            if ( inputStream != null ) {
            	//read from the file
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                ret = bufferedReader.readLine(); //set file to string
                inputStream.close();
                bufferedReader.close();
            }
        }
        catch (FileNotFoundException e) {
            Log.e(SocialActivity.class.getName(), "File not found: " + e.toString());
        } 
        catch (IOException e) {
            Log.e(SocialActivity.class.getName(), "Can not read file: " + e.toString());
        }
        ret = ret.replace(",", "").replace("[", "").replace("]", "").replace(" ", ""); //remove symbols and spaces from read in file
        Log.v("myfollowing", ret);
        for(int i = 0; i < ret.length(); i++)
        {
        	flag[i] = Integer.parseInt(Character.toString(ret.charAt(i)));
        }
		for(int i = followList.size() - 1; i >= 0; i--)
		{
			if(flag[i] == 0)
			{
				followList.remove(i);
			}
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, followList);
		final ListView lv = (ListView) findViewById(R.id.follow_list);
		lv.setAdapter(adapter);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
		      public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
		    	  String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));
		    	  sendMessage(selectedFromList);
		      }                 
		});
	}
	

	@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_following, 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 sendMessage(String s)
	    { 
	    	Intent intent = new Intent(this, FriendProfile_Activity.class);
	    	String message3 = s;
	    	intent.putExtra(EXTRA_MESSAGE3, message3);
	    	startActivity(intent);
	    }
}
