package com.renitha.Activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.renitha.Interface.APIService;
import com.renitha.Model.APIUrl;
import com.renitha.Model.SetterLogin;
import com.renitha.Pref.Pref;
import com.renitha.R;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity
{
    private EditText editTextUsername,editTextPassword;
    private TextView btnLogin;
    String LoginType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editTextUsername    =   (EditText)findViewById(R.id.editTextUsername);
        editTextPassword    =   (EditText)findViewById(R.id.editTextPassword);

        Bundle extras       =    getIntent().getExtras();
        LoginType           =    extras.getString("LoginType");

        btnLogin            =   (TextView)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editTextUsername.getText().toString().equals("")) {
                    showAlert("Please enter Mobile No ");
                } else if (editTextPassword.getText().toString().equals("")) {
                    showAlert("Please enter password ");
                }
                else {
                    userSignIn();
                }
            }
        });
    }

    private void userSignIn()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Wait...");
        progressDialog.show();
        String username     =   editTextUsername.getText().toString().trim();
        String password     =   editTextPassword.getText().toString().trim();
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        // add your other interceptors …
        // add logging as last interceptor
        httpClient.addInterceptor(logging);  // <-- this is the important line


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

        APIService service = retrofit.create(APIService.class);
        Call<SetterLogin> call = service.userLogin(username,password,LoginType);
        call.enqueue(new Callback<SetterLogin>()
        {
            @Override
            public void onResponse(Call<SetterLogin> call, Response<SetterLogin> response)
            {
                progressDialog.dismiss();
                if (response.body().getError().equals(true))
                {
                    finish();
                    Pref.getInstance(getApplicationContext()).setIsLogin(true);
                    Pref.getInstance(getApplicationContext()).setLoginId(response.body().getLogin_id());
                    Pref.getInstance(getApplicationContext()).setLoginType(response.body().getLogin_type());
                    //  Toast.makeText(getApplicationContext(), response.body().getMessage(), Toast.LENGTH_LONG).show();
                    if(LoginType.equals("Customer"))
                    {
                        startActivity(new Intent(getApplicationContext(), ViewCustomerComplaints.class));
                    }
                    else if(LoginType.equals("User"))
                    {
                        startActivity(new Intent(getApplicationContext(), ViewUserComplaints.class));
                    }
                    overridePendingTransition(R.anim.enter, R.anim.exit);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<SetterLogin> call, Throwable t)
            {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(),"Invalid username or password", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void showAlert(String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(Html.fromHtml("<font color='#8472BB'>"+message+"!</font>")).setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
        Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
        pbutton.setTextColor(Color.BLACK);
    }
}
