package br.com.codetec.saudesabor.ui.login.ui.dashboard;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import br.com.codetec.saudesabor.R;
import br.com.codetec.saudesabor.model.ResponseDashboard;
import br.com.codetec.saudesabor.service.ServiceConector;
import br.com.codetec.saudesabor.ui.login.CadastrarUsuarioActivity;
import br.com.codetec.saudesabor.ui.login.DashboardActivity;
import br.com.codetec.saudesabor.ui.login.LoginActivity;
import br.com.codetec.saudesabor.utils.UtilsActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;

    private TextView txtPontosAcumulados;
    private TextView txtRanking;
    private TextView txtNome;
    private ImageView fotoUsuario;

    public static ResponseDashboard responseDashboard;

    View root;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                ViewModelProviders.of(this).get(DashboardViewModel.class);
        root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        dashboardViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
            }
        });
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        txtPontosAcumulados = root.findViewById(R.id.txtPontosAcumulados);
        txtRanking = root.findViewById(R.id.txtRanking);
        txtNome = root.findViewById(R.id.txtNome);
        fotoUsuario = root.findViewById(R.id.fotoUsuario);

        fotoUsuario.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editarUsuario();
            }
        });

        txtNome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editarUsuario();
            }
        });

        //Carregando imagem
        if(LoginActivity.responseLogin.getUsuario().getPhoto()!=null) {
            try {
                byte[] decodedString = Base64.decode(LoginActivity.responseLogin.getUsuario().getPhoto(), Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                fotoUsuario.setImageBitmap(decodedByte);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        Call<ResponseDashboard> call = ServiceConector.getService(DashboardFragment.this.getActivity()).dashboard(LoginActivity.responseLogin.getToken());
        call.enqueue(new Callback<ResponseDashboard>() {
            @Override
            public void onResponse(Call<ResponseDashboard> call, Response<ResponseDashboard> response) {
                if(response.body()!=null){
                    responseDashboard = response.body();
                    txtPontosAcumulados.setText("Pontos: " + response.body().getPts_acumulados());
                    txtRanking.setText("Ranking: " + response.body().getPos_ranking() + " de " + response.body().getParticipantes());
                    txtNome.setText(LoginActivity.responseLogin.getUsuario().getFirstname() + " " + LoginActivity.responseLogin.getUsuario().getLastname());
                }
            }

            @Override
            public void onFailure(Call<ResponseDashboard> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void editarUsuario() {
        CadastrarUsuarioActivity.EDIT_MODE = true;
        UtilsActivity.getInstance().startActivity(DashboardFragment.this.getActivity(), CadastrarUsuarioActivity.class);
    }
}