package athary.audio.ency;

import info.guardianproject.database.sqlcipher.SQLiteDatabase;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import athary.audio.ency.R;
import athary.audio.ency.Helpers.data.ClassAdapter;
import athary.audio.ency.Helpers.data.IconContextMenu;
import athary.audio.ency.Helpers.data.NewDataDbAdapter;

public class Class_Activity extends Activity {

//Define Views
private TextView title_txt;
private ListView listView;

//Define Other Variables
private final int CONTEXT_MENU_ID = 1;
private final int MENU_ITEM_1_ACTION = 1;
private final int MENU_ITEM_2_ACTION = 2;
private IconContextMenu iconContextMenu = null;
private NewDataDbAdapter dbHelper; // your db adapter
private int LVL;
private int LEVEL;
private String title;
private Cursor Testcursor;
private ClassAdapter dataAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class_expandables);     

      	initDB();
      	itializecontextmenu();
    	initIntents();
    	initViews();
    	Appearance();
    	displayListView();
    	setListeners();

}

private void initDB() {

    SharedPreferences athary = getSharedPreferences("athary", 0);
    SQLiteDatabase.loadLibs(this);
	NewDataDbAdapter.PATH = athary.getString("path", "");;
	dbHelper = NewDataDbAdapter.getInstance(this);
  	dbHelper.open("Athary412Test412");

}

private void itializecontextmenu(){
	
    Resources res = getResources();
    iconContextMenu = new IconContextMenu(this, CONTEXT_MENU_ID);
    iconContextMenu.addItem(res, "عرض الفهارس", MENU_ITEM_1_ACTION);
    iconContextMenu.addItem(res, "أقسام التصنيف", MENU_ITEM_2_ACTION);

}

private void initIntents() {

	LVL = getIntent().getIntExtra("LVL",0);
	title = getIntent().getStringExtra("title");
	
}

private void initViews() {

	title_txt = (TextView) findViewById(R.id.title_txt);
	listView = (ListView) findViewById(R.id.listView1);
	
}

private void Appearance() {

	title_txt.setTypeface(Typeface.createFromAsset(getAssets(), "font2.ttf"),Typeface.NORMAL);
	title_txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, IntroActivity.fontsize - 1);
	
	title_txt.setText(title);
	
}

private void displayListView() {
	 Cursor cursor = dbHelper.CategoryBy_CATEGORY_PARENT(LVL);
	 dataAdapter = new ClassAdapter(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);
				LEVEL = c.getInt(c.getColumnIndex(NewDataDbAdapter.KEY_CATEGORY_ID));
				title = c.getString(c.getColumnIndex(NewDataDbAdapter.KEY_CATEGORY_NAME));
				
				 Testcursor = dbHelper.CategoryBy_CATEGORY_PARENT(LEVEL);
				showDialog(CONTEXT_MENU_ID);
		         
		 	   }
		 });
		 
		 //set onclick listener for context menu
		 iconContextMenu.setOnClickListener(new IconContextMenu.IconContextMenuOnClickListener() {
		     
		 	
				@Override
				public void onClick(int menuId) {
					
					switch(menuId) {
					case MENU_ITEM_1_ACTION:
						Intent ii = new Intent(getApplicationContext(), Selected_Class_Activity.class);
			 				ii.putExtra("CATID", LEVEL);
			 			startActivity(ii);	
						break;
						
					case MENU_ITEM_2_ACTION:
						if (Testcursor.getCount() == 0){
							Intent iii = new Intent(getApplicationContext(), Selected_Class_Activity.class);
			 					iii.putExtra("CATID", LEVEL);
			 				startActivity(iii);	
						}else{
							Intent i = new Intent(getApplicationContext(), Class_Activity.class);
					 			i.putExtra("LVL", LEVEL);
					 			i.putExtra("title", title);
					 		startActivity(i);
						}
						break;
					
						
					}
				}
			});	
}

@Override
protected Dialog onCreateDialog(int id) {
	if (id == CONTEXT_MENU_ID) {
		return iconContextMenu.createMenu("خيارات التصنيف");
	}
	return super.onCreateDialog(id);
}

@Override
protected void onResume() {
	 super.onResume();
}

@Override
protected void onDestroy() {
	super.onDestroy();
}

}

