package fanch.multijeu.numbers.widget;

import android.content.Context;
import android.support.annotation.CallSuper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import fanch.multijeu.common.exceptions.BadChildClass;
import fanch.multijeu.numbers.data.Expression;
import fanch.multijeu.numbers.data.ExpressionParser;
import fanch.multijeu.numbers.data.NumberExpr;
import fanch.multijeu.numbers.data.OperationExpr;
import fanch.multijeu.numbers.data.ParentheseExpr;

public class NonAssistedCalcWidget extends CalcWidget {



    private CalcButton mPOpen;
    private CalcButton mPClose;
    protected ArrayList<Action> mHistory = new ArrayList();
    private String mCurrent="";

    private static class Action {
        public Action(String s, CalcButton b) {string=s; button=b;}
        public Action(String s) {string=s; button=null;}
        String string;
        CalcButton button;
    }

    public NonAssistedCalcWidget(Context context) {
        super(context);
        initControlButton();
    }

    public NonAssistedCalcWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        initControlButton();
    }

    public void initControlButton()
    {
        mMinGridHeight=5;
        super.initControlButton();
        mPOpen=new CalcButton(getContext());
        mPOpen.setOpen();
        mPOpen.setOnClickListener(this);

        mPClose=new CalcButton(getContext());
        mPClose.setClose();
        mPClose.setOnClickListener(this);


        mOperandes = new CalcButton[]{mAdd, mSub, mMult, mPClose, mPOpen};

        addView(mPOpen);
        addView(mPClose);

        updateShow();
    }

    public void pushHistory(CalcButton pressed)
    {
        if(pressed!=null && pressed.getExpression() instanceof NumberExpr)
            pressed.setEnable(false);
        else
            pressed=null;
        mHistory.add(new Action(mCurrent, pressed));
    }


    public void clearHistory()
    {
        mHistory.clear();
        for(int i=0; i<mNumbers.size(); i++)
            mNumbers.get(i).setEnable(true);
        mScreen.setText("");
        mCurrent="";
    }


    public void revert()
    {
        final int n = mHistory.size();
        if(n>0) {
            Action e = mHistory.get(n - 1);
            mCurrent = e.string;
            if(e.button!=null)
            {
                e.button.setEnable(true);
            }
            mHistory.remove(n - 1);
            updateText();
        }
    }



    public Expression getResult() {
        return getExpression();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        final CalcButton.CalcButtonContent cb = ((CalcButton)v).getContent();
        switch(cb.type)
        {
            case OTHER:
                if(cb.value instanceof ParentheseExpr)
                {
                    final ParentheseExpr pe = (ParentheseExpr) cb.value;
                    parenthese(((CalcButton)v), pe.isOpen());
                }
                break;
            case UNDO:
                revert();
                break;
        }
    }

    @Override
    protected void updateShow() {

    }

    @Override
    protected void clear() {
        clearHistory();
        updateText();
    }

    protected Expression getExpression()
    {
        //return ExpressionParser.parse(mScreen.getText()+"");
        String s = mScreen.getText()+"";
        Log.e("///////", "------>'"+s+"'");
        if( s==null || s.length()==0) return null;
        ExpressionParser parser = new ExpressionParser(s);
        Expression e = parser.parse();
        Log.e("///////", "---------->'"+e+"' = "+e.compute());
        Log.e("///////", "");
        return e;
    }

    @Override
    protected void valid() {
        Expression e = getExpression();
        if(e!=null)
        {
            if(mListener!=null)
                mListener.onExpressionDone(e);
        }

        if(mListener!=null)
            mListener.onValid();
    }

    @Override
    protected void number(CalcButton v) {
        pushHistory(v);
        mCurrent+=v.getExpression().compute();
        updateText();
    }

    @Override
    protected void operation(CalcButton v) {
        pushHistory(v);
        String c="";
        final OperationExpr e = (OperationExpr) v.getState().exp.value;
        switch(e.getOperande())
        {
            case ADD:c="+"; break;
            case MULT:c="*"; break;
            case DIV: c="*"; break;
            case SUB: c="-"; break;
        }
        mCurrent+=c;
        updateText();
    }

    protected void parenthese(CalcButton v, boolean open)
    {
        pushHistory(v);
        if(open)
            mCurrent+="(";
        else
            mCurrent+=")";
        updateText();
    }

    protected void updateText()
    {
        mScreen.setText(mCurrent);
    }

    public void setNumbers(int x[])
    {
        clearNumbers();
        clearHistory();
        mState=TypeState.IDLE;
        super.setNumbers(x);
        updateShow();
        mScreen.setText("");
        showNumber(0);
    }

    public void setNumbers(Expression x[])
    {
        clearNumbers();
        clearHistory();
        mState=TypeState.IDLE;
        super.setNumbers(x);
        updateShow();
        mScreen.setText("");
        showNumber(0);
    }

    @Override
    protected void onOperandeLayout(int w, int h) {
        for(int i=0; i<mOperandes.length-1; i++)
            mOperandes[i].setLayoutParams(new LayoutParams(3,i+1));
        final int x = mOperandes.length-1;
        mOperandes[x].setLayoutParams(new LayoutParams(2,x));
    }

}
