package com.cygnusa.hyperm.adapter;

import android.content.Context;
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.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.LeaderBoard;

import java.util.ArrayList;

public class TRLeaderBoardAdapter extends RecyclerView.Adapter<TRLeaderBoardAdapter.ViewHolder> {

    private ArrayList<LeaderBoard> LEADER_BOARD_LIST;
    private Context context;

    public TRLeaderBoardAdapter(Context context, ArrayList<LeaderBoard> items) {
        this.LEADER_BOARD_LIST = items;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tr_leader_board_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LeaderBoard LEADER_BOARD = LEADER_BOARD_LIST.get(position);
        holder.view.setTag(LEADER_BOARD);

        holder.name.setText(String.format("%s %s", LEADER_BOARD.getUser().getFname(), LEADER_BOARD.getUser().getLname()));
        if (LEADER_BOARD.getUser().getCity() == null || LEADER_BOARD.getUser().getCity().equalsIgnoreCase(""))
            holder.location.setVisibility(View.GONE);
        else {
            holder.location.setVisibility(View.VISIBLE);
            holder.location.setText(LEADER_BOARD.getUser().getCity());
        }

        if (LEADER_BOARD.getUser().getDealer().getDealer_name() == null || LEADER_BOARD.getUser().getDealer().getDealer_name().equalsIgnoreCase(""))
            holder.dealerName.setVisibility(View.GONE);
        else {
            holder.dealerName.setVisibility(View.VISIBLE);
            holder.dealerName.setText(LEADER_BOARD.getUser().getDealer().getDealer_name());
        }


        if (LEADER_BOARD.getRank() == null || LEADER_BOARD.getRank().equalsIgnoreCase(""))
            holder.position.setVisibility(View.GONE);
        else {
            holder.position.setVisibility(View.VISIBLE);
            holder.position.setText(LEADER_BOARD.getRank());
        }


        if (LEADER_BOARD.getRank().isEmpty()) {
            holder.cup.setVisibility(View.GONE);
            holder.score.setText("");
        } else {
            holder.cup.setVisibility(View.VISIBLE);

            try {
                int d = (int) Math.round(Double.parseDouble(LEADER_BOARD.getPercentage().trim()));
                holder.score.setText(String.valueOf(d).length() > 1 ? d + "%" : d + "%");
            } catch (Exception e) {
                holder.score.setText("-");
            }

        }

        Glide.with(context).load(LEADER_BOARD.getUser().getProfile_photo())
                .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                .into(holder.userImage);
    }

    @Override
    public int getItemCount() {
        return LEADER_BOARD_LIST.size();
    }

    public void add(LeaderBoard item, int position) {
        LEADER_BOARD_LIST.add(position, item);
        notifyItemInserted(position);
    }

    public void add(LeaderBoard item) {
        LEADER_BOARD_LIST.add(item);
        notifyItemInserted(LEADER_BOARD_LIST.size());
    }

    public void remove(int position) {
        LEADER_BOARD_LIST.remove(position);
        notifyItemRemoved(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, location, position, score, dealerName;
        ImageView userImage, cup;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            name = itemView.findViewById(R.id.name);
            location = itemView.findViewById(R.id.location);
            dealerName = itemView.findViewById(R.id.dealer_name);
            score = itemView.findViewById(R.id.score);
            position = itemView.findViewById(R.id.position);
            userImage = itemView.findViewById(R.id.userImage);
            cup = itemView.findViewById(R.id.cup);
        }

    }
}
