package com.revoapp.calendar.components;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.ImageButton;

import com.revoapp.calendarapp.R;
import com.revoapp.calendar.dialogs.AddEventDialog;
import com.revoapp.calendar.helper.ThemeHelper;

public class RevoImageButton extends ImageButton {
    private boolean comboboxStyle = false;
    private AddEventDialog mainActivity;

    public RevoImageButton(Context context) {
        super(context);
        setAttributes(context);
    }

    public RevoImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        obrainAttributes(context, attrs);
        setAttributes(context);
    }

    public RevoImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        obrainAttributes(context, attrs);
        setAttributes(context);
    }

    private void obrainAttributes(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.RevoButton);

        final int N = a.getIndexCount();
        for (int i = 0; i < N; ++i) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.RevoButton_comboboxStyle:
                    comboboxStyle = a.getBoolean(attr, false);
                    break;
            }
        }
        a.recycle();
    }

    private void setAttributes(Context context) {
        if (!isInEditMode()) {

            if (!comboboxStyle) {


                setBackgroundResource(ThemeHelper
                        .get_button_selector());


                if (getId() == R.id.button_tab1 || getId() == R.id.button_tab2 || getId() == R.id.button_tab3) {
                    setBackgroundResource(ThemeHelper
                            .get_component_tab());

                }


            }


        }
    }


    public void setSampleLongpress(AddEventDialog act) {
        mainActivity = act;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        cancelLongpressIfRequired(event);
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        cancelLongpressIfRequired(event);
        return super.onTrackballEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
                || (keyCode == KeyEvent.KEYCODE_ENTER)) {
            cancelLongpress();
        }
        return super.onKeyUp(keyCode, event);
    }

    private void cancelLongpressIfRequired(MotionEvent event) {
        if ((event.getAction() == MotionEvent.ACTION_CANCEL)
                || (event.getAction() == MotionEvent.ACTION_UP)) {
            cancelLongpress();
        }
    }

    private void cancelLongpress() {
        if (mainActivity != null) {
            mainActivity.cancelLongPress();
        }

    }


}