package com.wsfmn.habittracker;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.maps.model.LatLng;
import com.wsfmn.habit.Geolocation;

import java.io.IOException;
import java.util.List;

public class AddLocationActivity extends AppCompatActivity {

    private Button button;
    private Button B_new;
    private TextView T_address;
    private TextView T_coord;
    private LocationManager locationManager;
    private LocationListener listener;
    private EditText E_address;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_location);

        T_coord = (TextView) findViewById(R.id.T_coordination);
        T_address = (TextView) findViewById(R.id.T_address);
        button = (Button) findViewById(R.id.B_Current);
        B_new = (Button) findViewById(R.id.B_New);
        E_address = (EditText) findViewById(R.id.E_address);



        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                T_coord.setText("");
                T_address.setText("");

                T_coord.append("\n " + location.getLongitude() + " " + location.getLatitude());
                Geocoder geocoder = new Geocoder(AddLocationActivity.this);
                LatLng latLng = new LatLng(location.getLongitude(),location.getLatitude());
                try {
                    List<Address> addressList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                    Address myAddress = addressList.get(0);
                    //set to Geolocation
                    Geolocation geolocation = new Geolocation(myAddress,latLng);
                    T_address.append(myAddress.toString());

                    //Intent  intent = new Intent(AddLocationActivity.this,HabitEventActivity.class);
                    //startActivity(intent);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }
        };

        configure_button();
        newplace_button();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case 10:
                configure_button();
                break;
            default:
                break;
        }
    }

    void newplace_button(){


        B_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //noinspection MissingPermission
                if (ActivityCompat.checkSelfPermission(AddLocationActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(AddLocationActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                String location = E_address.getText().toString();
                List<Address> addressList = null;

                //use Geocoder class here
                Geocoder geocoder = new Geocoder(AddLocationActivity.this);
                try {
                    addressList = geocoder.getFromLocationName(location, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Address myAddress = addressList.get(0);
                LatLng latlng = new LatLng(myAddress.getLatitude(), myAddress.getLongitude());
                T_coord.setText("");
                T_address.setText("");

                T_coord.append("\n"+myAddress.getLatitude()+" "+myAddress.getLongitude());
                T_address.append(myAddress.toString());


                Geolocation geolocation = new Geolocation(myAddress,latlng);


            }
        });


    }

    void configure_button() {
        // first check for permissions
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
                        , 10);
            }
            return;
        }
        // this code won'textView execute IF permissions are not allowed, because in the line above there is return statement.
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //noinspection MissingPermission
                if (ActivityCompat.checkSelfPermission(AddLocationActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(AddLocationActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                locationManager.requestLocationUpdates("gps", 5000, 0, listener);
            }
        });
    }
}

@@ -3290,24 +3290,50 @@
 e_button();%0A
+        confirm_button();%0A
     %7D%0A%0A    @
@@ -3670,32 +3670,360 @@
        %7D%0A    %7D%0A%0A
+    void confirm_button()%7B%0A        button.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A%0A                Intent  intent = new Intent(AddLocationActivity.this,HabitEventActivity.class);%0A                startActivity(intent);%0A%0A            %7D%0A%0A        %7D);%0A    %7D%0A%0A
     void newplac

add confirm button