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.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.renitha.Interface.APIService;
import com.renitha.Model.APIUrl;
import com.renitha.Model.SetterContactUs;
import com.renitha.R;

import org.w3c.dom.Text;

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

public class LoginTypeActivity extends AppCompatActivity
{
    private TextView btnTechnician,btnCustomer;
    private TextView branchEmail,branchPhone,branchAddress;
    private String contact_no,email_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_type);
        btnTechnician   =   (TextView)findViewById(R.id.btnTechnician);
        btnCustomer     =   (TextView)findViewById(R.id.btnCustomer);
        branchAddress   =   (TextView) findViewById(R.id.branchAddress);
        branchPhone     =   (TextView) findViewById(R.id.branchPhone);
        branchEmail     =   (TextView) findViewById(R.id.branchEmail);

        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(LoginTypeActivity.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(LoginTypeActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnTechnician.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginTypeActivity.this, LoginActivity.class);
                intent.putExtra("LoginType","User");
                overridePendingTransition(R.anim.enter, R.anim.exit);
                startActivity(intent);
            }
        });
        btnCustomer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginTypeActivity.this, LoginActivity.class);
                intent.putExtra("LoginType","Customer");
                overridePendingTransition(R.anim.enter, R.anim.exit);
                startActivity(intent);
            }
        });
        JSON_DATA_GET_PROFILE();
    }
    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);
                }
            }
            @Override
            public void onFailure(Call<SetterContactUs> call, Throwable t)
            {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}
