package com.cygnusa.hyperm.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.smilerating.SmileyRating;
import com.cygnusa.hyperm.wrappers.FeedbackAnswerArrayModel;
import com.cygnusa.hyperm.wrappers.ProductFeedback;

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

public class ProductFeedbackListAdapter extends RecyclerView.Adapter<ProductFeedbackListAdapter.ViewHolder> implements SmileyRating.OnSmileySelectionListener {
    private ArrayList<ProductFeedback> Questions;
    private Context context;
    String TAG = "Smiley";
    //public static ArrayList<SmileyRatingAnswer> ratingAnswerArrayList;
    private FeedbackAnswerArrayModel answerArrayModels;
    public static List<FeedbackAnswerArrayModel> feedbackAnsArr;


    public ProductFeedbackListAdapter(Context context, ArrayList<ProductFeedback> items) {
        this.context = context;
        this.Questions = items;
        answerArrayModels = new FeedbackAnswerArrayModel();
        feedbackAnsArr = new ArrayList<>();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        ProductFeedback item = Questions.get(position);
        holder.view.setTag(item);
        holder.feedBackQuestionsId.setText(String.format("%s. ", item.getQid()));
        holder.feedBackQuestions.setText(item.getQuestiontext());

        holder.smileRating.setOnSmileySelectionListener(new SmileyRating.OnSmileySelectionListener() {
            @Override
            public void onSmileySelected(int smiley, boolean reselected) {
                answerArrayModels = new FeedbackAnswerArrayModel();
                answerArrayModels.setQid(Questions.get(position).getQid());
                answerArrayModels.setSurveyRating(Integer.valueOf(Questions.get(position).getRating_options().get(smiley).getRating_text()));
                feedbackAnsArr.add(answerArrayModels);
            }
        });
    }

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

    @Override
    public void onSmileySelected(int smiley, boolean reselected) {

    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView feedBackQuestionsId, feedBackQuestions;
        View view;
        SmileyRating smileRating;
        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            smileRating = view.findViewById(R.id.smiley_rating);
            feedBackQuestions = view.findViewById(R.id.feedbackQuestion);
            feedBackQuestionsId = view.findViewById(R.id.feedbackQuestionId);
        }

    }
}
