package com.cygnusa.hyperm.fragments;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.adapter.ReviewAdapter;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.wrappers.ReviewQuestion;

public class ReviewFragment extends Fragment {

    View mainView;
    ApiService restService;
    String QUESTION_NO = "";
    ReviewQuestion reviewQuestion = new ReviewQuestion();
    TextView questionNumber, questionText;
    ListView answerList;
    LinearLayout correct, wrong;
    public static Button submitButton;
    Context context;

    public ReviewFragment() {

    }

    public void onResumeFragment() {
    }

    public void setReviewQuestion(Context applicationContext, ReviewQuestion question) {
        this.reviewQuestion = question;
        context = applicationContext;
    }

    public void setQuestionNo(String number) {
        this.QUESTION_NO = number;
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mainView = view;
        restService = ((MainApplication) getActivity().getApplication()).getClient();
        //questionNumber = mainView.findViewById(R.id.questionNumber);
        questionText = mainView.findViewById(R.id.questionText);
        answerList = mainView.findViewById(R.id.answerList);
        correct = mainView.findViewById(R.id.correct);
        wrong = mainView.findViewById(R.id.wrong);
        submitButton = mainView.findViewById(R.id.submitButton);
        submitButton.setVisibility(View.GONE);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
                getActivity().overridePendingTransition(R.anim.animation_f_enter,
                        R.anim.animation_f_leave);
            }
        });

        answerList.post(new Runnable() {
            @Override
            public void run() {
                if (reviewQuestion.getIs_correct().trim().equalsIgnoreCase("1")) {
                    correct.setVisibility(View.VISIBLE);
                    wrong.setVisibility(View.GONE);
                } else {

                    correct.setVisibility(View.GONE);
                    wrong.setVisibility(View.VISIBLE);
                }
                //  questionNumber.setText("Q." + QUESTION_NO);
                questionText.setText(reviewQuestion.getQuestiontext());
                answerList.setAdapter(new ReviewAdapter(context,reviewQuestion.getOptions(), answerList, reviewQuestion.getQuestiontypeid(), reviewQuestion.getIs_correct()));
            }
        });
    }

}
