package com.cygnusa.hyperm.fragments;

import android.app.Dialog;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
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.CommonResponse;
import com.cygnusa.hyperm.wrappers.ErrorResponse;
import com.cygnusa.hyperm.wrappers.GetAllScorePercentage;
import com.cygnusa.hyperm.wrappers.SingleParticipantList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

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

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

public class TrainingScoreCommandFragment extends androidx.fragment.app.Fragment {
    HorizontalScrollView headerScroll;
    RecyclerView rcvDynamicView;
    TextView txtNoDataFound;
    int scrollX = 0;
    public ScoreDataAdapter scoreAdapter;

    private Button btnSave, btncancel, btnsubmit, btnFinish;
    ApiService restService;
    boolean isEnable = false;
    int listSize = 0;
    List<GetAllScorePercentage.PercentageData> percentageData = new ArrayList<>();
    private LinearLayout lloutRoleplay, lloutAssignment, lloutPerformance;
    ArrayList<SingleParticipantList> attendanceList = new ArrayList<>();


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


        btncancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });

        return view;
    }

    private void setUpRecyclerView() {

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


    private void initViews(View view) {
        headerScroll = view.findViewById(R.id.headerScroll);
        rcvDynamicView = view.findViewById(R.id.rcvDynamicView);
        txtNoDataFound = view.findViewById(R.id.txtNoDataFound);
        txtNoDataFound.setVisibility(View.GONE);

        lloutAssignment = view.findViewById(R.id.lloutAssignment);
        lloutPerformance = view.findViewById(R.id.lloutPerformance);
        lloutRoleplay = view.findViewById(R.id.lloutRolePlay);


        lloutAssignment.setVisibility(View.GONE);
        lloutPerformance.setVisibility(View.GONE);
        lloutRoleplay.setVisibility(View.GONE);

        btnSave = view.findViewById(R.id.btnSave);
        btncancel = view.findViewById(R.id.btncancel);
        btnsubmit = view.findViewById(R.id.btnsubmit);
        btnFinish = view.findViewById(R.id.btnFinish);


        if (singleDateOneList.getScore_submit() != null && scheduledDataList.get(0).getCurrent_am_acknowledgement_status() != null)
            if (singleDateOneList.getScore_submit().equalsIgnoreCase("1") && scheduledDataList.get(0).getCurrent_am_acknowledgement_status().equalsIgnoreCase("1")) {
                btnsubmit.setVisibility(View.GONE);
                btnFinish.setVisibility(View.VISIBLE);
                btncancel.setVisibility(View.GONE);
                btnSave.setVisibility(View.GONE);
                btnFinish.setEnabled(true);
                btnFinish.setAlpha(1f);
                btnFinish.setText("Finish Training");
                btnFinish.setPadding(2, 0, 2, 0);
            } else if (singleDateOneList.getScore_submit().equalsIgnoreCase("2")) {
                btnsubmit.setVisibility(View.GONE);
                btnFinish.setVisibility(View.VISIBLE);
                btncancel.setVisibility(View.GONE);
                btnSave.setVisibility(View.GONE);
                btnFinish.setEnabled(false);
                btnFinish.setAlpha(.7f);
                btnFinish.setPadding(2, 0, 2, 0);
                btnFinish.setText("Training Finished");
            }

        if (scheduledDataList.get(0).getSubmit_enable().equalsIgnoreCase("1")) {
            btnsubmit.setEnabled(true);
        } else {
            btnsubmit.setEnabled(false);
        }

        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendAllScoreDatasToServer("2");
            }
        });

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

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sendAllScoreDatasToServer("0");
            }
        });

        btnsubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendAllScoreDatasToServer("1");
            }
        });
        if (singleDateOneList != null)
            getScorePercentageDetails();
    }

    private void getScorePercentageDetails() {
        restService.getScorePercentage(singleDateOneList.getLvlId()).enqueue(new Callback<GetAllScorePercentage>() {
            @Override
            public void onResponse(Call<GetAllScorePercentage> call, Response<GetAllScorePercentage> response) {
                if (response.body() != null) {
                    if (response.body().getResponse().equalsIgnoreCase("Success")) {
                        percentageData = response.body().getPercentageDataList();

                        SingleParticipantList attendance = new SingleParticipantList();
                        for (int k = 0; k < singleDateOneList.getSingleParticipantList().size(); k++) {
                            if (singleDateOneList.getSingleParticipantList().get(k).getSingleAttendance() != null) {
                                if (singleDateOneList.getSingleParticipantList().get(k).getSingleAttendance().getIspresent() != null
                                        && singleDateOneList.getSingleParticipantList().get(k).getSingleAttendance().getIspresent().equalsIgnoreCase("1")) {
                                    isEnable = true;
                                    attendance = new SingleParticipantList();
                                    attendance.setCertification(singleDateOneList.getSingleParticipantList().get(k).getCertification());
                                    attendance.setSmpin(singleDateOneList.getSingleParticipantList().get(k).getSmpin());
                                    attendance.setName(singleDateOneList.getSingleParticipantList().get(k).getName());
                                    attendance.setDealerName(singleDateOneList.getSingleParticipantList().get(k).getDealerName());
                                    attendance.setPreTestPercentage(singleDateOneList.getSingleParticipantList().get(k).getPreTestPercentage());
                                    attendance.setPostTestPercentage(singleDateOneList.getSingleParticipantList().get(k).getPostTestPercentage());
                                    attendance.setRoleplayOrDemo(singleDateOneList.getSingleParticipantList().get(k).getRoleplayOrDemo());
                                    attendance.setAssignment(singleDateOneList.getSingleParticipantList().get(k).getAssignment());
                                    attendance.setRemarks(singleDateOneList.getSingleParticipantList().get(k).getRemarks());
                                    attendance.setPreTest(singleDateOneList.getSingleParticipantList().get(k).getPreTest());
                                    attendance.setPostTest(singleDateOneList.getSingleParticipantList().get(k).getPostTest());
                                    attendance.setPerformance(singleDateOneList.getSingleParticipantList().get(k).getPerformance());
                                    attendance.setOverall(singleDateOneList.getSingleParticipantList().get(k).getOverall());
                                    attendance.setUserid(singleDateOneList.getSingleParticipantList().get(k).getUserid());

                                    attendanceList.add(attendance);
                                }
                            }
                        }
                        if (isEnable) {
                            rcvDynamicView.setVisibility(View.VISIBLE);
                            txtNoDataFound.setVisibility(View.GONE);
                            scoreAdapter = new ScoreDataAdapter(attendanceList);
                            rcvDynamicView.setAdapter(scoreAdapter);
                        } else {
                            rcvDynamicView.setVisibility(View.GONE);
                            txtNoDataFound.setVisibility(View.VISIBLE);
                        }
                    } 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<GetAllScorePercentage> call, Throwable t) {

            }
        });
    }

    private void sendAllScoreDatasToServer(String s) {


        if (attendanceList.size() > 0) {
            for (int k = 0; k < attendanceList.size(); k++) {
                if (attendanceList.get(k).getRemarks() == null || attendanceList.get(k).getRemarks().equalsIgnoreCase("")) {
                    GlobalData.toast(getActivity(), "Kindly Enter all Score and Comment Remarks.");
                    return;
                }
            }
        }


        ArrayList<HashMap<String, String>> scoreCalue = new ArrayList();
        HashMap<String, String> scoreValueMap = new HashMap<>();
        if (attendanceList.size() > 0) {
            for (int k = 0; k < attendanceList.size(); k++) {
                scoreValueMap = new HashMap<>();
                scoreValueMap.put("remarks", attendanceList.get(k).getRemarks());
                scoreValueMap.put("userid", attendanceList.get(k).getUserid());
                scoreValueMap.put("roleplay_or_demo", attendanceList.get(k).getRoleplayOrDemo());
                scoreValueMap.put("assignment", attendanceList.get(k).getAssignment());
                scoreValueMap.put("performance", attendanceList.get(k).getPerformance());
                scoreValueMap.put("certification", attendanceList.get(k).getCertification());
                scoreCalue.add(scoreValueMap);
            }
        }


        Gson gson = new Gson();
        JsonElement jsonElement = gson.toJsonTree(
                scoreCalue, new TypeToken<ArrayList<String>>() {
                }.getType());

        HashMap<String, String> map = new HashMap<>();
        map.put("score", jsonElement.toString());
        map.put("schedule_id", singleDateOneList.getScheduleId());
        map.put("score_submit", s);

        restService.sendScoreDetailsToServer(map).enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                if (response.body() != null) {
                    if (response.body().getResponse().equalsIgnoreCase("Success")) {
                        if (s.equalsIgnoreCase("1")) {
                            btnsubmit.setVisibility(View.GONE);
                            btnFinish.setVisibility(View.VISIBLE);
                            btnSave.setVisibility(View.GONE);
                        } else {
                            GlobalData.toast(getActivity(), response.body().getMessage());
                            getActivity().onBackPressed();
                        }
                    } 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<CommonResponse> call, Throwable t) {

            }
        });
    }

    private class ScoreDataAdapter extends RecyclerView.Adapter<TrainingScoreCommandFragment.ScoreViewHolder> {
        float overallScore = 0, overAllPercentage = 0;
        ArrayList<SingleParticipantList> attendanceList;

        public ScoreDataAdapter(ArrayList<SingleParticipantList> list) {
            attendanceList = list;
        }

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

        @Override
        public void onBindViewHolder(@NonNull ScoreViewHolder holder, int position) {
            holder.smPin.setText(attendanceList.get(position).getSmpin());
            holder.participantName.setText(attendanceList.get(position).getName());
            holder.dealerName.setText(attendanceList.get(position).getDealerName());
            holder.preScore.setText(attendanceList.get(position).getPreTestPercentage());
            holder.postScore.setText(attendanceList.get(position).getPostTestPercentage());
            holder.rolePlayScore.setText(attendanceList.get(position).getRoleplayOrDemo());
            holder.assignmentScore.setText(attendanceList.get(position).getAssignment());
            holder.performance_score.setText(attendanceList.get(position).getPerformance());

            for (int k = 0; k < percentageData.size(); k++) {
                if (percentageData.get(k).getName().contains("Certification %")) {
                    overAllPercentage = Integer.parseInt(percentageData.get(k).getPercentage());
                }

                if (percentageData.get(k).getName() != null && percentageData.get(k).getName().equalsIgnoreCase("Role Play/ Demo")) {
                    holder.lloutRoleplay.setVisibility(View.VISIBLE);
                    lloutRoleplay.setVisibility(View.VISIBLE);
                    holder.viewOne.setVisibility(View.VISIBLE);

                }

                if (percentageData.get(k).getName() != null && percentageData.get(k).getName().equalsIgnoreCase("Assignment")) {
                    holder.lloutAssignment.setVisibility(View.VISIBLE);
                    lloutAssignment.setVisibility(View.VISIBLE);
                    holder.viewTwo.setVisibility(View.VISIBLE);
                }

                if (percentageData.get(k).getName() != null && percentageData.get(k).getName().equalsIgnoreCase("Performance")) {
                    holder.lloutPerformance.setVisibility(View.VISIBLE);
                    lloutPerformance.setVisibility(View.VISIBLE);
                    holder.viewThree.setVisibility(View.VISIBLE);
                }
            }
            overallScore = 0;
            if (attendanceList.get(position).getPreTestPercentage() != null && !attendanceList.get(position).getPreTestPercentage().equalsIgnoreCase("")) {
                overallScore = Float.parseFloat(attendanceList.get(position).getPreTestPercentage());
            }
            if (attendanceList.get(position).getPostTestPercentage() != null && !attendanceList.get(position).getPostTestPercentage().equalsIgnoreCase("")) {
                overallScore = overallScore + Float.parseFloat(attendanceList.get(position).getPostTestPercentage());
            }
            if (attendanceList.get(position).getPerformance() != null && !attendanceList.get(position).getPerformance().equalsIgnoreCase("")) {
                overallScore = overallScore + Float.parseFloat(attendanceList.get(position).getPerformance());
            }
            if (attendanceList.get(position).getAssignment() != null && !attendanceList.get(position).getAssignment().equalsIgnoreCase("")) {
                overallScore = overallScore + Float.parseFloat(attendanceList.get(position).getAssignment());
            }
            if (attendanceList.get(position).getRoleplayOrDemo() != null && !attendanceList.get(position).getRoleplayOrDemo().equalsIgnoreCase("")) {
                overallScore = overallScore + Float.parseFloat(attendanceList.get(position).getRoleplayOrDemo());
            }

            if (attendanceList.get(position).getOverall() != null && !attendanceList.get(position).getOverall().isEmpty())
                holder.sendCode.setText(attendanceList.get(position).getOverall());

            if (attendanceList.get(position).getRemarks() != null && !attendanceList.get(position).getRemarks().isEmpty()) {
                holder.enterReason.setText("View Remarks");
            } else {
                holder.enterReason.setText("Enter Remarks");
            }

            if (overallScore >= overAllPercentage) {
                holder.sendCode.setText("Certified");
                ColorStateList colorStateList = ContextCompat.getColorStateList(getActivity(), R.color.btn_green);
                holder.sendCode.setBackgroundTintList(colorStateList);
                attendanceList.get(position).setCertification("1");
            } else {
                ColorStateList colorStateList = ContextCompat.getColorStateList(getActivity(), R.color.btn_red);
                holder.sendCode.setBackgroundTintList(colorStateList);
                holder.sendCode.setText("Failed");
                attendanceList.get(position).setCertification("2");
            }

            holder.overall_score.setText("" + overallScore);

            holder.rolePlayScore.setTag(position);
            holder.rolePlayScore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String fixedScore = "";
                    for (int i = 0; i < percentageData.size(); i++) {
                        if (percentageData.get(i).getName().equalsIgnoreCase("Role Play/ Demo")) {
                            fixedScore = percentageData.get(i).getPercentage();
                        }
                    }
                    enterRolePlayCode((Integer) v.getTag(), fixedScore, attendanceList.get((Integer) v.getTag()).getName(), attendanceList.get((Integer) v.getTag()).getSmpin());

                }
            });

            holder.assignmentScore.setTag(position);
            holder.assignmentScore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String fixedScore = "";
                    for (int i = 0; i < percentageData.size(); i++) {
                        if (percentageData.get(i).getName().equalsIgnoreCase("Assignment")) {
                            fixedScore = percentageData.get(i).getPercentage();
                        }
                    }
                    enterAssignmentCode((Integer) v.getTag(), fixedScore, attendanceList.get((Integer) v.getTag()).getName(), attendanceList.get((Integer) v.getTag()).getSmpin());
                }
            });

            holder.performance_score.setTag(position);
            holder.performance_score.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String fixedScore = "";
                    for (int i = 0; i < percentageData.size(); i++) {
                        if (percentageData.get(i).getName().equalsIgnoreCase("Performance")) {
                            fixedScore = percentageData.get(i).getPercentage();
                        }
                    }
                    enterPerformanceCode((Integer) v.getTag(), fixedScore, attendanceList.get((Integer) v.getTag()).getName(), attendanceList.get((Integer) v.getTag()).getSmpin());
                }
            });


            holder.sendCode.setTag(position);
            holder.sendCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // enterCode(position, attendanceList.get(position).getOverall(), attendanceList.get(position).getName(), attendanceList.get(position).getSmpin());
                }
            });

            holder.enterReason.setTag(position);
            holder.enterReason.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    enterComment((Integer) v.getTag(), attendanceList.get((Integer) v.getTag()).getRemarks(), attendanceList.get((Integer) v.getTag()).getName(), attendanceList.get((Integer) v.getTag()).getSmpin());
                }
            });
        }


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

    private void enterPerformanceCode(int position, String fixedScore, String name, String smpin) {

        Dialog aDialog = new Dialog(getActivity());
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_send_code);
        aDialog.setCancelable(false);

        EditText edtManualReason = aDialog.findViewById(R.id.edtManualReason);
        TextView txtTitle = aDialog.findViewById(R.id.txtTitle);
        TextView txtSubTitle = aDialog.findViewById(R.id.txtSubTitle);

        edtManualReason.setLines(1);

        txtTitle.setText("Performance");
        edtManualReason.setHint("Enter Score");
        txtSubTitle.setText("Participant Name : " + smpin + "-" + name);
        edtManualReason.setInputType(InputType.TYPE_CLASS_NUMBER);
        edtManualReason.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});

        Button btnOk = aDialog.findViewById(R.id.btnOk);
        Button btnCancel = aDialog.findViewById(R.id.btnCancel);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edtManualReason.getText().toString().trim().isEmpty()) {
                    GlobalData.toast(getActivity(), "Enter Score");
                    return;
                }

                int enterdScore = Integer.parseInt(edtManualReason.getText().toString().trim());
                int fixedScoreValue = Integer.parseInt(fixedScore);
                if (enterdScore > fixedScoreValue) {
                    GlobalData.toast(getActivity(), "Entered Score should be less than or Equal to " + fixedScoreValue);
                } else {
                    setPerformanceDatas(position, edtManualReason.getText().toString());
                    scoreAdapter.notifyDataSetChanged();
                    aDialog.dismiss();
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        aDialog.show();

    }

    private void enterAssignmentCode(int position, String fixedScore, String name, String smpin) {

        Dialog aDialog = new Dialog(getActivity());
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_send_code);
        aDialog.setCancelable(false);

        EditText edtManualReason = aDialog.findViewById(R.id.edtManualReason);
        TextView txtTitle = aDialog.findViewById(R.id.txtTitle);
        TextView txtSubTitle = aDialog.findViewById(R.id.txtSubTitle);

        edtManualReason.setLines(1);

        txtTitle.setText("Assignment");
        edtManualReason.setHint("Enter Score");
        txtSubTitle.setText("Participant Name : " + smpin + "-" + name);
        edtManualReason.setInputType(InputType.TYPE_CLASS_NUMBER);
        edtManualReason.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});

        Button btnOk = aDialog.findViewById(R.id.btnOk);
        Button btnCancel = aDialog.findViewById(R.id.btnCancel);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edtManualReason.getText().toString().trim().isEmpty()) {
                    GlobalData.toast(getActivity(), "Enter Score");
                    return;
                }
                int enterdScore = Integer.parseInt(edtManualReason.getText().toString().trim());
                int fixedScoreValue = Integer.parseInt(fixedScore);
                if (enterdScore > fixedScoreValue) {
                    GlobalData.toast(getActivity(), "Entered Score should be less than or Equal to " + fixedScoreValue);
                } else {
                    setAssignmentDatas(position, edtManualReason.getText().toString());
                    scoreAdapter.notifyDataSetChanged();
                    aDialog.dismiss();
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        aDialog.show();

    }

    private void enterRolePlayCode(int position, String fixedScore, String name, String smpin) {

        Dialog aDialog = new Dialog(getActivity());
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_send_code);
        aDialog.setCancelable(false);

        EditText edtManualReason = aDialog.findViewById(R.id.edtManualReason);
        TextView txtTitle = aDialog.findViewById(R.id.txtTitle);
        TextView txtSubTitle = aDialog.findViewById(R.id.txtSubTitle);

        edtManualReason.setLines(1);

        txtTitle.setText("RolePlay/Demo Score");
        edtManualReason.setHint("Enter Score");
        txtSubTitle.setText("Participant Name : " + smpin + "-" + name);
        edtManualReason.setInputType(InputType.TYPE_CLASS_NUMBER);
        edtManualReason.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});

        Button btnOk = aDialog.findViewById(R.id.btnOk);
        Button btnCancel = aDialog.findViewById(R.id.btnCancel);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edtManualReason.getText().toString().trim().isEmpty()) {
                    GlobalData.toast(getActivity(), "Enter Score");
                    return;
                }
                int enterdScore = Integer.parseInt(edtManualReason.getText().toString().trim());
                int fixedScoreValue = Integer.parseInt(fixedScore);
                if (enterdScore > fixedScoreValue) {
                    GlobalData.toast(getActivity(), "Entered Score should be less than or Equal to " + fixedScoreValue);
                } else {
                    setRolepayClickedDatas(position, edtManualReason.getText().toString());
                    scoreAdapter.notifyDataSetChanged();
                    aDialog.dismiss();
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        aDialog.show();

    }

    private void enterCode(int position, String enterScore, String name, String smpin) {


        Dialog aDialog = new Dialog(getActivity());
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_send_code);
        aDialog.setCancelable(false);

        EditText edtManualReason = aDialog.findViewById(R.id.edtManualReason);
        TextView txtTitle = aDialog.findViewById(R.id.txtTitle);
        TextView txtSubTitle = aDialog.findViewById(R.id.txtSubTitle);

        edtManualReason.setLines(1);

        if (enterScore != null && !enterScore.isEmpty())
            edtManualReason.setText(enterScore);

        txtTitle.setText("Certification Score");
        edtManualReason.setHint("Enter Score");
        txtSubTitle.setText("Participant Name : " + smpin + "-" + name);
        edtManualReason.setInputType(InputType.TYPE_CLASS_NUMBER);

        Button btnOk = aDialog.findViewById(R.id.btnOk);
        Button btnCancel = aDialog.findViewById(R.id.btnCancel);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edtManualReason.getText().toString().trim().isEmpty()) {
                    GlobalData.toast(getActivity(), "Enter Score");
                } else {
                    setClickedDatas(position, edtManualReason.getText().toString());
                    scoreAdapter.notifyDataSetChanged();
                    aDialog.dismiss();
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        aDialog.show();

    }

    private void enterComment(int position, String comment, String name, String smpin) {


        Dialog aDialog = new Dialog(getActivity());
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_send_code);
        aDialog.setCancelable(false);

        EditText edtManualReason = aDialog.findViewById(R.id.edtManualReason);
        TextView txtTitle = aDialog.findViewById(R.id.txtTitle);
        TextView txtSubTitle = aDialog.findViewById(R.id.txtSubTitle);

        txtTitle.setText("Enter Remarks");
        txtSubTitle.setText("Participant Name : " + smpin + "-" + name);
        edtManualReason.setHint("Enter Remarks (Min 60 letters)");
        edtManualReason.setLines(3);

        if (comment != null && !comment.isEmpty())
            edtManualReason.setText(comment);


        Button btnOk = aDialog.findViewById(R.id.btnOk);
        Button btnCancel = aDialog.findViewById(R.id.btnCancel);

        if (singleDateOneList.getScore_submit().equalsIgnoreCase("2")) {
            btnOk.setVisibility(View.GONE);
        }

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edtManualReason.getText().toString().trim().isEmpty()) {
                    GlobalData.toast(getActivity(), "Enter Comment");
                } else if (edtManualReason.getText().toString().trim().length() < 60) {
                    GlobalData.toast(getActivity(), "Kindly enter minimum 60 characters.");
                } else {
                    setComentDatas(position, edtManualReason.getText().toString());
                    scoreAdapter.notifyDataSetChanged();
                    aDialog.dismiss();
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        aDialog.show();

    }

    private void setComentDatas(int position, String reason) {

        attendanceList.get(position).setRemarks(reason);
    }

    private void setRolepayClickedDatas(int position, String reason) {
        attendanceList.get(position).setRoleplayOrDemo(reason);
    }

    private void setAssignmentDatas(int position, String reason) {
        attendanceList.get(position).setAssignment(reason);
    }

    private void setPerformanceDatas(int position, String reason) {
        attendanceList.get(position).setPerformance(reason);
    }

    private void setClickedDatas(int position, String reason) {
        attendanceList.get(position).setOverall(reason);
    }

    private static class ScoreViewHolder extends RecyclerView.ViewHolder {
        TextView smPin, participantName, dealerName, sendCode, enterReason, preScore, postScore, rolePlayScore, assignmentScore, performance_score, overall_score;
        LinearLayout lloutAssignment, lloutRoleplay, lloutPerformance;

        View viewOne, viewTwo, viewThree;
        Button btnCertification;

        public ScoreViewHolder(@NonNull View itemView) {
            super(itemView);
            smPin = itemView.findViewById(R.id.smpin);
            participantName = itemView.findViewById(R.id.participant_name);
            dealerName = itemView.findViewById(R.id.dealername);
            sendCode = itemView.findViewById(R.id.btnCertification);
            preScore = itemView.findViewById(R.id.preScore);
            postScore = itemView.findViewById(R.id.post_score);
            rolePlayScore = itemView.findViewById(R.id.rolePlayScore);
            assignmentScore = itemView.findViewById(R.id.assignmentScore);
            performance_score = itemView.findViewById(R.id.performance_score);
            overall_score = itemView.findViewById(R.id.overall_score);
            enterReason = itemView.findViewById(R.id.chkMark);

            lloutAssignment = itemView.findViewById(R.id.lloutAssignment);
            lloutRoleplay = itemView.findViewById(R.id.lloutRolePlay);
            lloutPerformance = itemView.findViewById(R.id.lloutPerformance);

            viewOne = itemView.findViewById(R.id.viewOne);
            viewTwo = itemView.findViewById(R.id.viewTwo);
            viewThree = itemView.findViewById(R.id.viewThree);


            lloutPerformance.setVisibility(View.GONE);
            lloutAssignment.setVisibility(View.GONE);
            lloutRoleplay.setVisibility(View.GONE);

            viewOne.setVisibility(View.GONE);
            viewTwo.setVisibility(View.GONE);
            viewThree.setVisibility(View.GONE);

            if (singleDateOneList.getScore_submit().equalsIgnoreCase("2")) {
                sendCode.setEnabled(false);
                preScore.setEnabled(false);
                postScore.setEnabled(false);
                rolePlayScore.setEnabled(false);
                assignmentScore.setEnabled(false);
                performance_score.setEnabled(false);
                overall_score.setEnabled(false);
            }
        }
    }
}
