package com.cygnusa.hyperm.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.google.gson.Gson;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.TrainerViewFeedbackActivity;
import com.cygnusa.hyperm.pickimage.util.FixedGridLayoutManager;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.wrappers.DSEFeedbackResponse;
import com.cygnusa.hyperm.wrappers.ErrorResponse;

import java.util.List;
import java.util.Objects;

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

public class TrainerFeedBackFragment extends Fragment {

    HorizontalScrollView headerScroll;
    RecyclerView rcvDynamicView;
    TextView txtNoDataFound;
    int scrollX = 0;
    ApiService restService;
    public TrainerFeedbackAdapter feedbackAdapter;

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

        rcvDynamicView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                scrollX += dx;

                headerScroll.scrollTo(scrollX, 0);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });

        getDseFeedback();

        return view;
    }

    private void getDseFeedback() {
        restService.getTrainingFeedback().enqueue(new Callback<DSEFeedbackResponse>() {
            @Override
            public void onResponse(Call<DSEFeedbackResponse> call, Response<DSEFeedbackResponse> response) {
                if (response.body() != null) {
                    if (response.body().getMessage().equalsIgnoreCase("Success")) {
                        if (response.body().getFeedBackData() != null && response.body().getFeedBackData().size() > 0) {
                            txtNoDataFound.setVisibility(View.GONE);
                            rcvDynamicView.setVisibility(View.VISIBLE);
                            GlobalData.DSEFEEDBACKRESPONSEGROUPIMAGE = response.body().getFeedBackData().get(0).getTraining_group_image();
                            feedbackAdapter = new TrainerFeedbackAdapter(response.body().getFeedBackData());
                            rcvDynamicView.setAdapter(feedbackAdapter);
                        } else {
                            txtNoDataFound.setVisibility(View.VISIBLE);
                            rcvDynamicView.setVisibility(View.GONE);
                        }
                    } else {
                        GlobalData.toast(getActivity(), response.body().getMessage());
                    }
                } else if (response.body() != null && response.body().getStatus() == 401) {
                    GlobalData.canLogout = true;
                    Objects.requireNonNull(getActivity()).finish();
                } else if (response.errorBody() != null) {
                    Gson gson = new Gson();
                    ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                    GlobalData.showToast(getActivity(), message.getMessage(), Toast.LENGTH_SHORT).show();
                    if (message.getStatus() == 401) {
                        GlobalData.canLogout = true;
                        getActivity().finish();
                    }
                }
            }

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

            }
        });

    }

    private void setUpRecyclerView() {

        FixedGridLayoutManager manager = new FixedGridLayoutManager();
        manager.setTotalColumnCount(1);
        rcvDynamicView.setLayoutManager(manager);
    }

    private void initView(View view) {
        headerScroll = view.findViewById(R.id.headerScroll);
        rcvDynamicView = view.findViewById(R.id.rcvDynamicView);
        txtNoDataFound = view.findViewById(R.id.txtNoDataFound);
        txtNoDataFound.setVisibility(View.GONE);
        restService = ((MainApplication) getActivity().getApplication()).getClient();

    }

    private class TrainerFeedbackAdapter extends RecyclerView.Adapter<TrainerFeedBackFragment.TrainereedbackViewHolder> {
        List<DSEFeedbackResponse.DSEFeedbackData> feedBackData;

        public TrainerFeedbackAdapter(List<DSEFeedbackResponse.DSEFeedbackData> data) {
            feedBackData = data;
        }

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

        @Override
        public void onBindViewHolder(@NonNull TrainerFeedBackFragment.TrainereedbackViewHolder holder, int position) {
            holder.overall_feedback.setText(feedBackData.get(position).getOverallFeedback());
            holder.trainer_feedback.setText(feedBackData.get(position).getTrainerFeedback());
            holder.training_feedback.setText(feedBackData.get(position).getTrainingFeedback());
            holder.training_title.setText(feedBackData.get(position).getTitle());
            holder.training_id.setText(feedBackData.get(position).getTraining_id());

            holder.trining_dates.setText(feedBackData.get(position).getStartDate() + " - " + feedBackData.get(position).getEndDate());
            holder.noOfParticipants.setText(feedBackData.get(position).getTotal_participants());

            holder.btnCertification.setTag(position);
            holder.btnCertification.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GlobalData.DSEFEEDBACKVIEW = feedBackData.get((Integer) v.getTag());
                    startActivity(new Intent(getActivity(), TrainerViewFeedbackActivity.class));
                }
            });
        }

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

    private static class TrainereedbackViewHolder extends RecyclerView.ViewHolder {
        TextView training_title, trining_dates, noOfParticipants, training_feedback, trainer_feedback, overall_feedback, training_id;
        Button btnCertification;

        public TrainereedbackViewHolder(@NonNull View itemView) {
            super(itemView);

            training_title = itemView.findViewById(R.id.training_title);
            trining_dates = itemView.findViewById(R.id.trining_dates);
            noOfParticipants = itemView.findViewById(R.id.noOfParticipants);
            training_feedback = itemView.findViewById(R.id.training_feedback);
            trainer_feedback = itemView.findViewById(R.id.trainer_feedback);
            overall_feedback = itemView.findViewById(R.id.overall_feedback);
            training_id = itemView.findViewById(R.id.training_id);

            btnCertification = itemView.findViewById(R.id.btnCertification);

        }
    }
}