package anabalica.github.io.meowletters.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TableRow.LayoutParams;

import java.util.HashMap;

import anabalica.github.io.meowletters.R;

/**
 * SquareButton width and height are set according to the CustomTableRow width
 * and custom attribute columnsCount, by dividing CustomTableRow width to the
 * columnsCount.
 *
 * @author Ana Balica
 */
public class SquareButton extends Button {
    private Integer row;
    private Integer column;
    private HashMap<String, Integer> position = new HashMap<>(2);
    LayoutParams params = new LayoutParams();

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray attributes = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.SquareButton, 0, 0);

        try {
            row = attributes.getInteger(R.styleable.SquareButton_row, 0);
            column = attributes.getInteger(R.styleable.SquareButton_column, 0);
        } finally {
            attributes.recycle();
        }
        position.put("row", row);
        position.put("column", column);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        CustomTableRow parent = (CustomTableRow) getParent();
        final int parentWidth = parent.getMeasuredWidth();
        final int columnsCount = parent.getColumnsCount();
        // TODO: adjust the margin according to the screen resolution
        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        final int width = (parentWidth / columnsCount) - margin * 2;

        setMeasuredDimension(width, width);
        params.setMargins(margin, margin, margin, margin);
        setLayoutParams(params);
    }

    public int getColumn() {
        return column;
    }

    public int getRow() {
        return row;
    }

    public HashMap<String, Integer> getPosition() {
        return position;
    }
}

@@ -152,16 +152,52 @@
 uteSet;%0A
+import android.util.DisplayMetrics;%0A
 import a
@@ -254,46 +254,8 @@
 on;%0A
-import android.widget.RelativeLayout;%0A
 impo
@@ -616,16 +616,188 @@
 utton %7B%0A
+    public static final int THRESHOLD_TABLETS = 600;%0A    public static final int SMALL_MARGIN = 2;%0A    public static final int AVERAGE_MARGIN = 4;%0A%0A    private int margin;%0A
     priv
@@ -1047,16 +1047,57 @@
  attrs);
+%0A        margin = computeMargin(context);
 %0A%0A      
@@ -1869,200 +1869,8 @@
 ();%0A
-        // TODO: adjust the margin according to the screen resolution%0A        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());%0A
     
@@ -2070,32 +2070,750 @@
 params);%0A    %7D%0A%0A
+    /**%0A     * Compute the margin of a single square button in the runtime according to the device width%0A     *%0A     * @param context Context object%0A     * @return int margin in px units%0A     */%0A    private int computeMargin(Context context) %7B%0A        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();%0A        int dpWidth = Math.round(displayMetrics.widthPixels / displayMetrics.density);%0A%0A        int dpMargin = 0;%0A        if (dpWidth %3C THRESHOLD_TABLETS) %7B%0A            dpMargin = SMALL_MARGIN;%0A        %7D else %7B%0A            dpMargin = AVERAGE_MARGIN;%0A        %7D%0A        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpMargin, getResources().getDisplayMetrics());%0A    %7D%0A%0A
     public int g

determine the SquareButton margin in the runtime