package fanch.multijeu.findwords.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;

import fanch.multijeu.R;

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


    protected char mLetter;
    protected Animation mShake;

    public LetterButton(Context context) {
        super(context);
        setLetterBackground();

        setGravity(Gravity.CENTER);
        setPadding(0,0,0,0);

    }


    public LetterButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLetterBackground();
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.LetterButton, 0, 0);
        try {
            setLetter(a.getString(R.styleable.LetterButton_letter).charAt(0));
        } finally {
            a.recycle();
        }

    }


    protected void setLetterBackground()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            setBackground(getResources().getDrawable(R.drawable.lettre));
        }
    }


    public void setLetter(char l) {
        this.mLetter = l;
        super.setText(""+mLetter);
    }

    public char getLetter() {
        return mLetter;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        final int w = right - left;
        final int h = bottom -top;
        final int size = (w<h)?w:h;
        setTextSize(TypedValue.COMPLEX_UNIT_PX, (int) (((double)size)*0.75));
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void shake() {
        if(mShake==null || mShake.hasEnded()) {
            mShake = AnimationUtils.loadAnimation(getContext(), R.anim.shake);

            startAnimation(mShake);
        }
    }

/*
    @Override
    protected void onMeasure(int ww, int hh) {
        final int w = ww&MEASURED_SIZE_MASK;
        final int h = hh&MEASURED_SIZE_MASK;
        final int size = (w<h)?w:h;
        setMeasuredDimension(size,size);
    }*/



}
