package com.app.rewardapp.ui.fragments;

import static com.ads.androidsdk.sdk.util.Tool.isConnected;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.LeaderboardResp;
import com.app.rewardapp.adapters.LeaderboardAdapter;
import com.app.rewardapp.databinding.FragmentLeaderboardBinding;
import com.app.rewardapp.restApi.ApiClient;
import com.app.rewardapp.restApi.ApiInterface;
import com.app.rewardapp.util.Fun;

import java.util.ArrayList;
import java.util.List;

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

public class Leaderboard extends Fragment {
    FragmentLeaderboardBinding bind;
    LeaderboardAdapter adapter;
    List<LeaderboardResp> leaderboardResps;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        bind = FragmentLeaderboardBinding.inflate(getLayoutInflater());

        bind.getRoot().setFocusableInTouchMode(true);
        bind.getRoot().requestFocus();
        bind.getRoot().setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                goback();
                return true;
            }
            return false;
        });

        leaderboardResps = new ArrayList<>();
        bind.rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new LeaderboardAdapter(getActivity(), leaderboardResps);
        bind.rv.setAdapter(adapter);

        if (isConnected(getActivity())) {
            getApi();
        } else {
            Toast.makeText(getActivity(), getString(R.string.no_internet), Toast.LENGTH_SHORT).show();
        }

        return bind.getRoot();
    }


    private void getApi() {
        ApiClient.getClient(getActivity()).create(ApiInterface.class).getLeaderboard().enqueue(new Callback<List<LeaderboardResp>>() {
            @Override
            public void onResponse(Call<List<LeaderboardResp>> call, Response<List<LeaderboardResp>> response) {
                bind.pb.setVisibility(View.GONE);
                if (response.isSuccessful() && response.body().size() != 0) {
                    for(int i=0; i<response.body().size(); i++){
                        if(i<3){
                            try {
                                bind.player1Name.setText(response.body().get(0).getName());
                                bind.player1Coin.setText(response.body().get(0).getCoin());

                                bind.player2Name.setText(response.body().get(1).getName());
                                bind.player2Coin.setText(response.body().get(1).getCoin());

                                bind.player3Name.setText(response.body().get(2).getName());
                                bind.player3Coin.setText(response.body().get(2).getCoin());
                            }catch (Exception e){}
                        }else{
                            leaderboardResps.add(response.body().get(i));
                        }
                    }

                    adapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onFailure(Call<List<LeaderboardResp>> call, Throwable t) {

            }
        });
    }

    private void goback() {
        FragmentMain NameofFragment = new FragmentMain();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.container, NameofFragment);
        transaction.commit();
    }

}