package fanch.multijeu.findwords.data.dictionaire;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

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

public class Dictionaire extends Saveable {

    public enum Source {
        RESOURCE_RAW,
        APP_FILE,
        NON_APP_FILE
    }
    protected Source mSource;
    protected int mRes=-1;
    protected String mFile=null;
    protected DictionaireReader mReader;
    protected DictionaireIndex mIndex;

    public Dictionaire(Context c, int res) throws FileNotFoundException {
        mSource=Source.RESOURCE_RAW;
        mRes=res;
        mReader=new DictionaireReader(newInputStream(c));
        mIndex=mReader.getIndex();
    }

    public Dictionaire(Context c, String res) throws FileNotFoundException {
        mSource=Source.APP_FILE;
        mFile=res;
        mReader=new DictionaireReader(newInputStream(c));
        mIndex=mReader.getIndex();
    }

    public Dictionaire(String res) throws FileNotFoundException {
        mSource=Source.NON_APP_FILE;
        mFile=res;
        mReader=new DictionaireReader(newInputStream(null));
        mIndex=mReader.getIndex();
    }

    protected InputStream newInputStream(Context c) throws FileNotFoundException {
        switch (mSource)
        {
            case NON_APP_FILE:
            {
                FileInputStream fis;
                fis = new FileInputStream(mFile);
                return fis;
            }


            case APP_FILE:
            {
                FileInputStream fis = c.openFileInput(mFile);
                return fis;
            }

            case RESOURCE_RAW:
                return c.getResources().openRawResource(mRes);

            default:
                return null;
        }
    }

    public void resetStream(Context c, boolean close) throws FileNotFoundException {
        if(close) mReader.close();
        mReader=new DictionaireReader(newInputStream(c));
    }



    public ArrayList<Word> constructWith(String m_letters) {return mReader.constructWith(m_letters); }
    public ArrayList<Word> constructWithIgnoreAccent(String m_letters) { return mReader.constructWithIgnoreAccent(m_letters);    }


}
