package com.paulbsd;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
<<<<<<< HEAD:paulbsdmyip/src/com/paulbsd/main.java
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
=======
import android.widget.Toolbar;
>>>>>>> 64e9f08dbcd4adcd0791cbe73337bd2b3822ecce:app/src/main/java/com/paulbsd/myip/MainActivity.java
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    String url = "https://ipinfo.io/json";

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final TextView public_ip = (TextView) findViewById(R.id.PublicIP);
        final TextView hostname = (TextView) findViewById(R.id.Hostname);
        final TextView city = (TextView) findViewById(R.id.City);
        final TextView org = (TextView) findViewById(R.id.Org);

        Button but = (Button) findViewById(R.id.but);

<<<<<<< HEAD:paulbsdmyip/src/com/paulbsd/main.java
        this.updateIpInfo(publicip, hostname, city, org);
=======
        this.updateIpInfo(public_ip,hostname,city,org);
>>>>>>> 64e9f08dbcd4adcd0791cbe73337bd2b3822ecce:app/src/main/java/com/paulbsd/myip/MainActivity.java

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
<<<<<<< HEAD:paulbsdmyip/src/com/paulbsd/main.java
                updateIpInfo(publicip, hostname, city, org);
=======
                updateIpInfo(public_ip,hostname,city,org);
>>>>>>> 64e9f08dbcd4adcd0791cbe73337bd2b3822ecce:app/src/main/java/com/paulbsd/myip/MainActivity.java
            }
        });
    }

    private void setSupportActionBar(Toolbar toolbar) {
    }

    public void updateIpInfo(TextView public_ip, TextView hostname, TextView city, TextView org) {
        try {
            getIpInfo ipinfo = new getIpInfo(this.url);
            public_ip.setText(ipinfo.getIp());
            hostname.setText(ipinfo.getHostname());
            city.setText(ipinfo.getCity());
            org.setText(ipinfo.getOrg());
        } catch (Exception e) {

        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}