package com.hesiii.i_hear.Summarization;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Toast;

import com.hesiii.i_hear.API.ApiServices;
import com.hesiii.i_hear.API.RetroServer;
import com.hesiii.i_hear.ResponseModels.SummarizationResponse;
import com.hesiii.i_hear.databinding.ActivitySummarizationBinding;

import java.util.ArrayList;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SummarizationActivity extends AppCompatActivity {

    private ActivitySummarizationBinding binding;
    private static final int SPEECH_REQUEST_CODE = 100;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySummarizationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (getSupportActionBar() != null) {

            getSupportActionBar().setTitle("Summarization");

        }

        binding.recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                recordButton();

            }
        });

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please Wait...");
        progressDialog.setCancelable(false);

    }

    private void recordButton() {

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Just Speak...");

        try {

            startActivityForResult(intent, SPEECH_REQUEST_CODE);

        } catch (Exception e) {

            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SPEECH_REQUEST_CODE) {

            if (resultCode == RESULT_OK && data != null) {

                progressDialog.show();
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                getResultFromAPI(result.get(0));

            }

        }

    }

    private void getResultFromAPI(String input) {

        progressDialog.show();

        ApiServices apiServices = RetroServer.getRetrofitInstance().create(ApiServices.class);
        Call<SummarizationResponse>call = apiServices.getSummarization(input);
        call.enqueue(new Callback<SummarizationResponse>() {
            @Override
            public void onResponse(@NonNull Call<SummarizationResponse> call, @NonNull Response<SummarizationResponse> response) {

                if (response.isSuccessful()) {

                    if (response.body() != null) {

                        if (response.body().getSummerized() != null && !response.body().getSummerized().isEmpty()) {

                            System.out.println("Summerized : "+response.body().getSummerized());
                            binding.predictionText.setText(response.body().getSummerized());

                        } else {

                            Toast.makeText(SummarizationActivity.this, "Something Went Wrong!", Toast.LENGTH_SHORT).show();

                        }

                    } else {

                        Toast.makeText(SummarizationActivity.this, "Something Went Wrong!", Toast.LENGTH_SHORT).show();

                    }

                } else {

                    Toast.makeText(SummarizationActivity.this, "Error - "+response.code(), Toast.LENGTH_SHORT).show();

                }

                progressDialog.dismiss();

            }

            @Override
            public void onFailure(@NonNull Call<SummarizationResponse> call, @NonNull Throwable t) {

                progressDialog.dismiss();
                Toast.makeText(SummarizationActivity.this, "Error - "+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }

}