package gr.mybook.myweb;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button btn1, btn2;
	ProgressDialog progressDialog;
	Bitmap bitmap=null;
	String text=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
	}
	
	public void getImage(View v) {
		String url1 = "http://icons.iconarchive.com/icons/icons-land/multiple-smiley/48/Auto-Angry-icon.png";
		downloadImage(url1);
	}
	
	public void getText(View v) {
		String url2 = "http://www.enet.gr/rss?i=news.el.categories&c=episthmh--texnologia";
		downloadText(url2);
	}

	private void downloadImage(String urli) {
		progressDialog = ProgressDialog.show(this, "", "Fetching Image...");
        final String url = urli;
         
        new Thread() {
            public void run() {
                InputStream in = null;
                Message msg = Message.obtain();
                msg.what = 1;
                try {
                    in = openHttpConnection(url);
                    bitmap = BitmapFactory.decodeStream(in);
                    Bundle b = new Bundle();
                    b.putParcelable("bitmap", bitmap);
                    msg.setData(b);
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                messageHandler.sendMessage(msg);    
            }
        }.start();
 	}
	
	private void downloadText(String urlt) {
        progressDialog = ProgressDialog.show(this, "", "Fetching Text...");
        final String url = urlt;
         
        new Thread () {
            public void run() {
                int BUFFER_SIZE = 2000;
                InputStream in = null;
                Message msg = Message.obtain();
                msg.what=2;
                try {
                    in = openHttpConnection(url);
                    InputStreamReader isr = new InputStreamReader(in);
                    int charRead;
                    text = "";
                    char[] inputBuffer = new char[BUFFER_SIZE];
                    while ((charRead = isr.read(inputBuffer))>0) {                    
                       //---convert the chars to a String---
                       String readString = String.copyValueOf(inputBuffer, 0, charRead);                    
                       text += readString;
                       inputBuffer = new char[BUFFER_SIZE];
                    }
                    Bundle b = new Bundle();
                    b.putString("text", text);
                    msg.setData(b);
                    in.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                messageHandler.sendMessage(msg);
            }
        }.start();
	}
	
    private InputStream openHttpConnection(String urlString) {
        InputStream in = null;
        int response = -1;
         
        try {
            URL url = new URL(urlString);
            URLConnection conx = url.openConnection();
             
            if (!(conx instanceof HttpURLConnection)) {
                throw new IOException ("URL is not an Http URL");
            }
             
            HttpURLConnection httpConnection = (HttpURLConnection)conx;
            httpConnection.setAllowUserInteraction(false);
            httpConnection.setInstanceFollowRedirects(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.connect(); 
 
            response = httpConnection.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConnection.getInputStream();                                 
            }         
        } catch (IOException e) {
            e.printStackTrace();
        }
        return in;
    }
     
    private Handler messageHandler = new Handler() {
         
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 1:
                ImageView img = (ImageView) findViewById(R.id.imageView1);
                img.setImageBitmap((Bitmap)(msg.getData().getParcelable("bitmap")));
                break;
            case 2:
                TextView text = (TextView) findViewById(R.id.textView1);
                text.setText(msg.getData().getString("text"));
                break;
            }
            progressDialog.dismiss();
        }
    };

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
