package org.arkiwi.app.activity;

import org.arkiwi.app.R;
import org.arkiwi.app.feed.FeedItem;
import org.arkiwi.app.feed.FeedItemFile;
import org.arkiwi.app.interfaces.SDialogInterface;
import org.arkiwi.app.rss.RssDoc;
import org.arkiwi.app.rss.RssReader;
import org.arkiwi.app.service.*;
import org.arkiwi.app.utility.IcoCtxMenu;
import org.arkiwi.app.utility.SDialog;
import org.arkiwi.app.utility.SNetwork;

import android.app.Dialog;
import android.app.DownloadManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Messenger;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.Toast;

@SuppressWarnings("unused")
public class ATab extends TabActivity implements SDialogInterface{
	private final static String LOG_TAG = "AATab";
	
	public final static int MENU_SETTING_ID = 0;
	public final static int MENU_DOWNMANAGER_ID = 1;
	public final static int MENU_ABOUT_ID = 2;
	public final static int MENU_CLOSE_ID = 3;
	
	public static TabHost tabHost;
	private static Intent archiveFeedI, viewSearchedI, searchI;
	public static Intent feedBurnI;
	public static Integer max_cache = 0;
	
	private Resources res;
	private Dialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tab_main);
		res = getResources();
		tabHost = getTabHost();
		searchI = new Intent(this,Search.class);
        tabHost.addTab(tabHost.newTabSpec("search").setContent(searchI)
        		.setIndicator(getString(R.string.app_tabSearchLabel),
        					  res.getDrawable(R.drawable.ic_tab_search)));
        
        viewSearchedI = new Intent(this,Viewer.class);
		tabHost.addTab(tabHost.newTabSpec("view").setContent(viewSearchedI)
				.setIndicator(getString(R.string.app_tabViewSearchLabel),
							  res.getDrawable(R.drawable.ic_tab_view)));
		
		archiveFeedI = new Intent(this,Archive.class);
		tabHost.addTab(tabHost.newTabSpec("archiveFeed").setContent(archiveFeedI)
				.setIndicator(getString(R.string.app_tabFeedArchiveLabel),
							  res.getDrawable(R.drawable.ic_tab_feed)));
		
		feedBurnI = new Intent(this,FeedBurnIndeterminato.class);
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		int tabPref = Integer.valueOf(prefs.getString("view_tab", "0")); 
		max_cache = Integer.valueOf(prefs.getString("max_cache", "5"));
		int notify = getIntent().getIntExtra("feedBurn", tabPref);
		//Log.d(LOG_TAG,"n:"+notify);
		
		int tab = tabPref;
		switch (notify) {
			case FeedBurnIndeterminato.NOTIFY_FEEDS_UPDATED:
				tab = 2;
				break;
		}
		tabHost.setCurrentTab(tab);
		
		if (prefs.getBoolean("service", false)){
			startService(feedBurnI);
		}
		
		if (prefs.getBoolean("update", true)){
			Intent checkUpdateI = new Intent(this, CheckUpdate.class);
			startService(checkUpdateI);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (! SNetwork.isConnected((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)))
			showDialog(DIALOG_WIFI);
	}
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	
    	//l'ordine è alto (10,11..) così rimangono in basso rispetto agli 
    	//elementi dei menu specifici per tab
    	Intent i = new Intent(this, PrefScreen.class);
        menu.add(Menu.FIRST, MENU_SETTING_ID, 10, R.string.menu_atab_pref)
        		.setIntent(i).setIcon(R.drawable.ic_menu_settings);

        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO){
        	Intent dmI = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
        	menu.add(Menu.FIRST, MENU_DOWNMANAGER_ID, 11, 
        		R.string.menu_atab_downloadmanager)
        		.setIntent(dmI).setIcon(R.drawable.audio_video);
        }
        
    	// group item order titolo
        menu.add(Menu.FIRST, MENU_ABOUT_ID, 12, R.string.menu_atab_info)
        		.setOnMenuItemClickListener(new OnMenuItemClickListener() {
					@Override
					public boolean onMenuItemClick(MenuItem item) {
						showDialog(DIALOG_INFO);
						return true;
					}
				}).setIcon(R.drawable.ic_menu_about);
        menu.add(Menu.FIRST, MENU_CLOSE_ID, 13, R.string.menu_atab_chiudi)
        		.setOnMenuItemClickListener(new OnMenuItemClickListener() {
					@Override
					public boolean onMenuItemClick(MenuItem item) {
						showDialog(DIALOG_CLOSE);
						return true;
					}
				}).setIcon(R.drawable.ic_menu_close);
  
        return true;
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_WIFI:
				dialog =  SDialog.choseDialog(this, id, R.string.diag_wifi_title, 
							R.drawable.no_network, R.string.diag_wifi_yes, 
							R.string.diag_wifi_no_net, 
							SDialog.setSimpleTV(this, R.string.diag_wifi_msg));
				break;
			case DIALOG_CLOSE:
				dialog = SDialog.choseDialog(this, id, R.string.diag_close_title, 
							R.drawable.ic_menu_close, R.string.diag_close_yesbtn, 
		    				R.string.diag_close_nobtn, 
		    				SDialog.setSimpleTV(this, R.string.diag_close_msg));
				break;
			case DIALOG_INFO:
				dialog = SDialog.neutralDialog(this, id, R.string.diag_info_title, R.drawable.ic_menu_about, 
						SDialog.setHtmlTV(this, R.string.diag_info_msg));
				break;
		}

		return dialog;
    }
}
