package fanch.multijeu.findwords.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;

public class CircularLayout extends ViewGroup{
    protected float mRadiusRatio= (float) 0.7;

    protected int mFixedSize=-1;

    public CircularLayout(Context context) {
        super(context);

    }

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

    public Button newButton()
    {
        Button b = new Button(getContext());
        b.setLayoutParams(new ViewGroup.LayoutParams(50,50) );
        return b;
    }

    public float getRadiusRatio() {
        return mRadiusRatio;
    }

    public void setRadiusRatio(float mRadiusRatio) {
        this.mRadiusRatio = mRadiusRatio;
    }

    public int getFixedSize() {
        return mFixedSize;
    }

    public void setFixedSize(int mFixedSize) {
        this.mFixedSize = mFixedSize;
        if(mFixedSize<=0) return;
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            child.setLayoutParams(new ViewGroup.LayoutParams(mFixedSize, mFixedSize));
        }
    }

    public class LayoutParams extends ViewGroup.LayoutParams
    {
        public int top=0;
        public int left=0;
        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(int x, int y, int width, int height) {
            super(width, height);
            top=y;
            left=x;
        }
    }

    protected void onLayout(boolean b, int left, int top, int right, int bottom) {
        int w = getWidth();
        int h = getHeight();
        int min = (w<h)?w:h;
        final int radius = (int) (0.5*min*mRadiusRatio);
        final int count = getChildCount();
        final double angle = 2*Math.PI/count;
        final int xmiddle = left+ (right-left)/2;
        final int ymiddle = top+(bottom-top)/2;

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                int width = child.getLayoutParams().width;
                int height = child.getLayoutParams().height;
                if(mFixedSize>0)
                {
                    width=height=mFixedSize;
                }
                final double cangle = angle*i;
                int childLeft;
                int childTop;

                childLeft = xmiddle+(int)(radius*Math.cos(i*angle-Math.PI/2))-width/2;
                childTop = ymiddle+(int)(radius*Math.sin(i*angle-Math.PI/2))-height/2;

                child.layout(childLeft, childTop, childLeft + width, childTop + height);
                //child.layout(0,0, 200,100);

            }
        }
    }

    ArrayList<View> getChildrenAt(int x, int y)
    {
        ArrayList<View> l = new ArrayList<>();
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            final int top = child.getTop();
            final int left = child.getLeft();
            final int right = child.getLeft()+child.getWidth();
            final int bottom = child.getTop()+child.getHeight();

            if( x>=left && x<=right && y>=top && y<=bottom)
            {
                l.add(child);
            }
        }
        return l;
    }



}
