package ir.niknam.daghdagheha;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListe  ner;
import android.widget.TextView;
@SuppressLint("ShowToast")
public class MyAdapter extends ArrayAdapter<String>{
	
	private final Activity context;
	String nam[];
	String matn[];
	int fav[];
	int khande[];
	int targ[];
	int ID[];
	
	public MyAdapter(Activity context,String nam[], String matn[], int fav[], int khande[], int targ[], int ID[]) {	
		super(context, R.layout.item, nam);
		this.context = context;
		this.nam = nam;
		this.matn = matn;
		this.fav = fav;
		this.khande = khande;
		this.targ = targ;
		this.ID = ID;
	}
	@Override
	public boolean isEnabled(int position) {
		return true;
	}
	@SuppressLint({ "ViewHolder", "InflateParams" })
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
	
		LayoutInflater inflater = context.getLayoutInflater();
		View rowView = inflater.inflate(R.layout.item, null, true);
		TextView txtName = (TextView) rowView.findViewById(R.id.txt_name);
		CheckBox favotite = (CheckBox) rowView.findViewById(R.id.favorite_check);
		txtName.setText(nam[position]);
		if(fav[position]==0) {
			favotite.setChecked(false);
		} else if (fav[position]==1) {
			favotite.setChecked(true);
		}
		
		favotite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@SuppressLint("ShowToast")
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				MyDatabase MyDatabase = new MyDatabase(context);
				SQLiteDatabase mydb = MyDatabase.getWritableDatabase();
				if(fav[position] == 1) {
					fav[position] = 0;
					Toast.makeText(getContext(), "fav=0 / favDB:"+fav[position], 1000).show();
				} else {
					fav[position] = 1;
					Toast.makeText(getContext(), "fav=1 / favDB:"+fav[position], 1000).show();
				}
				mydb.execSQL("update [main].[tb_book] set fav = "+fav[position]+" where ID = " + ID[position]);
				Toast.makeText(getContext(), "favSQL / favDB:"+fav[position], 1000).show();
				mydb.close();
			}
		});
			
		return rowView;
} }