package app.headphone.myapplication;

import android.graphics.Typeface;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class MainActivity extends ActionBarActivity {

    TextView text,views,favs,text2;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);
        views=(TextView)findViewById(R.id.views);
        favs = (TextView)findViewById(R.id.favs);
        text2=(TextView)findViewById(R.id.text2);
        button= (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DownloadTextTask()
                        .execute(
                                "http://www.instructables.com/id/Arduino-Simple-POV-Display/");
            }
        });
        Typeface font = Typeface.createFromAsset(getAssets(),"PrintClearly.otf");
        text.setTypeface(font);
        views.setTypeface(font);
        text2.setTypeface(font);
        favs.setTypeface(font);
    }


    public static InputStream OpenHttpGETConnection(String url) {
        InputStream inputStream = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
            inputStream = httpResponse.getEntity().getContent();
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
        return inputStream;
    }

    private String DownloadText(String URL) {
        int BUFFER_SIZE = 2000;
        InputStream in = null;
        try {
            in = OpenHttpGETConnection(URL);
        } catch (Exception e) {
            Log.d("DownloadText", e.getLocalizedMessage());
            return "";
        }
        InputStreamReader isr = new InputStreamReader(in);
        int charRead;
        String str = "";
        char[] inputBuffer = new char[BUFFER_SIZE];
        try {
            while ((charRead = isr.read(inputBuffer)) > 0) {
// ---convert the chars to a String---
                String readString = String
                        .copyValueOf(inputBuffer, 0, charRead);
                str += readString;
                inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        } catch (IOException e) {
            Log.d("DownloadText", e.getLocalizedMessage());
            return "";
        }
        return str;
    }int length;
    String viewsstr = "";
    String favsstr= "";

    private class DownloadTextTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            return DownloadText(urls[0]);
        }
        @Override
        protected void onPostExecute(String result) {
            Log.d("DownloadTextTask", result);
            //length = result.length();

            // length = result.indexOf("<!-- H");
            viewsstr = result.substring(53810, 53816);
            favsstr= result.substring(53967, 53970);
            text.setText(viewsstr);
            text2.setText(favsstr);

            if(viewsstr != text.getText()) {
            }
            else {}
        }
    }
}



