package org.arkiwi.app.activity;

import io.vov.vitamio.VitamioInstaller;
import io.vov.vitamio.VitamioInstaller.VitamioNotCompatibleException;
import io.vov.vitamio.VitamioInstaller.VitamioNotFoundException;

import org.arkiwi.app.R;
import org.arkiwi.app.interfaces.Define;
import org.arkiwi.app.interfaces.SDialogInterface;
import org.arkiwi.app.service.CheckUpdate;
import org.arkiwi.app.utility.SDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class PrefScreen extends PreferenceActivity implements SDialogInterface, Define {
	private static final String LOG_TAG = "PreferenceScreenActivity";
	private static final String 
		URI_SPEC_TORRENT1 = "org.transdroid.lite",
		URI_SPEC_TORRENT2 = "com.rsa.transgui";
	private static String 
		PREF_KEY_VITAMIO,
		PREF_KEY_TORRENT1,
		PREF_KEY_TORRENT2,
		SPREF_KEY_SERV,
		SPREF_KEY_REFRESH,
		SPREF_KEY_UPDATE;
	private static Context ctx;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        ctx = getApplicationContext();
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        prefs.registerOnSharedPreferenceChangeListener(mListener);
        
    	PREF_KEY_VITAMIO = getString(R.string.key_soft_vitamio);
    	PREF_KEY_TORRENT1 = getString(R.string.key_soft_transdrone);
    	PREF_KEY_TORRENT2 = getString(R.string.key_soft_transgui);
		SPREF_KEY_SERV = getString(R.string.key_refresh_service);
		SPREF_KEY_REFRESH = getString(R.string.key_refresh_time);
		SPREF_KEY_UPDATE = getString(R.string.key_update_service);
    	
        Preference vitamio = findPreference(PREF_KEY_VITAMIO);
        vitamio.setOnPreferenceClickListener(prefClick);
        Preference transdroid = findPreference(PREF_KEY_TORRENT1);
        transdroid.setOnPreferenceClickListener(prefClick);
        Preference trasmissionRemGui = findPreference(PREF_KEY_TORRENT2);
        trasmissionRemGui.setOnPreferenceClickListener(prefClick);
    }
    
    public OnPreferenceClickListener prefClick = new OnPreferenceClickListener() {
		@Override
		public boolean onPreferenceClick(Preference preference) {
			String key = preference.getKey();
			String uri_spec = null;
			
			Log.i(LOG_TAG, "click():"+key);
			if(key.equalsIgnoreCase(PREF_KEY_VITAMIO)) {
				try {
				   VitamioInstaller.checkVitamioInstallation(ctx);
				   Toast.makeText(ctx, R.string.soft_vitamio_already, Toast.LENGTH_LONG).show();
				} catch (VitamioNotCompatibleException e) {
				   showDialog(DIALOG_VITAMIO_COMPATIBLE);				    	
				} catch (VitamioNotFoundException e) {
				   showDialog(DIALOG_VITAMIO_NOTF);
				} 
			} else if(key.equalsIgnoreCase(PREF_KEY_TORRENT1)) {
				uri_spec = URI_SPEC_TORRENT1;
			} else if(key.equalsIgnoreCase(PREF_KEY_TORRENT2)) {
				uri_spec = URI_SPEC_TORRENT2 ;
			}

			if(uri_spec != null){
				Uri marketUri = Uri.parse(URI_GEN_MARKET+uri_spec);
				Intent marketIntent = 
						new Intent(Intent.ACTION_VIEW, marketUri);
				startActivity(marketIntent);	
			}
        	
			return false;
		}
	};
    
    public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {        
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        	boolean preference;
        	Log.d(LOG_TAG, "onSharedPrefCh(): "+key);
        	if (key.equalsIgnoreCase(SPREF_KEY_SERV)){
        		preference = sharedPreferences.getBoolean(SPREF_KEY_SERV, false);
        		if(preference){
        			startService(ATab.feedBurnI);
        		} else {
        			stopService(ATab.feedBurnI);
        		}
        	} else if (key.equalsIgnoreCase(SPREF_KEY_REFRESH)) {
        		stopService(ATab.feedBurnI);
        		startService(ATab.feedBurnI);
        	}
        	
        	preference = sharedPreferences.getBoolean(SPREF_KEY_UPDATE, true);
        	if (preference){
                Intent iUpdate = new Intent(ctx,CheckUpdate.class);
        		startService(iUpdate);
        	}
        }
    };
    
    @Override
	protected Dialog onCreateDialog(int id) {
		return SDialog.vitamioDialog(this, id);
	}
}
