package fanch.multijeu.findwords.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import java.util.ArrayList;

import fanch.multijeu.common.core.Word;

public class LetterWidget extends FrameLayout implements LetterView.LetterViewDriver {

    public interface LetterWidgetListener{
        void onTryWord(Word s);
        void onReady();
    }

    protected CircularLayout mLettersLayout;
    protected LetterView mOverlay;
    protected LetterWidgetListener mListener;
    protected boolean mLayoutIsReady=false;


    public LetterWidget(Context context) {
        super(context);
        mLettersLayout = new CircularLayout(context);
        mLettersLayout.setLayoutParams(new FrameLayout.LayoutParams(-1,-1));
        addView(mLettersLayout);
        mOverlay = new LetterView(context, this);
        addView(mOverlay);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                onLayoutReady();
            }
        });
    }


    public LetterWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLettersLayout = new CircularLayout(context);
        mLettersLayout.setLayoutParams(new FrameLayout.LayoutParams(-1,-1));
        addView(mLettersLayout);
        mOverlay = new LetterView(context, this);
        addView(mOverlay);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if(mLayoutIsReady)return;
                onLayoutReady();
                mLayoutIsReady=true;
            }
        });
    }

    private void onLayoutReady()
    {
        if(mListener!=null)
            mListener.onReady();
    }

    private void addLetter(char c)
    {
        LetterButton btn = new LetterButton(getContext());
        btn.setLetter(c);
        mLettersLayout.addView(btn);
    }

    public void setLetters(String s)
    {
        int w=getHeight();
        int h=getWidth();
        int minwh=(w>h)?h:w;
        int size=minwh/6;
        if(s.length()>8) size=minwh/8;
        if(s.length()>12) size=minwh/10;
        mLettersLayout.removeAllViews();
        for(int i=0; i<s.length(); i++)
            addLetter(s.charAt(i));
        mLettersLayout.setFixedSize(size);

    }

    public void setListener(LetterWidgetListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public LetterButton getLetterAt(int x, int y) {
        ArrayList<View> v =  mLettersLayout.getChildrenAt(x,y);
        for(int i=0; i<v.size(); i++)
            if(v.get(i) instanceof LetterButton)
                return (LetterButton) v.get(i);
        return null;
    }

    @Override
    public void onPathCompleted(String w) {
        if(mListener!=null)
            mListener.onTryWord(new Word(w));
    }
}
