package fanch.multijeu.findwords.data.dictionaire;

import android.content.Context;

import java.io.InputStream;

import fanch.multijeu.common.core.Saveable;
import fanch.multijeu.common.core.Word;

public class DictionaireIndex extends Saveable {

    protected int mCharLentgh;

    public DictionaireIndex()
    {
    }

    DictionaireIndex(int depth)
    {
        if(depth>0)
        {
            child=new DictionaireIndex[27];
            for(int i=0; i<27; i++)
                child[i]=new DictionaireIndex(depth-1);
        }
    }

    int getIndex(char x)
    {
        if(x>='a' && x<='z') return x-'a'+1;
        if(x>='A' && x<='Z') return x-'A'+1;
        return 0;
    }

    void set(long ind, Word s, int offset)
    {
        if(index<0) index=ind;
        if(s.length()>offset && child!=null)
            child[getIndex(s.charAt(offset))].set(ind, s, offset+1);
    }

    void set(long ind, Word s)
    {
        set(ind, s,0);
    }

    long find(String s)
    {
        return find(s, 0);
    }

    long find(String s, int offset)
    {
        if(offset<s.length() && child!=null)
            return child[getIndex(s.charAt(offset))].find(s, offset+1);
        return index;
    }


    DictionaireIndex child[]=null;
    long index=-1;

    public DictionaireReader newDictionaireReader(InputStream is)
    {
        return new DictionaireReader(is, this);
    }


    public DictionaireReader newDictionaireReader(Context c, int res)
    {
        return new DictionaireReader(c.getResources().openRawResource(res));
    }

    public int getCharLentgh() {
        return mCharLentgh;
    }

    public void setCharLentgh(int mCharLentgh) {
        this.mCharLentgh = mCharLentgh;
    }
}
