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.res.Resources;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v4.app.NavUtils;

public class FavoritesActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorites);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		Resources res = getResources();
		String[] fav = res.getStringArray(R.array.company_array);
		ArrayList<String> favList = new ArrayList<String>(Arrays.asList(fav));
		int count = favList.size();
		int flag[] = new int[count];
		
		String ret = "";
		try {
			//open file
            InputStream inputStream = openFileInput("faves.txt");
             
            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("myapp", ret);
        this.testRetLength(ret);
        for(int i = 0; i < ret.length(); i++)
        {
        	flag[i] = Integer.parseInt(Character.toString(ret.charAt(i)));
        }
        this.testFlagLength(flag);
		for(int i = favList.size() - 1; i >= 0; i--)
		{
			if(flag[i] == 0)
			{
				favList.remove(i);
			}
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, favList);
		ListView lv = (ListView) findViewById(R.id.fav_list);
		lv.setAdapter(adapter);
	}

	private void testRetLength(String ret) {
		assert ret.length() == 12;
	}

	private void testFlagLength(int[] a) {
		assert a.length == 12;
	}

	@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_favorites, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void goHome(View view) //go to home page
	{
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}
    
    public void goTopFive(View view) //go to top five page
    {
    	Intent intent = new Intent(this, TopFiveActivity.class);
    	startActivity(intent);
    }
    
    public void goSocial(View view) //go to social page
    {
    	GlobalVariables gv = (GlobalVariables)getApplicationContext();
    	if(gv.getStatus() == 4)//get login status
    	{ 	//if logged in then go to social page
    		Intent intent = new Intent(this, SocialActivity.class);
    		startActivity(intent);
    	}
    	else
    	{ 	//if not logged in go to login page
    		gv.setStatus(0);
    		Intent intent = new Intent(this, LoginActivity.class);
    		startActivity(intent);
    	}
    }

}