/*
 * $URL$
 * $Revision$
 * $LastChangedBy$
 * $LastChangedDate$
 *
 * Copyright (c) 2011 spherIC, Switzerland
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of spherIC. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you
 * entered into with spherIC.
 */
package ch.spherIC.recurvebowsight.components;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import android.os.SystemClock;

import android.util.AttributeSet;

import android.view.MotionEvent;

import ch.spherIC.recurvebowsight.R;


/**
 * DOCUMENT ME!
 *
 * @author   $author$
 * @version  $Revision$, $Date$
 */
public class XTextView extends FontTextView {

    private boolean drawborder;
    private int drawborderColor;
    private int drawborderWidth;

    /**
     * Creates a new XTextView object.
     *
     * @param  context  DOCUMENT ME!
     */
    public XTextView(final Context context) {
        super(context);
    }

    /**
     * Creates a new $class.name$ object.
     *
     * @param  context   DOCUMENT ME!
     * @param  attrs     DOCUMENT ME!
     * @param  defStyle  DOCUMENT ME!
     */
    public XTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    /**
     * Creates a new $class.name$ object.
     *
     * @param  context  DOCUMENT ME!
     * @param  attrs    DOCUMENT ME!
     */
    public XTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }


    /**
     * DOCUMENT ME!
     *
     * @param  attrs  DOCUMENT ME!
     */
    private void init(final AttributeSet attrs) {

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.XTextView);
        this.drawborder = a.getBoolean(R.styleable.XTextView_drawBorder, false);
        this.drawborderColor = a.getColor(R.styleable.XTextView_drawBorderColor, 0xffff0000);
        this.drawborderWidth = a.getDimensionPixelSize(R.styleable.XTextView_drawBorderWidth, 1);

        if (this.drawborder) {
            setPadding(getPaddingLeft() + this.drawborderWidth, getPaddingTop() + this.drawborderWidth, getPaddingRight() + this.drawborderWidth,
                       getPaddingBottom() + this.drawborderWidth);
        }
        a.recycle();
    }


    /**
     * @see  android.widget.TextView#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        if (this.drawborder) {
            Rect rect = new Rect();
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(this.drawborderColor);
            paint.setStrokeWidth(this.drawborderWidth);
            getLocalVisibleRect(rect);
            rect.bottom -= 1;
            rect.right -= 1;
            canvas.drawRect(rect, paint);
        }
    }


    /**
     * @param  drawborderColor  the drawborderColor to set
     */
    public void setDrawborderColor(final int drawborderColor) {
        this.drawborderColor = drawborderColor;
        invalidate();
    }

    /**
     * @return  the drawborder
     */
    public boolean isDrawborder() {
        return this.drawborder;
    }

    /**
     * @param  drawborder  the drawborder to set
     */
    public void setDrawborder(final boolean drawborder) {
        this.drawborder = drawborder;
        invalidate();
    }

    /**
     * @return  the drawborderWidth
     */
    public int getDrawborderWidth() {
        return this.drawborderWidth;
    }

    /**
     * @param  drawborderWidth  the drawborderWidth to set
     */
    public void setDrawborderWidth(final int drawborderWidth) {
        this.drawborderWidth = drawborderWidth;
        invalidate();
    }

    /**
     * @return  the drawborderColor
     */
    public int getDrawborderColor() {
        return this.drawborderColor;
    }


    public void doSingleTouch() {
        long now = SystemClock.uptimeMillis();
        MotionEvent evtDwn = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, 0, 0, 0);
        MotionEvent evtUp = MotionEvent.obtain(now, now + 1, MotionEvent.ACTION_UP, 0, 0, 0);
        dispatchTouchEvent(evtDwn);
        dispatchTouchEvent(evtUp);
        evtDwn.recycle();
        evtUp.recycle();
    }
}
