package fanch.multijeu.numbers.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.transition.Slide;
import android.support.transition.Transition;
import android.support.transition.TransitionManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import fanch.multijeu.numbers.data.Expression;
import fanch.multijeu.numbers.data.ExpressionSet;

public class CorrectionWidget extends FrameLayout implements CorrectionOverlay.CorrectionOverlayListener {

    protected LinearLayout mRootLayout;
    protected CorrectionOverlay mOverlay;
    protected ExpressionLayout mExpLayout;
    protected Expression mExpression;
    protected int        mStep=0;
    protected ArrayList<ExpressionLayout> mExpList = new ArrayList<>();
    protected ArrayList<ExpressionLayout> mNumberList = new ArrayList<>();
    protected CorrectionListener mListener;


    public interface CorrectionListener {
        void onDone();
        void onSend(Expression expr);
    }

    public CorrectionWidget(@NonNull Context context) {
        super(context);
        mRootLayout=new LinearLayout(getContext());
        mRootLayout.setOrientation(LinearLayout.VERTICAL);
        mOverlay = new CorrectionOverlay(getContext());
        mOverlay.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));

        addView(mOverlay);
        addView(mRootLayout);

    }

    public CorrectionWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mRootLayout=new LinearLayout(getContext());
        mRootLayout.setOrientation(LinearLayout.VERTICAL);
        mOverlay = new CorrectionOverlay(getContext());
        mOverlay.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
        mOverlay.setListener(this);
        addView(mOverlay);
        addView(mRootLayout);
    }

    protected void setExpression(ExpressionSet expr)
    {
        mExpression=expr.resultExpr;
        mExpLayout=new ExpressionLayout(getContext(), mExpression);
        mExpList.clear();
        mNumberList.clear();

        setExpressionList(mExpLayout);
        TextView correction = new TextView(getContext());
        correction.setText("Correction");
        correction.setGravity(Gravity.CENTER_HORIZONTAL);
        correction.setTextSize(30);

        mRootLayout.addView(correction);

        LinearLayout ll = new LinearLayout(getContext());
        for(int i=0; i<mNumberList.size(); i++)
        {

            TextView tv = new TextView(getContext());
            if(i==0) tv.setText("Nombres: ");
            else tv.setText(", ");
            ll.addView(tv);

            ll.addView(mNumberList.get(i));
        }
        mRootLayout.addView(ll);
        mStep=0;
    }

    protected void setExpressionList(ExpressionLayout exp)
    {
        if(!(exp.isNumber()))
        {
            setExpressionList(exp.getFirst());
            setExpressionList(exp.getSecond());
            mExpList.add(exp);
        }else{
            mNumberList.add(exp);
        }

    }



    protected void nextStep()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            if(mStep==mExpList.size())
            {
                mExpList.get(mStep-1).setFinish();
                mStep++;
                return;

            }else if(mStep>=mExpList.size())
            {
                onSkip();
                return;
            }
        }
        if(mStep==mExpList.size())
        {
            onSkip();
            return;
        }
        ExpressionLayout el = mExpList.get(mStep);
        LinearLayout ll = new LinearLayout(getContext());
        TextView tv = new TextView(getContext());
        tv.setText((mStep+1)+" : ");
        ll.addView(tv);
        ll.addView(el);
        el.setHighlight(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Transition t =  new Slide(Gravity.BOTTOM);
            t.setDuration(300);
            TransitionManager.beginDelayedTransition(mRootLayout, t);
        }
        mRootLayout.addView(ll);
        mStep++;

    }

    public void correction(ExpressionSet exp)
    {
        setExpression(exp);
        setVisibility(VISIBLE);
    }

    @Override
    public void onNext() {
        nextStep();
    }

    @Override
    public void onSkip() {
        mExpression=null;
        mStep=0;
        mExpList.clear();
        mNumberList.clear();
        mRootLayout.removeAllViews();
        setVisibility(GONE);
        if(mListener!=null)
            mListener.onDone();
    }

    public void setListener(CorrectionListener mListener) {
        this.mListener = mListener;
    }
}
