package com.cygnusa.hyperm.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatRadioButton;

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

import java.util.ArrayList;


public class ReviewAdapter extends BaseAdapter {

    private ArrayList<ReviewOption> options = new ArrayList<>();
    private LayoutInflater inflater;
    private String qType;
    private String isCorrect;
    Context context;

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

    @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());
            holder.radioButton.setEnabled(false);
            if (isCorrect.trim().equalsIgnoreCase("1")) {
                if (options.get(position).isChecked()) {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#006401"), Color.parseColor("#006401")});
                    holder.radioButton.setButtonTintList(colorStateList);
                    holder.radioButton.setTextColor(colorStateList);
                } else {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#000000"), Color.parseColor("#000000")});
                    holder.radioButton.setButtonTintList(colorStateList);
                    holder.radioButton.setTextColor(colorStateList);
                }
            } else {
                if (options.get(position).isChecked()) {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#950405"), Color.parseColor("#950405")});
                    holder.radioButton.setButtonTintList(colorStateList);
                    holder.radioButton.setTextColor(colorStateList);
                } else {
                    if (options.get(position).getIscorrect().equalsIgnoreCase("1")) {
                        ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#008000"), Color.parseColor("#008000")});
                        holder.radioButton.setButtonTintList(colorStateList);
                        holder.radioButton.setTextColor(colorStateList);
                    } else {
                        ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#000000"), Color.parseColor("#000000")});
                        holder.radioButton.setButtonTintList(colorStateList);
                        holder.radioButton.setTextColor(colorStateList);
                    }
                }
            }


            holder.radioButton.invalidate();
        } else if (qType.trim().equalsIgnoreCase("2")) {
            holder.checkBox.setChecked(options.get(position).isChecked());
            holder.checkBox.setText(options.get(position).getOpttext());
            holder.checkBox.setEnabled(false);
            if (isCorrect.trim().equalsIgnoreCase("1")) {
                if (options.get(position).isChecked()) {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#006401"), Color.parseColor("#006401")});
                    holder.checkBox.setButtonTintList(colorStateList);
                    holder.checkBox.setTextColor(colorStateList);
                } else {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#008000"), Color.parseColor("#008000")});
                    holder.checkBox.setButtonTintList(colorStateList);
                    holder.checkBox.setTextColor(colorStateList);
                }
            } else {
                if (options.get(position).isChecked()) {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#950405"), Color.parseColor("#950405")});
                    holder.checkBox.setButtonTintList(colorStateList);
                    holder.checkBox.setTextColor(colorStateList);
                } else {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                            new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#000000"), Color.parseColor("#000000")});
                    holder.checkBox.setButtonTintList(colorStateList);
                    holder.checkBox.setTextColor(colorStateList);
                }
            }
            holder.checkBox.invalidate();
        }

//        if (options.get(position).getIscorrect().equalsIgnoreCase("1"))
//            holder.radioButton.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));

        return convertView;
    }

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