package com.noprestige.kanaquiz.quiz;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import org.apmem.tools.layouts.FlowLayout;

import java.util.ArrayList;
import java.util.Collection;

public class MultipleChoicePad extends FlowLayout
{
    Collection<Button> btnChoices = new ArrayList<>();
    private OnAnswerListener answerListener;

    public MultipleChoicePad(Context context)
    {
        super(context);
    }

    public MultipleChoicePad(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public MultipleChoicePad(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public void setOnAnswerListener(OnAnswerListener listener)
    {
        answerListener = listener;
    }

    public void onAnswerSubmit(String userAnswer)
    {
        for (Button btnChoice : btnChoices)
            btnChoice.setEnabled(false);
        if (answerListener != null)
            answerListener.onAnswer(userAnswer);
    }

    public void enableButtons()
    {
        for (Button btnChoice : btnChoices)
            btnChoice.setEnabled(true);
    }

    public void deleteChoices()
    {
        removeAllViews();
        btnChoices = new ArrayList<>();
    }

    private void submitAnswer(View view, String answer)
    {
        //ref: https://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color
        view.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        onAnswerSubmit(answer);
        btnChoices.remove(view); //prevents selected button from being re-enabled
    }

    public void addChoice(String answer)
    {
        Button btnNewButton = new Button(getContext());
        btnNewButton.setOnClickListener((view) -> submitAnswer(view, answer));
        btnNewButton.setText(answer);
        addView(btnNewButton);
        btnChoices.add(btnNewButton);
    }

    public void setChoices(String[] answers)
    {
        deleteChoices();
        setVisibility(View.INVISIBLE);
        for (String answer : answers)
            addChoice(answer);

        new NormalizeSizeTask().execute(this);
    }

    static class NormalizeSizeTask extends AsyncTask<MultipleChoicePad, Void, MultipleChoicePad>
    {
        private int maxSize;

        @Override
        protected MultipleChoicePad doInBackground(MultipleChoicePad... item)
        {
            try
            {
                do
                {
                    Thread.sleep(100);
                    for (Button btnChoice : item[0].btnChoices)
                    {
                        int thisWidth = btnChoice.getWidth();
                        if (thisWidth > maxSize)
                            maxSize = thisWidth;
                    }
                }
                while (maxSize == 0);
            }
            catch (InterruptedException ignored)
            {
                cancel(true);
            }
            return item[0];
        }

        @Override
        protected void onPostExecute(MultipleChoicePad item)
        {
            if (maxSize > 0)
            {
                if ((maxSize * 2) > item.getMeasuredWidth())
                    maxSize = (item.getMeasuredWidth() / 2);
                for (Button btnChoice : item.btnChoices)
                    btnChoice.setWidth(maxSize);
            }
            item.setVisibility(View.VISIBLE);
        }
    }
}

@@ -1998,32 +1998,83 @@
 n.setText(answer
+.replace(%22 %22, System.getProperty(%22line.separator%22))
 );%0A        addVi
@@ -2501,20 +2501,52 @@
  int max
-Size
+Width;%0A        private int maxHeight
 ;%0A%0A     
@@ -2943,20 +2943,21 @@
 th %3E max
-Size
+Width
 )%0A      
@@ -2981,20 +2981,21 @@
      max
-Size
+Width
  = thisW
@@ -2995,24 +2995,193 @@
 = thisWidth;
+%0A%0A                        int thisHeight = btnChoice.getHeight();%0A                        if (thisHeight %3E maxHeight)%0A                            maxHeight = thisHeight;
 %0A           
@@ -3235,22 +3235,45 @@
 ile 
+(
 (max
-Size
+Width == 0) %7C%7C (maxHeight
  == 0)
+)
 ;%0A  
@@ -3538,20 +3538,21 @@
  if (max
-Size
+Width
  %3E 0)%0A  
@@ -3565,131 +3565,134 @@
     
-%7B%0A                if ((maxSize * 2) %3E item.getMeasuredWidth())%0A                    maxSize = (item.getMeasuredWidth() / 2);
+    for (Button btnChoice : item.btnChoices)%0A                    btnChoice.setWidth(maxWidth);%0A%0A            if (maxHeight %3E 0)
 %0A   
@@ -3782,37 +3782,27 @@
 .set
-Width(maxSize);%0A            %7D
+Height(maxHeight);%0A
 %0A   

Altered multiple choice buttons for height consistency