package net.indivia.hacklabbo.statusreader;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
	private static final String LOG_TAG = "MainActivity";
	private SharedPreferences prefs;
	public final static int 
		MENU_SETTINGS_ID = 10,
		MENU_REFRESH_ID = 11;
	private static ImageButton ibStatusBtn;
	private static TextView 
		tvStatusValue,tvServerValue,tvLastRefreshValue,tvLastStatusValue;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		ibStatusBtn = (ImageButton) findViewById(R.id.status_btn);
		tvStatusValue = (TextView) findViewById(R.id.status_value);
		tvServerValue = (TextView) findViewById(R.id.server_value);
		tvLastRefreshValue = (TextView) findViewById(R.id.last_refresh_value);
		tvLastStatusValue = (TextView) findViewById(R.id.last_status_change_value);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		LoadPreferences();
	}
	
	private void LoadPreferences(){
		//le preferenze relative ai servizi
		Boolean refresh_service =prefs.getBoolean(getString(R.string.key_refresh_service), false);
	    Boolean update_service = prefs.getBoolean("update", false);
	    
	    //setto il display 
	    setDisplay(null,null,null);
		
	    //avvio il service che verifica se ci sono update del software
		if (update_service)
			startService(new Intent(this, CheckUpdate.class));
			
		//avvio il service che verifica se ci sono update dello stato
		//TODO: se il service è già avviato?
		if (refresh_service)
			startService(setUpdateStatusIntent(true));
	}
	
	public Intent setUpdateStatusIntent(Boolean isCicleService){
		Intent intent = new Intent(this, StatusUpdate.class);
		
		String serverName = prefs.getString(getString(R.string.key_server_name), StatusUpdate.DEF_SITE);
		int refresh_time =  Integer.parseInt(prefs.getString(getString(R.string.key_refresh_status_time), "600"));
		
		intent.putExtra("serverName", serverName);
		
		SharedPreferences states = getApplicationContext().getSharedPreferences(getString(R.string.key_states), 0);
		Boolean oldStatus = states.getBoolean(getString(R.string.key_status), false);
		intent.putExtra("oldStatus", oldStatus);
		intent.putExtra("isCicleService", isCicleService);
		if(isCicleService)
			intent.putExtra("refreshTime", refresh_time);
		
		return intent;
	}
	
	public void setDisplay(Boolean status, String last_refresh, String status_changed){
		String serverName = prefs.getString(getString(R.string.key_server_name), StatusUpdate.DEF_SITE);
	    SharedPreferences states = getApplicationContext().getSharedPreferences(getString(R.string.key_states), 0);
	    
	    String mai = getString(R.string.key_status_changed);
	    if(status == null)
	    	status = states.getBoolean(getString(R.string.key_status), false);
	    
	    if(last_refresh == null) 
	    	last_refresh =  states.getString(getString(R.string.key_last_refresh), mai);
	    
	    if(status_changed == null) 
	    	status_changed =  states.getString(getString(R.string.key_status_changed), mai);
	    
	    int statusImg = R.drawable.red_btn,
	    	statusValue = R.string.close;
	    if(status){
	    	statusImg = R.drawable.green_btn;
	    	statusValue = R.string.open;
	    }
	    ibStatusBtn.setImageResource(statusImg);
    	tvStatusValue.setText(statusValue);
	    
	    tvLastRefreshValue.setText(last_refresh);
		tvLastStatusValue.setText(status_changed);
		tvServerValue.setText(serverName);
	}
	
	@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) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			startActivity(new Intent(this, PrefScreen.class));
		} else if( id== R.id.action_refresh){
			startService(setUpdateStatusIntent(false));
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
    protected void onStop() {
      super.onStop();
    }
}