package mh.adib.shiveh;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.ls.LSInput;

import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class Search extends Activity {
	
	ArrayAdapter<String> arr;
	List<String> list;
	List<String> list1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.search);
		list1=new ArrayList<String>();
		list=new ArrayList<String>();
	    final EditText eee=(EditText)findViewById(R.id.editText1);
	    final ListView lv=(ListView)findViewById(R.id.listView1);
	    final ImageView img1=(ImageView)findViewById(R.id.imageView2);
	    ImageView img=(ImageView)findViewById(R.id.imageView1);
	    img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(eee.getText().length()>=3)
				{
				list.clear();
				list1.clear();
			
		
				for(int i=1;i<9;i++)
				{
					int k=0;
					
					switch(i)
					{
					case 1:
						k=3;
						break;
					case 2:
						k=1;
						break;
					case 3:
						k=12;
						break;
					case 4:
						k=1;
						break;
					case 5:
						k=4;
						break;
					case 6:
						k=21;
						break;
					case 7:
						k=11;
						break;
					case 8:
						k=5;
						break;
					}
						for(int j=1;j<=k;j++)
						{
							InputStream is;
							String str="";
							try {
								is = getAssets().open(String.valueOf(i)+"_"+String.valueOf(j)+".html");
								int size = is.available();

								byte[] buffer = new byte[size];
								is.read(buffer);
								is.close();

								str = new String(buffer);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
							Pattern ptr=Pattern.compile(eee.getText().toString());
							Matcher mmm=ptr.matcher(Html.fromHtml(str));
							while(mmm.find())
							{
								if(mmm.start()>60)
								{
									if(mmm.end()+60<Html.fromHtml(str).length())
										list.add("... "+Html.fromHtml(str).subSequence(mmm.start()-60, mmm.end()+60).toString()+" ...");
										//list.add("... "+Html.fromHtml(str).subSequence(mmm.start()-60, mmm.start()).toString()+Html.fromHtml("<font style='color:yellow'>"+eee.getText().toString()+"</font>")+Html.fromHtml(str).subSequence(mmm.end(), mmm.end()+60).toString()+" ...");
									else
										list.add("... "+Html.fromHtml(str).subSequence(mmm.start()-60, Html.fromHtml(str).length()).toString()+" ...");
								}
								else
								{
									if(mmm.end()+60<Html.fromHtml(str).length())
										list.add("... "+Html.fromHtml(str).subSequence(0, mmm.end()+60).toString()+" ...");
									else
										list.add("... "+Html.fromHtml(str).subSequence(0, Html.fromHtml(str).length()).toString()+" ...");
								}
									
								list1.add(String.valueOf(i)+"_"+String.valueOf(j));
								//Toast.makeText(Search.this, String.valueOf(mmm.start()), Toast.LENGTH_LONG).show();
							}
						 
						}
					
				}
				
				arr=new ArrayAdapter<String>(Search.this, android.R.layout.simple_list_item_1, list);
				lv.setAdapter(arr);
				}
				else
				{
					Toast.makeText(Search.this,"لطفا یک لغت بیش از دو حرف برای جستجو بنویسید", Toast.LENGTH_LONG).show();
				}
			}
		});
		
	    
	    
	    lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String[] s=list1.get(arg2).split("_");
				Intent intent=new Intent(Search.this, Show.class);
				intent.putExtra("parent", s[0]);
				intent.putExtra("child", s[1]);
				intent.putExtra("search", "s");
				startActivity(intent);
			}
		});
		
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
		startActivity(new Intent(Search.this, MainActivity.class));
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}
}
