package fanch.multijeu.findwords.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import fanch.multijeu.common.core.Word;
import fanch.multijeu.common.exceptions.ForbiddenSuperCall;
import fanch.multijeu.findwords.data.dictionaire.Dictionaire;
import fanch.multijeu.findwords.data.TryState;
import fanch.multijeu.findwords.data.checker.WordChecker;
import fanch.multijeu.findwords.data.checker.WordCheckerList;

public abstract class AbstractWordActivity extends AppCompatActivity  {




    public static class WordActivityAttrs {
        public int viewResId=-1;
        public View view;
        public boolean useView=false;
        public WordCheckerList list;
        public Dictionaire dico;
        public boolean setContentView=true;


        public WordActivityAttrs(Dictionaire d){ dico=d; setContentView=false;}
        public WordActivityAttrs(Dictionaire d, int res) { dico=d;useView=false; viewResId=res; }
        public WordActivityAttrs(Dictionaire d, View v) { dico=d;useView=true; view=v; }

    }

    protected WordActivityAttrs mAttrs;
    private   WordChecker mChecker;
    protected WordCheckerList mCheckerList;

    protected void onCreate(Bundle savedInstanceState, WordActivityAttrs w) {
        super.onCreate(savedInstanceState);
        mAttrs = w;
        if(w.setContentView) {
            if(w.useView)
                setContentView(w.view);
            else
                setContentView(w.viewResId);
        }
        mCheckerList = mAttrs.list;
        mChecker =  mCheckerList.next(mAttrs.dico);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        throw new ForbiddenSuperCall("AbstractWordActivity::onCreate(Bundle) from "+this.getClass().toString());
    }

    public void setNewChecker(WordChecker w)
    {
        mChecker =w;
        onCheckerChange();
    }

    public abstract void onCheckerChange();

    public final WordChecker getChecker()
    {
        return mChecker;
    }

    public void onTryWord(Word w) {
        TryState ts = mChecker.tryWord(w);
        switch(ts)
        {
            case YES:
                onYes(w.toString());
                if(mChecker.expectedLeft()<=0) {
                    onDone();
                    if (mCheckerList.hasNext())
                        setNewChecker(mCheckerList.next(mAttrs.dico));
                    else
                        onFinish();
                }
                break;

            case YES_AGAIN: onYesAgain(w.toString()); break;
            case BONUS: onBonus(w.toString()); break;
            case BONUS_AGAIN: onBonusAgain(w.toString()); break;
            case NO: onNo(w.toString()); break;
        }
    }

    protected void onYes(String s){}
    protected void onYesAgain(String s){}
    protected void onBonus(String s){}
    protected void onBonusAgain(String s){}
    protected void onNo(String s){}
    protected abstract void onDone();
    protected abstract void onFinish();

}
