package com.cygnusa.hyperm.adapter;

import android.content.Context;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatRadioButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.QuestionOption;

import java.util.ArrayList;


public class QuestionAdapter extends BaseAdapter {

    private ArrayList<QuestionOption> options;
    private LayoutInflater inflater;
    private String qType;

    public QuestionAdapter(Context context, ArrayList<QuestionOption> options,
                           ListView listView, String qType) {
        this.options = options;
        inflater = LayoutInflater.from(listView.getContext());
        this.qType = qType;
    }

    @Override
    public int getCount() {
        return options.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (qType.trim().equalsIgnoreCase("1")) {
                convertView = inflater.inflate(
                        R.layout.answer_single_choice, null);
                holder.radioButton = convertView.findViewById(R.id.radioButton);
            } else if (qType.trim().equalsIgnoreCase("2")) {
                convertView = inflater.inflate(
                        R.layout.answer_multi_choice, null);
                holder.checkBox = convertView.findViewById(R.id.checkBox);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (qType.trim().equalsIgnoreCase("1")) {
            holder.radioButton.setChecked(options.get(position).isChecked());
            holder.radioButton.setText(options.get(position).getOpttext());
        } else if (qType.trim().equalsIgnoreCase("2")) {
            holder.checkBox.setChecked(options.get(position).isChecked());
            holder.checkBox.setText(options.get(position).getOpttext());
        }

        if (qType.trim().equalsIgnoreCase("1")) {
            holder.radioButton.setId(position);
            holder.radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (QuestionOption option : options) {
                        option.setChecked(false);
                    }
                    options.get(v.getId()).setChecked(true);
                    notifyDataSetChanged();
                }
            });
        } else if (qType.trim().equalsIgnoreCase("2")) {
            holder.checkBox.setId(position);
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    options.get(v.getId()).setChecked(!options.get(v.getId()).isChecked());
                    notifyDataSetChanged();
                }
            });
        }

        return convertView;
    }

    static class ViewHolder {
        AppCompatRadioButton radioButton;
        AppCompatCheckBox checkBox;
    }
}
