package com.tli.bee;

public class CoverflowExample extends Activity  implements Runnable{
	  private ProgressDialog mSpinner;
	  Thread thread ;
	   CoverFlow coverFlow;
	   Bitmap    bitmap;
	  	public ArrayList<Bitmap> al_bitmap =new ArrayList<Bitmap>();
	  	public Bitmap orgImg;
	String []strArray ;
    ImageAdapter coverImageAdapter;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.showall);
		
		  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,				
				           WindowManager.LayoutParams.FLAG_FULLSCREEN);
		  

			coverFlow =  (CoverFlow) findViewById(R.id.coverflow);
			
			
			 mSpinner = new ProgressDialog(this);
		      mSpinner.setMessage("Loading offers please wait..");
			offer_imgurls=	ms1.getOfferimgurl();
		        

			 try {
		        	
					mSpinner.show();
					thread= new Thread(CoverflowExample.this);
					thread.start();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				coverFlow.setAdapter(new ImageAdapter(CoverflowExample.this));
			     coverImageAdapter =  new ImageAdapter(CoverflowExample.this);


     coverFlow.setOnItemClickListener(new OnItemClickListener() {        
      	public void onItemClick(AdapterView<?> parent, View v,  final int position, long id) {            
			
			System.out.println(" b in on click"+ al_bitmap.get(position));
			System.out.println("onClick COVERFLOW   "+offDetail.get(position));
				
					}
	});
     
    }
	
	}

    public void run() {

	
			System.out.println("offers "+offers);
		ArrayList<String> img_urls = new ArrayList<String>();
			getBitmap(img_urls);
		 
			messageHandler.sendMessage(msg);
	}
    
    public void getBitmap( ArrayList str)
	{
    	InputStream is =null;
		try {
			for(int x =0;x<str.size();x++)
			{
			URL myFileUrl =new URL(str.get(x));
			HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			 is = conn.getInputStream();
				 System.gc();
			 BitmapFactory.Options options = new BitmapFactory.Options();
			 options.inTempStorage = new byte[16*1024];
			orgImg = BitmapFactory.decodeStream(is);
			al_bitmap.add(orgImg);
			}
			
		} catch (IOException e) {
			
			
			e.printStackTrace();
			
		}
		
		
		finally{
			if(is!=null)
			{
				try {
					is.close();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
   			private Handler messageHandler = new Handler() {
				
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					switch (msg.what) {
					case 1:
						System.out.println(al_bitmap.size());
						if(al_bitmap.size()==0)
						{
							Toast.makeText(CoverflowExample.this, "Please wait..", Toast.LENGTH_LONG).show();
						}
						else{
							coverFlow.setAdapter(coverImageAdapter);
						
					     coverFlow.setSpacing(-10);
					     coverFlow.setSelection(0, true);
					     coverFlow.setAnimationDuration(5000);
						}

						break;
				
					}
					mSpinner.dismiss();
				}
			};    
    		   
    
 public class ImageAdapter extends BaseAdapter {
     int mGalleryItemBackground;
     private Context mContext;
     private FileInputStream fis;
     private ImageView[] mImages;
     public ImageAdapter(Context c) {
      mContext = c;
      mImages = new ImageView[al_bitmap.size()];
     }
     public int getCount() {
    	 System.out.println("bitmap size "+al_bitmap.size());
         return al_bitmap.size();
     }

     public Object getItem(int position) {
         return position;
     }

     public long getItemId(int position) {
         return position;
     }

     public View getView(int position, View convertView, ViewGroup parent) {

      //Use this code if you want to load from resources
    	 
    	 ImageView img;
    	 if (convertView == null) {          
				img = new ImageView(mContext);                                 
				} 
    	 LayoutInflater inflater = getLayoutInflater();
			View view = inflater.inflate(R.layout.coverflow, parent, false);
			img = (ImageView) view.findViewById(R.id.img);
			
			System.out.println(" albitmap "+"position   "+position+" "+al_bitmap.get(position));
			if(al_bitmap.get(position) ==null)
			{
				img.setImageResource(R.drawable.bee);
		         img.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
		         TextView title = (TextView) view.findViewById(R.id.title);
					title.setText(titles.get(position));
					TextView descrition = (TextView) view.findViewById(R.id.description);
					descrition.setText(subtitle.get(position));
		         BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
		         drawable.setAntiAlias(true);
			}
			else
			{
		   img.setImageBitmap(al_bitmap.get(position));
            img.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
			TextView title = (TextView) view.findViewById(R.id.title);
			title.setText(titles.get(position));
			TextView descrition = (TextView) view.findViewById(R.id.description);
			descrition.setText(subtitle.get(position));
         BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
         drawable.setAntiAlias(true);
			}
			
         return view;   
     }

      public float getScale(boolean focused, int offset) { 
        /* Formula: 1 / (2 ^ offset) */ 
          return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
      } 

 }
 
/* @Override
 protected void onDestroy() {
     super.onDestroy();

    for(int i =0;i<al_bitmap.size();i++)
    {
    	if(al_bitmap.get(i)!=null)
    	{
    		al_bitmap.get(i).recycle();
    	}
    }

 }*/
}