package com.lathia.profilemanager.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lathia.profilemanager.ProfileManager;
import com.lathia.profilemanager.data.Distribution;

public abstract class DistributionActivity extends AbstractProfileActivity
{
	public static final String DISTRIBUTION_TITLE = "distribution_title";
	public static final String DISTRIBUTION_DATA = "distribution_data";
	public static final String DISTRIBUTION_VARIABLE = "distribution_variable";
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		Intent intent = getIntent();
		String title = intent.getStringExtra(DISTRIBUTION_TITLE);
		if (title != null)
		{
			getScreenTitle().setText(title);
		}
		loadData(intent);
	}
	
	protected void loadData(final Intent intent)
	{
		new AsyncTask<Void, Void, Distribution>()
		{
			@Override
			protected void onPreExecute()
			{
				super.onPreExecute();
				showLoadingInto(getLoadingProgressBar(), getListView(), true);
			}

			@Override
			protected Distribution doInBackground(Void... params)
			{
				Distribution distribution = null;
				if (intent.hasCategory(DISTRIBUTION_DATA))
				{
					distribution = intent.getParcelableExtra(DISTRIBUTION_DATA);
				}
				else
				{
					ProfileManager profileManager = ProfileManager.getInstance(DistributionActivity.this);
					String variableName = intent.getStringExtra(DISTRIBUTION_VARIABLE);
					if (variableName != null)
					{
						distribution = profileManager.getDistribution(variableName);
					}
				}
				return distribution;
			}
			
			@Override
			protected void onPostExecute(Distribution distribution)
			{
				super.onPostExecute(distribution);
				if (distribution != null)
				{
					getListView().setAdapter(getAdapter(distribution));
				}
				getNoDataView().setVisibility(distribution == null ? View.VISIBLE : View.GONE);
				showLoadingInto(getLoadingProgressBar(), getListView(), false);
			}
			
		}.execute();
	}
	
	protected abstract DistributionListAdapter getAdapter(Distribution distribution);
	
	protected abstract TextView getScreenTitle();
	
	protected abstract View getNoDataView();
}

@@ -573,18 +573,17 @@
 iable%22;%0A
-%09
 %0A
+
 %09@Overri
@@ -944,26 +944,25 @@
 intent);%0A%09%7D%0A
-%09
 %0A
+
 %09protected v
@@ -1762,20 +1762,17 @@
 n;%0A%09%09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%09@Over
@@ -1982,16 +1982,30 @@
 %09%09%7D%0A%09%09%09%09
+View noData = 
 getNoDat
@@ -2007,24 +2007,67 @@
 NoDataView()
+;%0A%09%09%09%09if (noData != null)%0A%09%09%09%09%7B%0A%09%09%09%09%09noData
 .setVisibili
@@ -2115,24 +2115,30 @@
 View.GONE);%0A
+%09%09%09%09%7D%0A
 %09%09%09%09showLoad
@@ -2202,12 +2202,9 @@
 %09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%7D.
@@ -2217,18 +2217,17 @@
 e();%0A%09%7D%0A
-%09
 %0A
+
 %09protect
@@ -2301,18 +2301,17 @@
 ution);%0A
-%09
 %0A
+
 %09protect
@@ -2349,17 +2349,16 @@
 itle();%0A
-%09
 %0A%09protec

fix for null no data