package com.cygnusa.hyperm.fragments;

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.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.ErrorResponse;
import com.cygnusa.hyperm.wrappers.GetAllTrainerResultAndScore;
import com.cygnusa.hyperm.wrappers.TrainerResultAndFeedback;

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

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

public class TrainerResultScoreFragment extends Fragment {

    HorizontalScrollView headerScroll;
    RecyclerView rcvDynamicView;
    TextView txtNoDataFound;
    int scrollX = 0;
    public TrainingScoreAdapter scoreAdapter;
    ApiService restService;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trainer_result_score_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);
            }
        });
        return view;
    }

    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();


        getDseFeedbackResultScore();
    }

    private void getDseFeedbackResultScore() {
        restService.trainerFeedbackResultAndScore().enqueue(new Callback<GetAllTrainerResultAndScore>() {
            @Override
            public void onResponse(Call<GetAllTrainerResultAndScore> call, Response<GetAllTrainerResultAndScore> response) {
                if (response.body() != null) {
                    if (response.body().getMessage().equalsIgnoreCase("Success")) {
                        if (response.body().getTrainerResultAndFeedbackList() != null && response.body().getTrainerResultAndFeedbackList().size() > 0) {
                            scoreAdapter = new TrainingScoreAdapter(response.body().getTrainerResultAndFeedbackList());
                            rcvDynamicView.setAdapter(scoreAdapter);
                        } else {
                            rcvDynamicView.setVisibility(View.GONE);
                            txtNoDataFound.setVisibility(View.VISIBLE);
                        }
                    } else {
                        GlobalData.toast(getActivity(), response.body().getMessage());
                        rcvDynamicView.setVisibility(View.GONE);
                        txtNoDataFound.setVisibility(View.VISIBLE);
                    }
                } 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<GetAllTrainerResultAndScore> call, Throwable t) {

            }
        });
    }


    private static class TrainingScoreAdapter extends RecyclerView.Adapter<TrainerResultScoreFragment.TrainingScoreViewHolder> {

        List<TrainerResultAndFeedback> resultAndScore;

        public TrainingScoreAdapter(List<TrainerResultAndFeedback> dseResultAndScoreList) {
            resultAndScore = dseResultAndScoreList;
        }


        @Override
        public void onBindViewHolder(@NonNull TrainerResultScoreFragment.TrainingScoreViewHolder holder, int position) {

            holder.dseName.setText(resultAndScore.get(position).getOrganizerName());
            holder.am.setText(resultAndScore.get(position).getVenueAm());
            holder.dealerName.setText(resultAndScore.get(position).getVenueDealer());
            holder.training_dates.setText(resultAndScore.get(position).getStartDate() + " - " + resultAndScore.get(position).getEndDate());
            holder.venue.setText(resultAndScore.get(position).getVenueAddress());
//            holder.pre_test_Score.setText(resultAndScore.get(position).p());
//            holder.post_test_score.setText(resultAndScore.get(position).getPostTestPercentage());
//            holder.activity_score.setText(resultAndScore.get(position).getAssignment());
//            holder.overall_score.setText(resultAndScore.get(position).getOverall());

        }

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


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

    public static class TrainingScoreViewHolder extends RecyclerView.ViewHolder {

        TextView dseName, dealerName, am, trainingLevel, training_dates, venue, pre_test_Score, post_test_score, activity_score, overall_score;
        Button btnCertification;


        public TrainingScoreViewHolder(@NonNull View itemView) {
            super(itemView);
            dseName = itemView.findViewById(R.id.dseName);
            dealerName = itemView.findViewById(R.id.dealerName);
            am = itemView.findViewById(R.id.am);
            trainingLevel = itemView.findViewById(R.id.trainingLevel);
            training_dates = itemView.findViewById(R.id.trainingDates);
            venue = itemView.findViewById(R.id.venue);
            pre_test_Score = itemView.findViewById(R.id.pre_test_Score);
            post_test_score = itemView.findViewById(R.id.post_test_score);
            activity_score = itemView.findViewById(R.id.activity_score);
            overall_score = itemView.findViewById(R.id.overall_score);

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


        }
    }
}