package athary.audio.ency.Helpers.search;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.BaseAdapter;
import athary.audio.ency.IntroActivity;

import java.util.List;

class CustomAdapterView extends LinearLayout {        
	public CustomAdapterView(Context context, RowData row_data) 
	{
		super( context );		
		setId(row_data.getID());    
		//container is a horizontal layer

		
		//image:params
		
		setOrientation(LinearLayout.HORIZONTAL);
		setPadding(0, 6, 0, 6);
		//ImageView ivLogo = new ImageView(context);
		//ivLogo.setImageDrawable(context.getResources().getDrawable(R.drawable.indicator));	
		//addView(ivLogo, Params);	
		
		//vertical layer for text
		LinearLayout.LayoutParams Paramstxt = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		LinearLayout PanelV = new LinearLayout(context);
		PanelV.setOrientation(LinearLayout.VERTICAL);
		PanelV.setGravity(Gravity.BOTTOM);

		TextView textAddress = new TextView( context );
		textAddress.setTextSize(TypedValue.COMPLEX_UNIT_SP, IntroActivity.fontsize);
		textAddress.setTextColor(Color.argb(255, 134, 121, 81));
		textAddress.setGravity(Gravity.RIGHT);
		textAddress.setMaxLines(4);
		textAddress.setTypeface(IntroActivity.tf, Typeface.NORMAL);

		if (row_data.getsearchterm().equals(""))
			textAddress.setText(row_data.getTXT());
		else 
		{
			String[] newString = row_data.getsearchterm().split(" ");
			for(String replacement: newString) {
				
				if (!replacement.equals("")) row_data.setTXT(row_data.getTXT().replaceAll(replacement,"<font color='#ff723a'>" + replacement + "</font>"));
			}
			textAddress.setText(Html.fromHtml(row_data.getTXT()));
			textAddress.setEllipsize(TruncateAt.END);
			}
		
		PanelV.addView(textAddress);   
			 
		TextView textName = new TextView( context );
		textName.setTextSize(TypedValue.COMPLEX_UNIT_SP, IntroActivity.fontsize - 2);
		textName.setTextColor(Color.argb(255, 185, 171, 127));
		textName.setGravity(Gravity.RIGHT);
		textName.setTypeface(IntroActivity.tf, Typeface.NORMAL);
		textName.setText( row_data.getSHEEKH() + " (" + row_data.getBOOKNAME() + ") ");
		PanelV.addView(textName);
		
		addView(PanelV, Paramstxt);
		


	}
}


public class CustomAdapter extends BaseAdapter /*implements OnClickListener*/ {
	
    private Context context;
    private List<RowData> row_dataList;

    public CustomAdapter(Context context, List<RowData> row_dataList ) { 
        this.context = context;
        this.row_dataList = row_dataList;
    }

    public int getCount() {                        
        return row_dataList.size();
    }

    public Object getItem(int position) {     
        return row_dataList.get(position);
    }

    public long getItemId(int position) {  
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    	RowData row_data = row_dataList.get(position);
        View v = new CustomAdapterView(this.context, row_data );
        
        if (row_data.getFAV() == 1) v.setBackgroundColor(Color.WHITE);
        //v.setBackgroundColor((position % 2) == 1 ? Color.GRAY : Color.WHITE);

        return v;
    }


}
