package fanch.multijeu.findwords.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import fanch.multijeu.R;
import fanch.multijeu.common.exceptions.BadParametterException;

public class PlaceHolderLayout extends ViewGroup implements View.OnClickListener {

    protected String    mText = "";
    protected boolean   mIsShown = false;
    protected int       mTipsCount = 0;
    protected int       mMaxSize = -1;

    public PlaceHolderLayout(Context context) {
        super(context);
        setOnClickListener(this);
    }

    public PlaceHolderLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.LetterButton, 0, 0);

        try {
            setText(a.getString(R.styleable.PlaceHolderButton_text));
        } finally {
            a.recycle();
        }
    }

    /*protected PlaceHolderButton newButton(char c)
    {
        PlaceHolderButton button = new PlaceHolderButton(getContext());
        button.setLetter(c);
        //button.setSize(50);
        button.setOnClickListener(this);
        return button;
    }
*/
    protected PlaceHolderButton newButton(char c)
    {
        PlaceHolderButton button = new PlaceHolderButton(getContext());
        button.setLetter(c);
        //button.setSize(50);
        button.setOnClickListener(this);
        return button;
    }
    @Override
    protected void onMeasure(int ww, int hh) {

        final int W = MeasureSpec.getSize(ww);
        final int WM = MeasureSpec.getMode(ww);

        final int H = MeasureSpec.getSize(hh);
        final int HM = MeasureSpec.getMode(hh);

        final int count = getChildCount();
        int wSpace=255555;
        int hSpace=255555;



        for(int i=0; i<count; i++)
        {
            final View child = getChildAt(i);
            child.measure(MeasureSpec.makeMeasureSpec(W, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(H, MeasureSpec.EXACTLY));
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            final int h = child.getMeasuredHeight();
            final int w = child.getMeasuredWidth();
            if(h<hSpace) hSpace=h;
            if(w<wSpace) wSpace=w;
        }

        if(hSpace>H)hSpace=H;
        if(wSpace*count>W) wSpace=W/count;

        //element need to be squares
        if(wSpace<hSpace) hSpace=wSpace;
        else wSpace=hSpace;

        Log.e("/////////", W+","+ hSpace);
        setMeasuredDimension(W, hSpace);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int W = r-l;
        final int H = b-t;
        final int count = getChildCount();
        int wSpace=255555;
        int hSpace=255555;
        int left = l;
        int pad;



        for(int i=0; i<count; i++)
        {
            final View child = getChildAt(i);
            child.measure(MeasureSpec.makeMeasureSpec(H, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(H, MeasureSpec.EXACTLY));

            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            final int h = child.getMeasuredHeight();
            final int w = child.getMeasuredWidth();
            if(h<hSpace) hSpace=h;
            if(w<wSpace) wSpace=w;
        }

        if(hSpace>H)hSpace=H;
        if(wSpace*count>W) wSpace=W/count;

        //element need to be squares
        if(wSpace<hSpace) hSpace=wSpace;
        else wSpace=hSpace;


        left+=(W-(wSpace*count))/2;
        for(int i=0; i<count; i++)
        {
            getChildAt(i).layout(left, 0,left+wSpace, wSpace );
            /*Log.e("/////////", "\t"+left+" "+t+" "+(left+wSpace)+" "+b
                                    +"  ("+wSpace+","+(b-t)+")" );*/
            left+=wSpace;
        }
    }


    public void onClick(View v)
    {
        requestLayout();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public void addView(View v)
    {
        if( !(v instanceof PlaceHolderButton) )
            throw new BadParametterException("Expect PlaceHolderButton here");
        super.addView(v);
    }

    protected int getButtonHeight()
    {
        if(getChildCount()==0) return 0;
        int min=25555;
        for(int i=0; i<getChildCount(); i++)
            if(getChildAt(i).getHeight()<min)
                min=getChildAt(i).getHeight();
        return min;
    }


    public void setText(String s)
    {
        if(s==null) s="";
        mText=s.toUpperCase();
        removeAllViews();
        for(int i=0; i<mText.length(); i++) {
            addView(newButton(mText.charAt(i)));
        }
    }

    public String getText() {
        return mText;
    }

    public boolean isIsShown() {
        return mIsShown;
    }

    public int getTipsCount() {
        return mTipsCount;
    }

    public void show()
    {
        for(int i=0; i<getChildCount(); i++)
            ((PlaceHolderButton)getChildAt(i)).setState(PlaceHolderButton.STATE_SHOWN);
        mIsShown=true;
        invalidate();
        requestLayout();
    }

    public void nextTip()
    {
        if(!hasTip()) return;
        ((PlaceHolderButton)getChildAt(mTipsCount)).setState(PlaceHolderButton.STATE_TIP);
        mTipsCount++;
        invalidate();
        requestLayout();
    }

    public boolean hasTip()
    {
        for(int i=0; i<getChildCount()-1; i++)
            if(!((PlaceHolderButton)getChildAt(i)).isDiscover())
                return true;
        return false;
    }
}
