package fanch.multijeu.findwords.widget;

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

import java.util.ArrayList;

public class PlaceHolderRootLayout extends CustomLinearLayout {
    PlaceHolderListLayout mLeft;
    PlaceHolderListLayout mRight;
    protected boolean  mIsUpdating=false;

    public PlaceHolderRootLayout(Context context) {
        super(context);
        setOrientation(HORIZONTAL);
        //setLayoutParams(new LayoutParams(-1,-1));
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(-1,-1);
        lp.gravity = Gravity.CENTER;
        setLayoutParams(lp);
        mLeft = new PlaceHolderListLayout(getContext());
        mRight= new PlaceHolderListLayout(getContext());




        addView(mLeft);
        addView(mRight);
    }

    public void setWords(ArrayList<String> strs)
    {
        String[] s = new String[strs.size()];
        for(int i=0; i<s.length; i++)
            s[i]=strs.get(i);
        setWords(s);
    }
    public void setWords(String[] strs)
    {
        mLeft.clear();
        mRight.clear();
        int n = strs.length;
        if(n<5)
        {
            mRight.setVisibility(View.GONE);
            for(int i=0; i<n; i++) mLeft.addWord(strs[i]);
        }
        else
        {
            mRight.setVisibility(View.VISIBLE);
            int first = n-(n/2);
            for(int i=0; i<first; i++) mLeft.addWord(strs[i]);
            for(int i=first; i<n; i++) mRight.addWord(strs[i]);
        }
    }





    public PlaceHolderRootLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /*mLeft = new PlaceHolderListLayout(getContext());
        mRight= new PlaceHolderListLayout(getContext());
        addView(mLeft);
        addView(mRight);*/
    }


    @Override
    protected void onLayout(boolean changed, int ll, int tt, int rr, int b) {

        mLeft.measure(rr-ll, b-tt);
        mRight.measure(rr-ll, b-tt);
        final int l = mLeft.getMinButtonHeight();
        final int r = mRight.getMinButtonHeight();

        final int W = rr-ll;
        int marge = W/40;
        int size = (l>r)?r:l;
        mLeft.setMinButtonHeight(size);
        mRight.setMinButtonHeight(size);

        if(mRight.getVisibility()==View.VISIBLE) {
            mLeft.layout(marge, 0,
                    W / 2 - marge, b - tt);

            mRight.layout(W / 2 + marge, 0,
                    W - marge, b - tt);
        }else
        {
            mLeft.layout(marge*2, 0,
                    W  - marge*2, b - tt);
        }
        //super.onLayout(changed, ll, tt, rr, b);
    }

    public boolean hasNextTip()
    {
        return mLeft.hasNextTip() || mRight.hasNextTip();
    }

    public void nextTip()
    {
        if(mLeft.hasNextTip()) mLeft.nextTip();
        else if(mRight.hasNextTip()) mRight.nextTip();
    }


    public void discoverWord(String s, boolean accentSensitive)
    {
        mLeft.discoverWord(s, accentSensitive);
        mRight.discoverWord(s, accentSensitive);
    }
}
