package fanch.multijeu.findwords.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import fanch.multijeu.common.utils.StringUtils;

public class PlaceHolderListLayout extends CustomLinearLayout implements View.OnClickListener {

    protected int mMinButtonHeight =0x7fffffff;

    public PlaceHolderListLayout(Context context) {
        super(context);
        setOrientation(VERTICAL);
        LayoutParams ll = new LayoutParams(-1,-1,1);
        ll.setMargins(0,0,0,0);
        setPadding(0,0,0,0);
        setLayoutParams(ll);

    }

    public PlaceHolderListLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        LayoutParams ll = new LayoutParams(-1,-2,1);
        ll.setMargins(0,0,0,0);
        setPadding(0,0,0,0);
        setLayoutParams(ll);

    }


    private PlaceHolderLayout children(int i)
    {
        return (PlaceHolderLayout)mChildren.get(i);
    }

    protected PlaceHolderLayout newLayout(String s)
    {
        PlaceHolderLayout pl = new PlaceHolderLayout(getContext());
        pl.setText(s);
        pl.setOnClickListener(this);
        return pl;
    }

    public void addWord(String w)
    {
        add(newLayout(w));
        requestLayout();
    }


    @Override
    protected void onMeasure(int ww, int hh) {
        final int W = ww&MEASURED_SIZE_MASK;
        final int H = hh&MEASURED_SIZE_MASK;
        final int count = mChildren.size();


        for(int i=0; i<count; i++) {
            final View child = children(i);
            child.measure(MeasureSpec.makeMeasureSpec(W, MeasureSpec.AT_MOST),
                    MeasureSpec.makeMeasureSpec(H/count, MeasureSpec.AT_MOST));
            final int x = children(i).getMeasuredHeight();
            if(x< mMinButtonHeight) mMinButtonHeight =x;
        }
        setMeasuredDimension(ww&MEASURED_SIZE_MASK, hh&MEASURED_SIZE_MASK);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(getHeight()==0) return;
        final int W = r-l;
        final int H = b-t;
        final int count = mChildren.size();


        int size=250000;
        for(int i=0; i<count; i++) {
            final View child = children(i);
            child.measure(MeasureSpec.makeMeasureSpec(W, MeasureSpec.AT_MOST),
                     MeasureSpec.makeMeasureSpec(H/count, MeasureSpec.AT_MOST));
            final int x = children(i).getMeasuredHeight();
            if(x<size) size=x;
        }

        if(size*count>H)
            size=H/count;

        if(size> mMinButtonHeight)
            size= mMinButtonHeight;

        int restant =H-count*size;

        int marge=0;
        if(restant<=0)
        {
            size= (int) (W/(count*1.5));
            marge=size/6;
        }else
        {
            marge = (restant)/(count+1);
        }

        int y = t+marge;
        for (int i = 0; i < count; i++) {
            final View child = children(i);

            child.layout(0, y, W, y+size);
            y+=size+marge;
        }
    }


    public void nextTip()
    {
        for(int i=0; i<mChildren.size(); i++)
            if(children(i).hasTip())
            {
                children(i).nextTip();
                return;
            }
    }

    public boolean hasNextTip()
    {
        for(int i=0; i<mChildren.size(); i++)
            if(children(i).hasTip()) return true;
        return false;
    }

    @Override
    public void onClick(View view) {

    }

    public void discoverWord(String s, boolean accentSensitive)
    {
        for(int i=0; i<mChildren.size(); i++)
        {
            if(accentSensitive)
            {
                if(children(i).getText().toUpperCase().equals(s.toUpperCase()))
                    children(i).show();
            }else
            {
                String a = StringUtils.toAscii(children(i).getText().toUpperCase());
                String b = StringUtils.toAscii(s.toUpperCase());
                if(a.equals(b))
                    children(i).show();
            }
        }
    }

    public void setMinButtonHeight(int x)
    {
        mMinButtonHeight =x;
    }


    public int getMinButtonHeight()
    {
        return mMinButtonHeight;
    }

}
