package com.cygnusa.hyperm.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

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

import com.google.gson.Gson;
import com.cygnusa.hyperm.R;
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.GetAllTrainingSummary;

import java.util.Objects;

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

import static com.cygnusa.hyperm.util.GlobalData.singleDateOneList;


public class TrainingSummaryFragment extends Fragment {

    private TextView totalPlannedParticipants, totalParticipantAppeared, noOfDSECertified, noOfDseFailed, batch, avgBatch, noDataFound;
    private RecyclerView rcvSummaryList;
    SummaryAdapter adapter;
    ApiService restService;
    public static GetAllTrainingSummary.SummaryData summaryData = new GetAllTrainingSummary.SummaryData();

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

    private void initViews(View view) {
        totalPlannedParticipants = view.findViewById(R.id.totalPlannedParticipants);
        totalParticipantAppeared = view.findViewById(R.id.totalParticipantAppeared);
        noOfDSECertified = view.findViewById(R.id.noOfDSECertified);
        noOfDseFailed = view.findViewById(R.id.noOfDseFailed);
        batch = view.findViewById(R.id.batch);
        noDataFound = view.findViewById(R.id.noDataFound);
        avgBatch = view.findViewById(R.id.avgBatch);

        restService = ((MainApplication) Objects.requireNonNull(getActivity()).getApplicationContext()).getClient();

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

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


        getAllTrainingSummary();

    }

    private void getAllTrainingSummary() {
        restService.getTrainingSummary(singleDateOneList.getScheduleId()).enqueue(new Callback<GetAllTrainingSummary>() {
            @Override
            public void onResponse(Call<GetAllTrainingSummary> call, Response<GetAllTrainingSummary> response) {
                if (response.body() != null) {
                    if (response.body().getResponse().equalsIgnoreCase("Success")) {
                        summaryData = response.body().getSummaryData();

                        if (summaryData.getSummaryAttendanceList() != null && summaryData.getSummaryAttendanceList().size() > 0) {
                            noDataFound.setVisibility(View.GONE);
                            rcvSummaryList.setVisibility(View.VISIBLE);
                            if (singleDateOneList.getScore_submit().equalsIgnoreCase("2")) {
                                setDetails();
                                adapter = new SummaryAdapter();
                                rcvSummaryList.setAdapter(adapter);
                            } else {
                                noDataFound.setVisibility(View.VISIBLE);
                                rcvSummaryList.setVisibility(View.GONE);
                            }
                        } else {
                            noDataFound.setVisibility(View.VISIBLE);
                            rcvSummaryList.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;
                        Objects.requireNonNull(getActivity()).finish();
                    }
                }
            }

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

            }
        });
    }

    private void setDetails() {
        totalParticipantAppeared.setText(summaryData.getTotalParticipantAppeared());
        totalPlannedParticipants.setText(summaryData.getTotalPlannedParticipant());
        noOfDSECertified.setText(summaryData.getNoOfDseCertified());
        noOfDseFailed.setText(summaryData.getNoOfDseFailed());
        batch.setText(summaryData.getBatchThrough());
        avgBatch.setText(summaryData.getAverageBatchScore());
    }

    private static class SummaryAdapter extends RecyclerView.Adapter<viewHolder> {
        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.summary_view_holder, parent, false);
            return new viewHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.present.setText(summaryData.getSummaryAttendanceList().get(position).getNoOfAbsents());
            holder.appeared.setText(summaryData.getSummaryAttendanceList().get(position).getTotalParticipantAppeared());
            holder.plan.setText(summaryData.getSummaryAttendanceList().get(position).getTotalPlannedParticipant());
            holder.days.setText(summaryData.getSummaryAttendanceList().get(position).getDay());
        }

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

    private static class viewHolder extends RecyclerView.ViewHolder {
        private TextView days, plan, appeared, present;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            days = itemView.findViewById(R.id.days);
            plan = itemView.findViewById(R.id.plan);
            appeared = itemView.findViewById(R.id.appeared);
            present = itemView.findViewById(R.id.present);
        }
    }
}
