package fanch.multijeu.findwords.fragments;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import fanch.multijeu.R;
import fanch.multijeu.findwords.fragments.model.SimpleWordViewViewModel;
import fanch.multijeu.findwords.widget.PlaceHolderRootLayout;

public class SimpleWordViewFragment extends Fragment {

    private SimpleWordViewViewModel mViewModel;

    public static SimpleWordViewFragment newInstance() {
        return new SimpleWordViewFragment();
    }

    protected ViewGroup mRoot;
    protected Bundle    mArgs;
    protected PlaceHolderRootLayout mLayout;

    protected static final String ARG_WORDLIST="wordList";

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.simple_word_view_fragment, container, false);

        mArgs=getArguments();
        mRoot=ret.findViewById(R.id.container);
        mLayout=new PlaceHolderRootLayout(getContext());
        mRoot.addView(mLayout);

        if(savedInstanceState==null)
        {
            mViewModel=new SimpleWordViewViewModel();
            if(mArgs!=null) doArgs();
        }else
        {
            init(savedInstanceState);
        }

        return ret;
    }

    protected void init(Bundle savedInstanceState) {
    }

    protected void doArgs()
    {
        ArrayList<String> x = mArgs.getStringArrayList(ARG_WORDLIST);
        setWordList(x);
    }

    public void setWordList(ArrayList<String> words)
    {
        mViewModel.mFound=new ArrayList<>();
        mViewModel.mTips=0;
        mViewModel.mWordList = words;
        mLayout.setWords(words);
    }

    public void nextTip()
    {
        mLayout.nextTip();
        mViewModel.mTips++;
    }


    public boolean hasNextTip()
    {
        return mLayout.hasNextTip();
    }

    public void discoverWord(String s, boolean accentSensitive)
    {
        mLayout.discoverWord(s, accentSensitive);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(SimpleWordViewViewModel.class);
    }

    public static SimpleWordViewFragment newInstance(ArrayList<String> wordList) {
        SimpleWordViewFragment f = new SimpleWordViewFragment();
        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putStringArrayList(ARG_WORDLIST, wordList);
        f.setArguments(args);
        return f;
    }


}
