package com.renitha.Activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.renitha.Interface.APIService;
import com.renitha.Model.APIUrl;
import com.renitha.Model.SetterContactUs;
import com.renitha.Pref.Pref;
import com.renitha.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ContactUsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private TextView toolTitle,toolSubTitle,branchEmail,branchPhone,branchAddress,branchEmailText,branchPhoneText;
    private GoogleMap mMap;
    private String mLat="0.0";
    private String mLon="0.0";
    private String contact_no,email_id;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_us);

        branchAddress = (TextView) findViewById(R.id.branchAddress);
        branchPhone = (TextView) findViewById(R.id.branchPhone);
        branchEmail = (TextView) findViewById(R.id.branchEmail);
        branchPhoneText = (TextView) findViewById(R.id.branchPhoneText);
        branchEmailText = (TextView) findViewById(R.id.branchEmailText);
        branchPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact_no));
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ContactUsActivity.this,
                            new String[]{Manifest.permission.CALL_PHONE}, 143);
                } else {
                    startActivity(intent);
                }
            }
        });
        branchPhoneText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact_no));
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ContactUsActivity.this,
                            new String[]{Manifest.permission.CALL_PHONE}, 143);
                } else {
                    startActivity(intent);
                }
            }
        });

        branchEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{email_id});
                i.putExtra(Intent.EXTRA_SUBJECT, "");
                i.putExtra(Intent.EXTRA_TEXT   , "");
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(ContactUsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        branchEmailText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{email_id});
                i.putExtra(Intent.EXTRA_SUBJECT, "");
                i.putExtra(Intent.EXTRA_TEXT   , "");
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(ContactUsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        JSON_DATA_GET_PROFILE();
        SupportMapFragment mapFragment = (SupportMapFragment)
                getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    private void JSON_DATA_GET_PROFILE()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Waiting...");
        progressDialog.show();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService service = retrofit.create(APIService.class);

        Call<SetterContactUs> call = service.getContactUs("1");
        call.enqueue(new Callback<SetterContactUs>()
        {
            @Override
            public void onResponse(Call<SetterContactUs> call, Response<SetterContactUs> response)
            {
                progressDialog.dismiss();
                Log.d("error", response.body().getError().toString());
                if (response.body().getError().equals(true))
                {
                    contact_no  =   response.body().getBranchContact();
                    email_id    =   response.body().getBranchEmail();
                    branchAddress.setText(response.body().getBranchAddress());
                    branchPhone.setText(contact_no);
                    branchEmail.setText(email_id);
                    mLat= response.body().getBranchLat();
                    mLon= response.body().getBranchLong();
                    getMap(mLat,mLon);
                }
            }
            @Override
            public void onFailure(Call<SetterContactUs> call, Throwable t)
            {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;


    }
    private void getMap(String mLat1,String mLon1)
    {
        LatLng mylocation= new LatLng(Double.parseDouble(mLat1), Double.parseDouble(mLon1));
        mMap.addMarker(new MarkerOptions().position(mylocation)).showInfoWindow();
        //   mMap.setMyLocationEnabled(true);
        //   mMap.getUiSettings().setMyLocationButtonEnabled(true);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation,16));
    }
}
