package com.kaviz.heal.Activities;

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

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.kaviz.heal.API.ApiRequestData;
import com.kaviz.heal.API.RetroServer;
import com.kaviz.heal.Model.ResponseModelTwo;
import com.kaviz.heal.R;
import com.kaviz.heal.databinding.ActivityHealthTipsBinding;

import java.util.Arrays;

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

public class HealthTipsActivity extends AppCompatActivity {

    ActivityHealthTipsBinding binding;

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

        if (getSupportActionBar() != null) {

            getSupportActionBar().setTitle(getResources().getString(R.string.health_tips));

        }

        getHealthTips();

    }

    private void getHealthTips() {

        binding.progressBar.setVisibility(View.VISIBLE);

        ApiRequestData apiRequestData = RetroServer.connectRetrofit().create(ApiRequestData.class);
        Call<ResponseModelTwo>call = apiRequestData.getHealthTips();
        call.enqueue(new Callback<ResponseModelTwo>() {
            @Override
            public void onResponse(@NonNull Call<ResponseModelTwo> call, @NonNull Response<ResponseModelTwo> response) {

                if (response.isSuccessful()) {

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

                        if (response.body().getStatus().equals("success")) {
                            
                            showHealthTips(response.body().getExercisePlan(), response.body().getDietPlan());
                            
                        }

                    }

                }

            }

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

                Toast.makeText(HealthTipsActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                binding.progressBar.setVisibility(View.GONE);

            }
        });

    }

    private void showHealthTips(String exercisePlan, String dietPlan) {

        binding.exercisePlanText.setText(exercisePlan);
        binding.dietPlanText.setText(dietPlan);
        binding.progressBar.setVisibility(View.GONE);

    }
}