package com.sliit.smartserviceassistant;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

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

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

public class chat_bot extends AppCompatActivity {

    private RecyclerView chatsRV;
    private ImageButton sendMsgFAB;
    private EditText userMsgEdt;
    private final String USER_KEY = "user";
    private final String BOT_KEY = "bot";
    private TextToSpeech mTTS;
    // creating a variable for array list and adapter class.
    private ArrayList<ChatsModal>chatsModalArrayList;
    private ChatRVAdapter chatRVAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_bot);
        mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTTS.setLanguage(Locale.GERMAN);

                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    } else {
                        mTTS.setPitch(1.0f);
                        mTTS.setSpeechRate(1.0f);
                        mTTS.speak("what is your name", TextToSpeech.QUEUE_FLUSH, null);

                    }
                } else {
                    Log.e("TTS", "Initialization failed");
                }
            }
        });///texttospeechpart


        chatsRV=findViewById(R.id.idRVChats);
        sendMsgFAB = findViewById(R.id.idFABSend);
        userMsgEdt = findViewById(R.id.idEdtMessage);
        chatsModalArrayList= new ArrayList<>();
        chatRVAdapter =new ChatRVAdapter(chatsModalArrayList,this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        chatsRV.setLayoutManager(manager);
        chatsRV.setAdapter(chatRVAdapter);



        sendMsgFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(userMsgEdt.getText().toString().isEmpty()){
                    Toast.makeText(chat_bot.this,"please enter your message",Toast.LENGTH_SHORT).show();
                    return;

                }

             postData13(userMsgEdt.getText().toString());
                userMsgEdt.setText("");

            }
        });
    }

//    private void getResponse(String message){
//        chatsModalArrayList.add(new ChatsModal(message,USER_KEY));
//
//        chatRVAdapter.notifyDataSetChanged();
//        String url="http://api.brainshop.ai/get?bid=157392&key=eJjQmMRimytCUA7E&uid=[uid]&msg="+message;
//        String BASE_URL ="http://api.brainshop.ai/";
//        Retrofit retrofit3 = new Retrofit.Builder()
//                .baseUrl(BASE_URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        RetrofitAPI3 retrofitAPI3=retrofit3.create(RetrofitAPI3.class);
//        Call<MsgModal> call3 =retrofitAPI3.getMessage(url);
//
//        call3.enqueue(new Callback<MsgModal>() {
//            @Override
//            public void onResponse(Call<MsgModal> call3, Response<MsgModal> response) {
//                if(response.isSuccessful()){
//                    System.out.println("done");
//                    MsgModal modal =response.body();
//                    System.out.println(modal.getCnt());
//                    chatsModalArrayList.add(new ChatsModal(modal.getCnt(),BOT_KEY));
//                    chatRVAdapter.notifyDataSetChanged();
//
//
//                }
//            }
//
//            @Override
//            public void onFailure(Call<MsgModal> call, Throwable t) {
//
//            }
//        });
//
//
//    }
private void postData13(String data){
        chatsModalArrayList.add(new ChatsModal(data,USER_KEY));
        chatRVAdapter.notifyDataSetChanged();

    Retrofit retrofit3 = new Retrofit.Builder()
            .baseUrl("http://192.168.1.5:8000/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    RetrofitAPI3 retrofitAPI3 = retrofit3.create(RetrofitAPI3.class);

    DataModal3 modal3 = new DataModal3(data);

    Call<DataModal3> call3 = retrofitAPI3.createPost3(modal3);
    call3.enqueue(new Callback<DataModal3>() {

        @Override
        public void onResponse(Call<DataModal3> call3, Response<DataModal3> response3) {
           // Toast.makeText(chat_bot.this, "Data added to API", Toast.LENGTH_SHORT).show();
            System.out.println(response3.body().getResponse());

            mTTS = new TextToSpeech(getBaseContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status == TextToSpeech.SUCCESS) {
                        int result = mTTS.setLanguage(Locale.GERMAN);

                        if (result == TextToSpeech.LANG_MISSING_DATA
                                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                            Log.e("TTS", "Language not supported");
                        } else {
                            mTTS.setPitch(1.0f);
                            mTTS.setSpeechRate(1.0f);
                            mTTS.speak(response3.body().getResponse(), TextToSpeech.QUEUE_FLUSH, null);

                        }
                    } else {
                        Log.e("TTS", "Initialization failed");
                    }
                }
            });///texttospeechpart



            chatsModalArrayList.add(new ChatsModal(response3.body().getResponse(),BOT_KEY));
                    chatRVAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFailure(Call<DataModal3> call3, Throwable t) {

            Toast.makeText(chat_bot.this, "Data can't added to API", Toast.LENGTH_SHORT).show();
            System.out.println(t.getMessage());

        }
    });

}
}