package fanch.multijeu.findwords.data.dictionaire;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import fanch.multijeu.common.core.Word;
import fanch.multijeu.common.utils.IntUtils;
import fanch.multijeu.common.utils.StringUtils;


public class DictionaireReader{

    protected InputStream       m_is;

    protected int               m_depth=2;
    protected int               m_current;
    protected long              m_nchar=0;
    protected long              m_prevNchar=0;
    protected DictionaireIndex m_data;


    public DictionaireReader(Context c, int file)
    {
        m_is = c.getResources().openRawResource(file);

        m_data=new DictionaireIndex(m_depth);
        m_is.mark(100000000);
        doIndex();
    }

    public DictionaireReader(InputStream is, int depth)
    {
        m_is = is;
        m_depth=depth;
        m_data=new DictionaireIndex(m_depth);
        m_is.mark(100000000);
        doIndex();
    }

    public DictionaireReader(InputStream is)
    {
        m_is = is;
        m_data=new DictionaireIndex(m_depth);
        m_is.mark(100000000);
        doIndex();
    }

    public DictionaireReader(InputStream is, DictionaireIndex index)
    {
        m_is = is;
        m_is.mark(100000000);
        m_data=index;
    }

    public DictionaireIndex getIndex()
    {
        return m_data;
    }


    public long find(String s)
    {
        int min = m_depth<s.length()?m_depth:s.length();
        String prefix = s.substring(0, m_depth);
        long start = m_data.find(s);
        seek(start);

        Word iter;
        do{
            iter=readWord();
            if(iter.compareTo(s)==0) return m_prevNchar;
        }while(iter.indexOf(prefix)==0);
        return -1;
    }

    public ArrayList<Word> constructWith(String in)
    {
        ArrayList<Word> out = new ArrayList<Word>();
        seek(0);
        Word s;

        do {
            s=readWord();
            int n=0;

            if(s==null) break;
            if(in.length()<s.length()) continue;
            char word[] = s.toCharArray();


            for(int i=0; i<in.length(); i++){
                for(int j=0; j<word.length; j++) {
                    if (word[j] == in.charAt(i)) {
                        n++;
                        word[j]=0;
                        break;
                    }
                }
            }

            if(n==s.length())
            {
                out.add(s);
            }

        }while (s!=null && s.length()>0);
        return out;
    }

    public ArrayList<Word> constructWithIgnoreAccent(String iin)
    {
        ArrayList<Word> out = new ArrayList<Word>();
        seek(0);
        Word s;
        String in = StringUtils.toAscii(iin);

        do {
            s=readWord();
            int n=0;

            if(s==null) break;
            if(in.length()<s.length()) continue;
            char word[] = s.toAscii().toCharArray();


            for(int i=0; i<in.length(); i++){
                for(int j=0; j<word.length; j++) {
                    if (word[j] == in.charAt(i)) {
                        n++;
                        word[j]=0;
                        break;
                    }
                }
            }

            if(n==s.length())
            {
                out.add(s);
            }

        }while (s!=null && s.length()>0);
        return out;
    }




    protected boolean seek(long start)
    {
        try {
            m_is.reset();
            m_prevNchar=start;
            m_nchar=start;
            m_is.skip(start);

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    protected  void doIndex()
    {
        Word s;
        int  i=0;
        do {
            int a=0,b=0,c=0;
            s=readWord();
            if(s==null) break;
            m_data.set(m_prevNchar, s);
        }while (s!=null && s.length()>0);
        m_data.setCharLentgh((int) m_nchar);

    }

    protected void reset()
    {
        try {
            m_is.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }
        m_prevNchar=0;
        m_nchar=0;
    }





    protected int nextChar() {

        try {

            m_current=m_is.read();
            m_nchar++;

            return m_current;
        } catch (IOException e) {
            e.printStackTrace();
            return m_current=-1;
        }
    }

    protected int getCurrentChar(){return m_current;}


    public Word randWord()
    {
        int n = IntUtils.rand(0, m_data.getCharLentgh());
        seek(n);
        goToNextWord();
        return readWord();
    }

    protected void goToNextWord()
    {
        int i;
        m_prevNchar=m_nchar;
        String out = "";
        do {
            char c=(char)nextChar();
            if(c!='\n') out+=c;
        }while(getCurrentChar()!=-1 && getCurrentChar()!='\n');
    }

    public Word readWord()
    {
        int i;
         m_prevNchar=m_nchar;
        String out = "";
        do {
            char c=(char)nextChar();
            if(c!='\n') out+=c;
        }while(getCurrentChar()!=-1 && getCurrentChar()!='\n');
        if(out.length()==0 || getCurrentChar()==-1) return null;

        return  new Word(out);
    }

    public void close()
    {
        try {
            m_is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
