package athary.audio.ency;
 
import info.guardianproject.database.sqlcipher.SQLiteDatabase;
import io.vov.utils.StringUtils;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import athary.audio.ency.R;
import athary.audio.ency.Helpers.data.ContentAdapter;
import athary.audio.ency.Helpers.data.NewDataDbAdapter;
 
public class PlaylistActivity extends Activity {

//Define Other Variables
private int CODE;
//private int SEQ;
private String TITLE;
private String URL;
private int OFFSET;
private int DURATION;
private String TXT;
private String TAF;
private int BID;
private int SID;
private ContentAdapter dataAdapter;
private NewDataDbAdapter dbHelper;
private SharedPreferences athary;
private ListView listView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.songs_layout);
  
  	Intializations();
	initViews();
	displayListView();
	setListeners();

 }

private void Intializations() {

	athary = getSharedPreferences("athary", 0); 
    SQLiteDatabase.loadLibs(this);
	NewDataDbAdapter.PATH = athary.getString("path", "");;
  	dbHelper = NewDataDbAdapter.getInstance(this);
    dbHelper.open("Athary412Test412");
    
	CODE = athary.getInt("CODE", 0);         
	TITLE = athary.getString("TITLE", "");
	URL = athary.getString("URL", "");
	SID = athary.getInt("SID", 0);         
	BID = athary.getInt("BID", 0);         

}

private void initViews() {
	   listView = (ListView) findViewById(R.id.listView1);
}

private void displayListView() {
	 Cursor cursor = dbHelper.ContentsBy_CODE(CODE);
	 dataAdapter = new ContentAdapter(this, cursor);
	 listView.setAdapter(dataAdapter);
}

private void setListeners() {
	  	    
	    listView.setOnItemClickListener(new OnItemClickListener() {

	    	@Override
	     	 public void onItemClick(AdapterView<?> listView, View view,
	     	 	     int position, long id) {
	    	
	     		Cursor c = (Cursor) listView.getItemAtPosition (position);

	     		OFFSET = c.getInt(c.getColumnIndex(NewDataDbAdapter.KEY_OFFSET));
	     		DURATION = c.getInt(c.getColumnIndex(NewDataDbAdapter.KEY_DURATION));
	     		TXT = c.getString(c.getColumnIndex(NewDataDbAdapter.KEY_LINE));
	     		TAF = c.getString(c.getColumnIndex(NewDataDbAdapter.KEY_TAFREEG));
	     		//SEQ = c.getInt(c.getColumnIndex(NewDataDbAdapter.KEY_SEQ));

		    	   SharedPreferences athary = getSharedPreferences("athary", 0);
		  		   SharedPreferences.Editor editor = athary.edit();
		  		   
		  		   editor.putInt("CODE", CODE);
		  		   editor.putString("TITLE", TITLE);
		  		   editor.putString("URL", URL);
		  		   editor.putInt("BID", BID);
		  		   editor.putInt("SID", SID);
		  		   editor.putInt("OFFSET", OFFSET);
		  		   editor.putInt("DURATION", DURATION);
		  		   editor.putInt("TYPE", 2);
		  		   editor.putString("TXT", TXT);
		  		   editor.putString("TAF", TAF);

		  		   //Commit the edits!
		  		   editor.commit();
		  		  
		  		 IntroActivity.mytxt = Html.fromHtml(TXT) + "\n\n" + TAF;
		  		 IntroActivity.stime = "التوقيت: " + StringUtils.generateTime(OFFSET);
		  		 IntroActivity.etime = "المدة: " + StringUtils.generateTime(DURATION);
		  		 
		  		 if (IntroActivity.tabHost != null) {
		  			if (!IntroActivity.isplaying) IntroActivity.newmedia = true;
		  			else if (IntroActivity.isplaying) IntroActivity.OFFSET = OFFSET; 

		  			IntroActivity.tabHost.setCurrentTab(0);
		  		 }

	              
	      	   }
	      });
	  }

@Override
protected void onResume() {
  	Intializations();
	initViews();
	displayListView();
	 super.onResume();
}
 
 @Override
 protected void onDestroy() {
 	super.onDestroy();
 }
 
}

	