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

package com.example.stockprophit;	

import java.io.*;
import java.util.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity 
{
	public final static String EXTRA_MESSAGE = "com.example.stockprophit.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<String> feed = readFeed();
        Collections.reverse(feed);// reverse string so that more recent posts at top
        
        //Use adapter to populate the listview with the string
        ArrayAdapter<String> nfadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, feed);
        ListView listview = (ListView) findViewById(R.id.news_feed);
        listview.setAdapter(nfadapter);
        
        // Get a reference to the AutoCompleteTextView in the layout
    	AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.edit_message);
    	// Get the string array
    	String[] tickers = getResources().getStringArray(R.array.ticker_array);
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tickers);
    	textView.setAdapter(adapter);
        //********************************************
        //Add static variable and link to login page on first create
    }
    
    @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_main, menu);
        return true;
    }
    
    @Override
    public void onBackPressed() //when back is pressed log out
    {
    	Intent intent = new Intent(this, LoginActivity.class);
    	startActivity(intent);
    }
    
    public void sendMessage(View view)
    { 
    	Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText)findViewById(R.id.edit_message);
    	String message = editText.getText().toString();
    	intent.putExtra(EXTRA_MESSAGE, message);
    	startActivity(intent);
    	
    }
    
    public void goFavorites(View view) //go to favorites page
    {
    	Intent intent = new Intent(this, FavoritesActivity.class);
    	startActivity(intent);
    }
    
    public void goSocial(View view) //go to social page
    {
    	GlobalVariables gv = (GlobalVariables)getApplicationContext();
    	if(gv.getStatus() == 4) // check to see if the user is logged in
    	{
    		// if user is logged in then go to social page
    		Intent intent = new Intent(this, SocialActivity.class);
    		startActivity(intent);
    	}
    	else
    	{
    		//if not logged in then go to login page
    		gv.setStatus(0);
    		Intent intent = new Intent(this, LoginActivity.class);
    		startActivity(intent);
    	}
    }
    private List<String> readFeed()
    {
    	String filename = "posts.txt";
    	List<String> abc = new ArrayList<String>();
    	try {
            InputStream inputStream = openFileInput(filename); //open file
             
            if ( inputStream != null ) {
            	//read stings from file
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                
                
                while ( (receiveString = bufferedReader.readLine()) != null ) {
                	abc.add(receiveString);	// save the strings from file to a string variable
                }
                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());
        }
    	return abc;
    }
}
