package fanch.multijeu.findwords.activity;

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

import java.io.Serializable;

import fanch.multijeu.R;
import fanch.multijeu.common.core.SerializedObject;
import fanch.multijeu.common.core.Word;
import fanch.multijeu.findwords.data.TryState;
import fanch.multijeu.findwords.data.dictionaire.Dictionaire;
import fanch.multijeu.findwords.data.checker.WordChecker;
import fanch.multijeu.findwords.data.checker.WordCheckerList;
import fanch.multijeu.findwords.fragments.ComposeFragment;
import fanch.multijeu.findwords.fragments.SimpleWordViewFragment;
import fanch.multijeu.findwords.widget.LetterWidget;

public class WordActivity extends AppCompatActivity  implements LetterWidget.LetterWidgetListener {

    protected static final String ARG_DICT="dictionnaire";
    protected static final String ARG_WORD_CHECKER_LIST="wordCheckerList";

    protected SimpleWordViewFragment mView;
    protected ComposeFragment       mCompose;




    protected class Model {
        protected Dictionaire                 dict;
        protected WordCheckerList             factory;
        protected WordChecker                 current;
    }

    private enum DataHolder {
        INSTANCE;

        private Object mObject;

        public static boolean hasData() {
            return INSTANCE.mObject != null;
        }

        public static void setData(final Object object) {
            INSTANCE.mObject = object;
        }

        public static Object getData() {
            final Object retList = INSTANCE.mObject;
            INSTANCE.mObject = null;
            return retList;
        }
    }

    protected Model m = new Model();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       Intent in = getIntent();
        if(in.hasExtra(ARG_WORD_CHECKER_LIST)) {
            m.factory = (WordCheckerList) ((SerializedObject) in.getSerializableExtra(ARG_WORD_CHECKER_LIST)).newObject();
        }

        if(DataHolder.hasData())
            m.dict= (Dictionaire) DataHolder.getData();

        m.current=m.factory.next(m.dict);
        setContentView(R.layout.activity_word);

        mCompose=(ComposeFragment)getSupportFragmentManager().findFragmentById(R.id.composeFragment);
        if (savedInstanceState == null) {

            getSupportFragmentManager().beginTransaction().replace(R.id.view_container,
                    mView=SimpleWordViewFragment.newInstance(m.current.getExpected().keys())).commitNow();
        }
    }

    public static void start(Context c, Dictionaire d, WordCheckerList w)
    {
        Intent in = new Intent(c, WordActivity.class);
        DataHolder.setData(d);
        SerializedObject<WordCheckerList> m = w.saveToSerializedObject();
        in.putExtra(ARG_WORD_CHECKER_LIST, (Serializable) m);

        c.startActivity(in);
    }


    @Override
    public void onReady() {
        mCompose.setLetters(m.current.getLetters());
    }

    @Override
    public void onTryWord(Word w) {
        TryState ts = m.current.tryWord(w);
        switch(ts)
        {
            case YES:
                onYes(w.toString());
                if(m.current.expectedLeft()<=0) {
                    onDone();
                    if (m.factory.hasNext()) {
                        setNewChecker(m.factory.next(m.dict));
                    }
                    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;
        }
    }

    private void setNewChecker(WordChecker next) {
        m.current=next;
        mCompose.setLetters(next.getLetters());
        mView.setWordList(next.getExpected().keys());
    }

    public void onTip(View view) {
        mView.nextTip();
    }

    private void onYes(String s) {
        mView.discoverWord(s,true);
    }

    private void onYesAgain(String s) {
    }

    private void onBonus(String s) {
    }

    private void onBonusAgain(String s) {
    }

    private void onNo(String s) {
    }

    private void onFinish() {
    }

    private void onDone() {
    }


}
