package ca.ualberta.cs.cmput301f14t14.questionapp;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import ca.ualberta.cs.cmput301f14t14.questionapp.data.DataManager;
import ca.ualberta.cs.cmput301f14t14.questionapp.model.Question;
import ca.ualberta.cs.cmput301f14t14.questionapp.view.AddQuestionDialogFragment;
import ca.ualberta.cs.cmput301f14t14.questionapp.view.QuestionListAdapter;
import ca.ualberta.cs.cmput301f14t14.questionapp.view.SearchQueryDialogFragment;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
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.Toast;

public class MainActivity extends Activity {

	private DataManager dataManager;
	private QuestionListAdapter qla = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       	setContentView(R.layout.activity_main);
       
        //Create a spinner adapter for sorting choices
        ArrayAdapter<CharSequence> sortAdapter = 
        		ArrayAdapter.createFromResource(
        					getActionBar().getThemedContext(),
        					R.array.sort_spinner_data, 
        					android.R.layout.simple_spinner_item
        				);
        // Specify the layout to use when the list of choices appears
        sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getActionBar().setListNavigationCallbacks(sortAdapter, changeSort());
                
        dataManager = DataManager.getInstance(this);
        
        // if first time logging in, prompt user to set username
        if(dataManager.getUsername() == null){

        	Intent intent = new Intent(this.getBaseContext(), WelcomeScreenActivity.class);
        	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        	startActivity(intent);
        }
        
        //create the list of questions
        List<Question> qList = dataManager.load();
        qla = new QuestionListAdapter(this, R.layout.list_question, qList);  
        ListView questionView = (ListView) findViewById(R.id.question_list);
        questionView.setAdapter(qla);
        questionView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// run off to the question view if you tap an item
				final Question question = qla.getItem(position);
				UUID qId = question.getId();
				Intent intent = new Intent(getApplicationContext(), QuestionActivity.class);
				intent.putExtra("QUESTION_UUID", qId.toString());
				startActivity(intent);
			}
		});
    }
    
    public OnNavigationListener changeSort() {
    	//This is the callback that is called when the user chooses 
    	//a sorting order from the spinner in the action bar.
    	return new OnNavigationListener() {
			@Override
			public boolean onNavigationItemSelected(int itemposition, long itemid) {
				// change way of sorting based on way selected
				List<Question> sortedList = MainActivity.sortList(itemposition, dataManager);
				qla.clear();
				qla.addAll(sortedList);
				qla.update();
				return true;
				
			}
		};
    }


    protected static List<Question> sortList(int itemposition, final DataManager dm) {
		// sort the list based on way selected
    	List<Question> qlist = dm.load();

		switch (itemposition){
		case 1:{
			// Sort by date
			Collections.sort(qlist, new Comparator<Question>(){
			
				@Override
				public int compare(Question q1, Question q2) {
					
					return q2.getDate().compareTo(q1.getDate());
				}
			
			
			});
			break;
		}	
		case 2:{
			// Sort by most upvoted
			Collections.sort(qlist, new Comparator<Question>(){

				@Override
				public int compare(Question q1, Question q2) {
					
					return q2.getUpvotes() - q1.getUpvotes();
				}
								
				
				});
			break;
			}
		case 3:{
			final List<UUID> favQ = dm.getFavoritedQuestions();
			for (UUID q : favQ){
				qlist.add(dm.getQuestion(q));
			}
			Collections.sort(qlist,new Comparator<Question>(){

				@Override
				public int compare(Question arg0, Question arg1) {
					if(favQ.contains(arg0.getId()) && favQ.contains(arg1.getId())){
						return 0;
					}
					else if(favQ.contains(arg0.getId()) && !favQ.contains(arg1.getId())){
						return -1;
					}
					return 1;
				}
				
			});
			break;
		}
		case 4:{
			// Sort by current user posts
			Collections.sort(qlist, new Comparator<Question>(){

				@Override
				public int compare(Question q1, Question q2) {
					if(q1.getAuthor().equals(dm.getUsername()) && q2.getAuthor().equals(dm.getUsername())){
						return 0;
					}
					else if(q1.getAuthor().equals(dm.getUsername()) && !q2.getAuthor().equals(dm.getUsername()))
						return -1;
					else{
						return 1;
					}
				}
				
			});
			break;
		}
		}
		return qlist;
    }
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        
        switch (id) {
	        case R.id.action_question_list_search:
	        	FragmentManager fm = getFragmentManager();
	        	SearchQueryDialogFragment sQ = new SearchQueryDialogFragment();
	        	sQ.show(fm, "search");
	        	break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void addQuestion(View view){
    	// open dialog to create a question
    	FragmentManager fm = getFragmentManager();
    	AddQuestionDialogFragment aQ = new AddQuestionDialogFragment();
    	aQ.show(fm, "addquestiondialogfragmentlayout");
    }
    
    public void updateList(Question q) {
    	// update the list
    	qla.add(q);
    	qla.update();
    	Toast.makeText(getApplicationContext(), "Question successfully added", Toast.LENGTH_LONG).show();
    }
    
    public void searchQuestions(String q) {
    	Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
		intent.putExtra("QUERY_STRING", q);
		startActivity(intent);
    }
}

@@ -4622,32 +4622,114 @@
 ()))%7B%0A%09%09%09%09%09%09
-return 0
+//Sort by date if both in favourites%0A%09%09%09%09%09%09return arg0.getDate().compareTo(arg1.getDate())
 ;%0A%09%09%09%09%09%7D%0A%09%09%09

Change sort by favourites to sort when same