package com.example.micha.pocasie;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    HashMap<String, Integer> images = new HashMap<String, Integer>();


    JSONObject data = null;
    TextView tv;
    int[] textsIds = new int[] {R.id.textView00, R.id.textView01, R.id.textView02, R.id.textView10, R.id.textView11, R.id.textView12, R.id.textView20, R.id.textView21, R.id.textView22, R.id.textView30, R.id.textView31, R.id.textView32, R.id.textView40, R.id.textView41, R.id.textView42, R.id.textView50, R.id.textView51, R.id.textView52, R.id.textView60, R.id.textView61, R.id.textView62, R.id.textView70, R.id.textView71, R.id.textView72, R.id.textView80, R.id.textView81, R.id.textView82, R.id.textView90, R.id.textView91, R.id.textView92, R.id.textView100, R.id.textView101, R.id.textView102, R.id.textView110, R.id.textView111, R.id.textView112, R.id.textView120, R.id.textView121, R.id.textView122, R.id.textView130, R.id.textView131, R.id.textView132, R.id.textView140, R.id.textView141, R.id.textView142, R.id.textView150, R.id.textView151, R.id.textView152, R.id.textView160, R.id.textView161, R.id.textView162, R.id.textView170, R.id.textView171, R.id.textView172, R.id.textView180, R.id.textView181, R.id.textView182, R.id.textView190, R.id.textView191, R.id.textView192, R.id.textView200, R.id.textView201, R.id.textView202, R.id.textView210, R.id.textView211, R.id.textView212, R.id.textView220, R.id.textView221, R.id.textView222, R.id.textView230, R.id.textView231, R.id.textView232, R.id.textView240, R.id.textView241, R.id.textView242, R.id.textView250, R.id.textView251, R.id.textView252, R.id.textView260, R.id.textView261, R.id.textView262, R.id.textView270, R.id.textView271, R.id.textView272, R.id.textView280, R.id.textView281, R.id.textView282, R.id.textView290, R.id.textView291, R.id.textView292, R.id.textView300, R.id.textView301, R.id.textView302, R.id.textView310, R.id.textView311, R.id.textView312, R.id.textView320, R.id.textView321, R.id.textView322, R.id.textView330, R.id.textView331, R.id.textView332, R.id.textView340, R.id.textView341, R.id.textView342, R.id.textView350, R.id.textView351, R.id.textView352, R.id.textView360, R.id.textView361, R.id.textView362, R.id.textView370, R.id.textView371, R.id.textView372, R.id.textView380, R.id.textView381, R.id.textView382, R.id.textView390, R.id.textView391, R.id.textView392};
    int[] imagesIds = new int[] {R.id.imageView0, R.id.imageView1, R.id.imageView2, R.id.imageView3, R.id.imageView4, R.id.imageView5, R.id.imageView6, R.id.imageView7, R.id.imageView8, R.id.imageView9, R.id.imageView10, R.id.imageView11, R.id.imageView12, R.id.imageView13, R.id.imageView14, R.id.imageView15, R.id.imageView16, R.id.imageView17, R.id.imageView18, R.id.imageView19, R.id.imageView20, R.id.imageView21, R.id.imageView22, R.id.imageView23, R.id.imageView24, R.id.imageView25, R.id.imageView26, R.id.imageView27, R.id.imageView28, R.id.imageView29, R.id.imageView30, R.id.imageView31, R.id.imageView32, R.id.imageView33, R.id.imageView34, R.id.imageView35, R.id.imageView36, R.id.imageView37, R.id.imageView38, R.id.imageView39};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        images.put( "01d", Integer.valueOf( R.drawable.a01d ) );
        images.put( "01n", Integer.valueOf( R.drawable.a01n ) );
        images.put( "02d", Integer.valueOf( R.drawable.a02d ) );
        images.put( "02n", Integer.valueOf( R.drawable.a02n ) );
        images.put( "03d", Integer.valueOf( R.drawable.a03d ) );
        images.put( "03n", Integer.valueOf( R.drawable.a03n ) );
        images.put( "04d", Integer.valueOf( R.drawable.a04d ) );
        images.put( "04n", Integer.valueOf( R.drawable.a04n ) );
        images.put( "09d", Integer.valueOf( R.drawable.a09d ) );
        images.put( "09n", Integer.valueOf( R.drawable.a09n ) );
        images.put( "10d", Integer.valueOf( R.drawable.a10d ) );
        images.put( "10n", Integer.valueOf( R.drawable.a10n ) );
        images.put( "11d", Integer.valueOf( R.drawable.a11d ) );
        images.put( "11n", Integer.valueOf( R.drawable.a11n ) );
        images.put( "13d", Integer.valueOf( R.drawable.a13d ) );
        images.put( "13n", Integer.valueOf( R.drawable.a13n ) );
        images.put( "50d", Integer.valueOf( R.drawable.a50d ) );
        images.put( "50n", Integer.valueOf( R.drawable.a50n ) );

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getJSON("Bratislava");

    }



    @SuppressLint("StaticFieldLeak")
    public void getJSON(final String city) {
        //http://api.openweathermap.org/data/2.5/forecast?q=Bratislava&APPID=b92e32028d240db22b21a564df95bde9
        //http://api.openweathermap.org/data/2.5/forecast?q=Bratislava&units=metric&APPID=b92e32028d240db22b21a564df95bde9

        new AsyncTask<String, Integer, String>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(String... params) {
                try {
                    URL url = new URL("http://api.openweathermap.org/data/2.5/forecast?q="+city+"&units=metric&APPID=b92e32028d240db22b21a564df95bde9");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    BufferedReader reader =
                            new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuffer json = new StringBuffer(1024);
                    String tmp = "";
                    while((tmp = reader.readLine()) != null)
                        json.append(tmp).append("\n");
                    reader.close();
                    data = new JSONObject(json.toString());

                    if(data.getInt("cod") != 200) {
                        System.out.println("Cancelled");
                        return null;
                    }
                } catch (Exception e) {
                    System.out.println("Exception "+ e.getMessage());
                    return null;
                }
                return null;
            }

            @Override
            protected void onPostExecute(String Void) {
                if(data!=null) {
                    try {
                        JSONObject mesto = new JSONObject(data.getString("city"));
                        JSONArray list = new JSONArray(data.getString("list"));
                        for (int i =0; i<41;i++){
                            JSONObject predpoved = list.getJSONObject(i);
                            JSONObject main = predpoved.getJSONObject("main");
                            JSONObject oblaky = predpoved.getJSONObject("clouds");
                            JSONObject vietor = predpoved.getJSONObject("wind");
                            JSONArray pocasie = new JSONArray(predpoved.getString("weather"));
                            String ikona = pocasie.getJSONObject(0).getString("icon");
                            tv = (TextView) findViewById(textsIds[i*3]);
                            tv.setText(mesto.getString("name")+", "+predpoved.getString("dt_txt").substring(11,16)+" "+
                                            predpoved.getString("dt_txt").substring(8,10)+"."+
                                            predpoved.getString("dt_txt").substring(5,7)+". "+
                                    ", Description: "+pocasie.getJSONObject(0).getString("description"));
                            tv = (TextView) findViewById(textsIds[i*3+1]);
                            tv.setText("Humidity: "+main.getString("humidity")+"%  Clouds: "+oblaky.getString("all")+"%  Wind: "+vietor.getString("speed")+"m/s");
                            tv = (TextView) findViewById(textsIds[i*3+2]);
                            tv.setText(main.getString("temp")+" °C");
                            ImageView img= (ImageView) findViewById(imagesIds[i]);
                            img.setImageResource(images.get( ikona ).intValue());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.execute();

    }
}
