package fanch.multijeu.sudoku.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.Button;

import fanch.multijeu.common.view.AutoSizeButton;

public class SudokuCase extends android.support.v7.widget.AppCompatButton {

    public static final int COLOR_LOCK=0xffcccccc;
    public static final int COLOR_EMPTY=0x80ffffff;
    public static final int COLOR_ERROR=0xffff0000;
    public static final int COLOR_VALUE=0x80ffffff;
    public static final int COLOR_INDICE=0xfffff7ab;

    public static enum CaseState{
        EMPTY,
        LOCKED,
        ERROR,
        INDICE,
        VALUE
    }

    protected CaseState mState=CaseState.EMPTY;
    protected int       mValue=0;


    public SudokuCase(Context context) {
        super(context);
        updateUi();
        setPadding(0,0,0,0);
    }

    public SudokuCase(Context context, AttributeSet attrs) {
        super(context, attrs);
        updateUi();
    }


    public CaseState getState() {
        return mState;
    }

    public void setState(CaseState mState) {
        this.mState = mState;
        updateUi();
    }

    public int getValue() {
        return mValue;
    }

    public void setValue(int mValue) {
        this.mValue = mValue;
        if(mValue>0 && mValue<=9)
            mState=CaseState.VALUE;
        else if(mValue==0 && mState!=CaseState.LOCKED)
            mState=CaseState.EMPTY;
        updateUi();
    }

    protected void updateUi()
    {
        switch(mState)
        {
            case EMPTY:
                setBackgroundColor(COLOR_EMPTY);
                setText("");
                mValue=0;
                break;
            case LOCKED:
                setBackgroundColor(COLOR_LOCK);
                setText(""+mValue);
                break;
            case VALUE:
                setBackgroundColor(COLOR_VALUE);
                setText(""+mValue);
                break;
            case INDICE:
                setBackgroundColor(COLOR_INDICE);
                setText(""+mValue);
                break;
            case ERROR:
            default:
                setBackgroundColor(COLOR_ERROR);
                setText(""+mValue);
                break;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if(changed)
        {
            setTextSize(TypedValue.COMPLEX_UNIT_PX,(float) ((bottom-top)*0.7));
        }
        super.onLayout(changed, left, top, right, bottom);
    }
}
