package com.team.app.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.team.app.R;
import com.team.app.models.Code;

import java.util.List;

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

    Context context;
    List<Object> list;
    ProgressBar progressBar;

    public ContestCodeAdapter(Context context, ProgressBar progressBar2, List<Object> list) {
        this.context = context;
        this.progressBar = progressBar2;
        this.list = list;
    }

    @NonNull
    @Override
    public ContestCodeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ContestCodeViewHolder(LayoutInflater.from(this.context).inflate(R.layout.contest_code, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Code contestCode = (Code) this.list.get(position);
        ContestCodeViewHolder contestCodeViewHolder = (ContestCodeViewHolder) holder;
        contestCodeViewHolder.btn_copy.setOnClickListener(view -> {
            ((ClipboardManager) ContestCodeAdapter.this.context.getSystemService(Context.CLIPBOARD_SERVICE))
                    .setPrimaryClip(ClipData.newPlainText(ContestCodeAdapter.this.context.getString(R.string.app_name), contestCode.getContest_code()));
            Toast.makeText(ContestCodeAdapter.this.context, "Code Copied",Toast.LENGTH_SHORT).show();
        });
        contestCodeViewHolder.app_name.setText(contestCode.getApp_name());
        TextView textView = contestCodeViewHolder.winning_amt;
        textView.setText("₹ " + contestCode.getWinning_amount());
        TextView textView2 = contestCodeViewHolder.entry_fees;
        textView2.setText("₹" + contestCode.getContest_fees());
        contestCodeViewHolder.winners.setText(contestCode.getTotal_winner());
        contestCodeViewHolder.total_teams.setText(contestCode.getTotal_team());
        contestCodeViewHolder.contest_code.setText(contestCode.getContest_code());
        this.progressBar.setVisibility(View.GONE);
    }

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

    public static class ContestCodeViewHolder extends RecyclerView.ViewHolder {
        TextView app_name;
        Button btn_copy;
        TextView contest_code;
        TextView entry_fees;
        TextView total_teams;
        TextView winners;
        TextView winning_amt;

        public ContestCodeViewHolder(View view) {
            super(view);
            this.app_name = view.findViewById(R.id.app_name);
            this.winning_amt = view.findViewById(R.id.winning_amt);
            this.entry_fees = view.findViewById(R.id.entry_fees);
            this.winners = view.findViewById(R.id.winners);
            this.total_teams = view.findViewById(R.id.total_teams);
            this.contest_code = view.findViewById(R.id.contest_code);
            this.btn_copy = view.findViewById(R.id.btn_copy);
        }
    }
}
