package com.cygnusa.hyperm.fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.wrappers.TrainerQuestion;

import java.util.ArrayList;

public class TrainingTrainerFeedback extends Fragment {

    private TextView noDataFound, hintText, txtScore;
    private RecyclerView rcvTrainingFeedbackList;
    TrainingTrainerAdapter adapter;
    Activity activity;
    int scrollX = 0;
    HorizontalScrollView headerScroll;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dse_training_feedback, container, false);
        initViews(view);
        return view;


    }

    private void initViews(View view) {
        activity = getActivity();
        noDataFound = view.findViewById(R.id.noDataFound);
        hintText = view.findViewById(R.id.hintText);
        txtScore = view.findViewById(R.id.score);

        hintText.setText("Over all Trainer Feedback score");

        rcvTrainingFeedbackList = view.findViewById(R.id.rcvSummaryList);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcvTrainingFeedbackList.setLayoutManager(manager);

        if (GlobalData.TrainerFeedbackData != null) {
            noDataFound.setVisibility(View.GONE);
            rcvTrainingFeedbackList.setVisibility(View.VISIBLE);
            ArrayList<TrainerQuestion> questions = new ArrayList<>();
            if (GlobalData.TrainerFeedbackData.getTrainerQuestions() != null)
                for (int k = 0; k < GlobalData.TrainerFeedbackData.getTrainerQuestions().size(); k++) {
                    if (GlobalData.TrainerFeedbackData.getTrainerQuestions().get(k).getQuestiontype().equalsIgnoreCase("2")) {
                        questions.add(GlobalData.TrainerFeedbackData.getTrainerQuestions().get(k));
                    }
                }
            txtScore.setText(GlobalData.TrainerFeedbackData.getTrainerFeedbackScoreAvg());
            adapter = new TrainingTrainerAdapter(questions, getActivity());
            rcvTrainingFeedbackList.setAdapter(adapter);
        } else {
            noDataFound.setVisibility(View.VISIBLE);
            rcvTrainingFeedbackList.setVisibility(View.GONE);

        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView question;
        public TextView score;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            question = itemView.findViewById(R.id.questions);
            score = itemView.findViewById(R.id.score);
        }
    }


    class TrainingTrainerAdapter extends RecyclerView.Adapter<TrainingTrainerFeedback.ViewHolder> {
        ArrayList<TrainerQuestion> questionData;
        private Context activity;

        public TrainingTrainerAdapter(ArrayList<TrainerQuestion> summaryData, FragmentActivity activity) {
            questionData = summaryData;
            this.activity = activity;
        }

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


        @Override
        public void onBindViewHolder(@NonNull TrainingTrainerFeedback.ViewHolder holder, int position) {
            if (questionData.get(position).getQuestiontype().equalsIgnoreCase("2")) {
                holder.question.setText(questionData.get(position).getQuestiontext());
                String value = String.valueOf(Math.round(Double.parseDouble(questionData.get(position).getQuestionAns())));
                holder.score.setText(value);
            }

        }

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