package com.cygnusa.hyperm.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatRadioButton;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.smilerating.BaseRating;
import com.cygnusa.hyperm.smilerating.SmileyRating;
import com.cygnusa.hyperm.wrappers.AssessmentQuestion;
import com.cygnusa.hyperm.wrappers.QuestionOption;
import com.cygnusa.hyperm.wrappers.RatingOption;

import java.util.ArrayList;


public class SurveyQuestionAdapter extends BaseAdapter {

    private ArrayList<AssessmentQuestion> items;
    private Context context;
    private boolean isReview;
    String TAG = "Smiley";

    public SurveyQuestionAdapter(Context context, ArrayList<AssessmentQuestion> items, boolean isReview) {
        this.items = items;
        this.context = context;
        this.isReview = isReview;
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(this.context).inflate(R.layout.answer_rating, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final AssessmentQuestion item = items.get(position);

        holder.questionText.setText(item.getQuestiontext());
        holder.ratingView.removeAllViews();
        holder.singleChoiceView.removeAllViews();
        holder.multiChoiceView.removeAllViews();
        holder.smileyview.removeAllViews();
        holder.remarks.clearFocus();

        if (item.getQuestiontypeid() != null && item.getQuestiontypeid().trim().equalsIgnoreCase("3")) {
            holder.ratingCardView.setVisibility(View.VISIBLE);
            holder.ratingView.setVisibility(View.VISIBLE);
            holder.remarks.setVisibility(View.GONE);
            holder.singleChoiceView.setVisibility(View.GONE);
            holder.smileyview.setVisibility(View.GONE);
            holder.multiChoiceView.setVisibility(View.GONE);

            if (item.getRating_options() != null && item.getRating_options().size() > 0) {
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,
                        RadioGroup.LayoutParams.WRAP_CONTENT, 1f);
                for (int i = 0; i < item.getRating_options().size(); i++) {
                    View v = LayoutInflater.from(context).inflate(R.layout.answer_rating_view, null, false);
                    AppCompatRadioButton radioButton = v.findViewById(R.id.radioButton);
                    radioButton.setText(item.getRating_options().get(i).getRating_text());
                    radioButton.setChecked(item.getRating_options().get(i).isChecked());
                    radioButton.setTag(item);
                    radioButton.setId(i);
                    radioButton.setEnabled(!isReview);
                    v.setEnabled(!isReview);
                    if (isReview) {
                        ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#00344c"), Color.parseColor("#2f6aac")});
                        radioButton.setCompoundDrawableTintList(colorStateList);
                    }

                    radioButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AssessmentQuestion question = (AssessmentQuestion) v.getTag();
                            for (RatingOption ratingOption : question.getRating_options()) {
                                ratingOption.setChecked(false);
                            }
                            question.getRating_options().get(v.getId()).setChecked(true);
                            notifyDataSetChanged();
                        }
                    });

                    holder.ratingView.addView(v, params);
                }
            }

            holder.ratingView.setEnabled(!isReview);
        } else if (item.getQuestiontypeid() != null && item.getQuestiontypeid().trim().equalsIgnoreCase("5")) {
            holder.ratingCardView.setVisibility(View.VISIBLE);
            holder.ratingView.setVisibility(View.GONE);
            holder.remarks.setVisibility(View.GONE);
            holder.singleChoiceView.setVisibility(View.VISIBLE);
            holder.multiChoiceView.setVisibility(View.GONE);
            holder.smileyview.setVisibility(View.GONE);

            if (item.getOptions() != null && item.getOptions().size() > 0) {
                for (int i = 0; i < item.getOptions().size(); i++) {
                    View v = LayoutInflater.from(context).inflate(R.layout.answer_single_choice_view, null, false);
                    AppCompatRadioButton radioButton = v.findViewById(R.id.radioButton);
                    radioButton.setText(item.getOptions().get(i).getOpttext());
                    radioButton.setChecked(item.getOptions().get(i).isCheckedSurvey());
                    radioButton.setTag(item);
                    radioButton.setId(i);
                    radioButton.setEnabled(!isReview);
                    v.setEnabled(!isReview);
                    if (isReview) {
                        ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#00344c"), Color.parseColor("#00344c")});
                        radioButton.setCompoundDrawableTintList(colorStateList);
                    }

                    radioButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AssessmentQuestion question = (AssessmentQuestion) v.getTag();
                            for (QuestionOption ratingOption : question.getOptions()) {
                                ratingOption.setCheckedSurvey(false);
                            }
                            question.getOptions().get(v.getId()).setCheckedSurvey(true);
                            notifyDataSetChanged();
                        }
                    });

                    holder.singleChoiceView.addView(v);
                }
            }

            holder.singleChoiceView.setEnabled(!isReview);
        } else if (item.getQuestiontypeid() != null && item.getQuestiontypeid().trim().equalsIgnoreCase("6")) {
            holder.ratingCardView.setVisibility(View.VISIBLE);
            holder.ratingView.setVisibility(View.GONE);
            holder.remarks.setVisibility(View.GONE);
            holder.singleChoiceView.setVisibility(View.GONE);
            holder.multiChoiceView.setVisibility(View.VISIBLE);
            holder.smileyview.setVisibility(View.GONE);

            if (item.getOptions() != null && item.getOptions().size() > 0) {
                for (int i = 0; i < item.getOptions().size(); i++) {
                    View v = LayoutInflater.from(context).inflate(R.layout.answer_multi_choice, null, false);
                    AppCompatCheckBox radioButton = v.findViewById(R.id.checkBox);
                    radioButton.setText(item.getOptions().get(i).getOpttext());
                    radioButton.setChecked(item.getOptions().get(i).isCheckedSurvey());
                    radioButton.setTag(item);
                    radioButton.setId(i);
                    radioButton.setEnabled(!isReview);
                    v.setEnabled(!isReview);
                    if (isReview) {
                        ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_enabled}}, new int[]{Color.parseColor("#00344c"), Color.parseColor("#2f6aac")});
                        radioButton.setCompoundDrawableTintList(colorStateList);
                    }

                    radioButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AssessmentQuestion question = (AssessmentQuestion) v.getTag();
                            question.getOptions().get(v.getId()).setCheckedSurvey(!question.getOptions().get(v.getId()).isCheckedSurvey());
                            notifyDataSetChanged();
                        }
                    });

                    holder.multiChoiceView.addView(v);
                }
            }

            holder.multiChoiceView.setEnabled(!isReview);
        } else if (item.getQuestiontypeid() != null && item.getQuestiontypeid().trim().equalsIgnoreCase("7")) {
            holder.ratingCardView.setVisibility(View.VISIBLE);
            holder.ratingView.setVisibility(View.GONE);
            holder.remarks.setVisibility(View.GONE);
            holder.singleChoiceView.setVisibility(View.GONE);
            holder.multiChoiceView.setVisibility(View.GONE);
            holder.smileyview.setVisibility(View.VISIBLE);
            final View v = LayoutInflater.from(context).inflate(R.layout.answer_smiley_view, null, false);
            final SmileyRating smileRating = v.findViewById(R.id.smiley_rating);


            if (item.getRating_options() != null && item.getRating_options().size() > 0) {
                for (int i = 0; i < item.getRating_options().size(); i++) {
                    smileRating.setTag(item);
                    smileRating.setId(i);
                    smileRating.setNameForSmile(BaseRating.BAD, "BAD");
                    smileRating.setNameForSmile(BaseRating.TERRIBLE, "TERRIBLE");
                    smileRating.setNameForSmile(BaseRating.GOOD, "GOOD");
                    smileRating.setNameForSmile(BaseRating.GREAT, "GREATE");
                    smileRating.setNameForSmile(BaseRating.OKAY, "OKAY");
                }
            }
            if (items.get(position).getRating_options().get(position).getSelected() != null) {
                if (items.get(position).getRating_options().get(position).getSelected().equalsIgnoreCase("BAD")) {
                    smileRating.setSelectedSmile(BaseRating.BAD);
                }
            }
            smileRating.setOnSmileySelectionListener(new SmileyRating.OnSmileySelectionListener() {
                @Override
                public void onSmileySelected(int smiley, boolean reselected) {
                    switch (smiley) {
                        case SmileyRating.BAD:
                            Log.i(TAG, "Bad" + String.valueOf(position));
                            smileRating.setSelectedSmile(BaseRating.BAD, true);
                            items.get(position).getRating_options().get(position).setSelected("BAD");
                            break;
                        case SmileyRating.GOOD:
                            Log.i(TAG, "Good" + String.valueOf(position));
                            smileRating.setSelectedSmile(BaseRating.GOOD, true);
                            break;
                        case SmileyRating.GREAT:
                            Log.i(TAG, "Great" + String.valueOf(position));
                            smileRating.setSelectedSmile(BaseRating.GREAT, true);
                            break;
                        case SmileyRating.OKAY:
                            Log.i(TAG, "Okay" + String.valueOf(position));
                            smileRating.setSelectedSmile(BaseRating.OKAY, true);
                            break;
                        case SmileyRating.TERRIBLE:
                            Log.i(TAG, "Terrible" + String.valueOf(position));
                            break;
                        case BaseRating.NONE:
                            Log.i(TAG, "None" + String.valueOf(position));
                            break;
                        default:
                    }
                }

            });
            holder.smileyview.addView(v);
        } else {
            holder.ratingCardView.setVisibility(View.GONE);
            holder.remarks.setVisibility(View.VISIBLE);
            holder.smileyview.setVisibility(View.GONE);
            TextChangeListener oldWatcher = (TextChangeListener) holder.remarks.getTag();
            if (oldWatcher != null)
                holder.remarks.removeTextChangedListener(oldWatcher);

            holder.remarks.setText(item.getSurvey_text());
            holder.remarks.setId(position);

            TextChangeListener newWatcher = new TextChangeListener(position);
            holder.remarks.setTag(newWatcher);
            holder.remarks.addTextChangedListener(newWatcher);
            holder.remarks.setEnabled(!isReview);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView questionText;
        EditText remarks;
        FrameLayout ratingCardView;
        RadioGroup singleChoiceView;
        public LinearLayout multiChoiceView, ratingView, smileyview;

        View view;

        private ViewHolder(View itemView) {
            view = itemView;
            ratingView = itemView.findViewById(R.id.ratingView);
            ratingCardView = itemView.findViewById(R.id.ratingCardView);
            remarks = itemView.findViewById(R.id.remarks);
            smileyview = itemView.findViewById(R.id.smileyView);
            questionText = itemView.findViewById(R.id.questionText);
            singleChoiceView = itemView.findViewById(R.id.singleChoiceView);
            multiChoiceView = itemView.findViewById(R.id.multiChoiceView);
        }
    }

    public class TextChangeListener implements TextWatcher {
        int position;

        TextChangeListener(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            items.get(position).setSurvey_text(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
