package fanch.multijeu.numbers.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class CorrectionOverlay extends LinearLayout implements View.OnClickListener {

    protected Button mNext;
    protected Button mSkip;
    protected CorrectionOverlayListener mListener;



    public interface CorrectionOverlayListener {
        void onNext();
        void onSkip();
    }

    public CorrectionOverlay(Context context) {
        super(context);
        mNext=new Button(context);
        mNext.setText("Suivant");
        mNext.setOnClickListener(this);
        addView(mNext);

        mSkip=new Button(context);
        mSkip.setText("Passer");
        mSkip.setOnClickListener(this);
        addView(mSkip);
    }

    public CorrectionOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);
        mNext=new Button(context);
        mNext.setText("Suivant");
        mNext.setOnClickListener(this);
        addView(mNext);

        mSkip=new Button(context);
        mSkip.setText("Passer");
        mSkip.setOnClickListener(this);
        addView(mSkip);
    }

    @Override
    protected void onMeasure(int w, int h) {
        setMeasuredDimension(w&MEASURED_SIZE_MASK, h&MEASURED_SIZE_MASK);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int W = r-l;
        final int H = b-t;
        mNext.measure(MeasureSpec.makeMeasureSpec(W, MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec(H, MeasureSpec.AT_MOST));
        mSkip.measure(MeasureSpec.makeMeasureSpec(W, MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec(H, MeasureSpec.AT_MOST));
        setFrame(mNext,W-mNext.getMeasuredWidth()-16, H-mNext.getMeasuredHeight()-16,
                mNext.getMeasuredWidth(), mNext.getMeasuredHeight());

        setFrame(mSkip,16, H-mSkip.getMeasuredHeight()-16,
                mSkip.getMeasuredWidth(), mSkip.getMeasuredHeight());
    }

    protected void setFrame(View c, int x, int y, int w, int h)
    {
        c.layout(x,y,x+w, y+h);
    }

    @Override
    public void onClick(View v) {
        if(v==mSkip && mListener!=null) mListener.onSkip();
        if(v==mNext && mListener!=null) mListener.onNext();
    }

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