package fanch.multijeu.numbers.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.widget.LinearLayout;
import android.widget.TextView;

import fanch.multijeu.numbers.data.Expression;
import fanch.multijeu.numbers.data.NumberExpr;
import fanch.multijeu.numbers.data.OperationExpr;

public class ExpressionLayout extends LinearLayout {
    protected Expression mExpression;
    protected TextView mBefore;
    protected TextView mViewFirst;
    protected TextView mViewOp;
    protected TextView mViewSecond;
    protected TextView mViewEgal;
    protected TextView mViewResult;
    protected ExpressionLayout mFirst;
    protected ExpressionLayout mSecond;
    protected int mOldColor=DEFAULT_COLOR;

    protected static final int DEFAULT_COLOR = 0xff000000;
    protected static final int FIRST_COLOR   = 0xff00afaf;
    protected static final int SECOND_COLOR  = 0xff00af55;

    public ExpressionLayout(Context c, Expression e){
        super(c);
        mExpression =e;
        mViewResult = createTextView(c, mExpression.compute()+"");
        if(mExpression instanceof OperationExpr)
        {
            final OperationExpr ee = (OperationExpr) e;
            mFirst=new ExpressionLayout(c, ((OperationExpr)e).getFirst());
            mSecond=new ExpressionLayout(c, ((OperationExpr)e).getSecond());

            mBefore = createTextView(c, ee.getFirst().compute()+"");
            mViewFirst = createTextView(c, ee.getFirst().compute()+"");
            switch(ee.getOperande()){
                case ADD: mViewOp=createTextView(c," + "); break;
                case SUB: mViewOp=createTextView(c," - "); break;
                case MULT: mViewOp=createTextView(c," * "); break;
                case DIV: mViewOp=createTextView(c," / "); break;
            }
            mViewSecond = createTextView(c, ee.getSecond().compute()+"");
            mViewEgal = createTextView(c, " = ");

            addView(mViewFirst);
            addView(mViewOp);
            addView(mViewSecond);
            addView(mViewEgal);
        }
        addView(mViewResult);
    }



    protected static TextView createTextView(Context c, String s)
    {
        TextView tv = new TextView(c);
        tv.setText(s);
        return tv;
    }

    public void setHighlight(boolean b)
    {
        if(b) {
            if(mExpression instanceof OperationExpr)
            {
                mFirst.setHighlight(false);
                mSecond.setHighlight(false);
                mFirst.setBackgroundColor(FIRST_COLOR & 0x7fffffff);
                mSecond.setBackgroundColor(SECOND_COLOR& 0x7fffffff);
                mOldColor=mViewFirst.getCurrentTextColor();
                mViewFirst.setTextColor(FIRST_COLOR);
                mViewSecond.setTextColor(SECOND_COLOR);
                mViewFirst.setTypeface(null, Typeface.BOLD);
                mViewSecond.setTypeface(null, Typeface.BOLD);
            }
        } else {
            if(mExpression instanceof OperationExpr) {
                mFirst.setBackgroundColor(0);
                mSecond.setBackgroundColor(0);
                mViewFirst.setTextColor(mOldColor);
                mViewSecond.setTextColor(mOldColor);
                mViewFirst.setTypeface(null, Typeface.NORMAL);
                mViewSecond.setTypeface(null, Typeface.NORMAL);
            }else
            {
                setBackgroundColor(0);
            }
        }
    }

    public boolean isNumber()
    {
        return mExpression instanceof NumberExpr;
    }

    public ExpressionLayout getFirst()
    {
        return mFirst;
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setFinish()
    {
        GradientDrawable gd = new GradientDrawable();
        //gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
        //gd.setCornerRadius(5);
        gd.setStroke(3, 0xFFFF0000);
        setPadding(5,5,5,5);
        setBackground(gd);
    }

    public ExpressionLayout getSecond()
    {
        return mSecond;
    }


}
