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.Button;

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

public class RevoButton extends Button {
    private boolean comboboxStyle = false;
    private AddEventDialog mainActivity;

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

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

    public RevoButton(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()) {
            setTypeface(ThemeHelper.get_primary_font());
            if (!comboboxStyle) {
                //if(isSelected()){
                //	setTextColor(context.getResources().getColor(R.color.black));
                //}
                //else
                setTextColor(context.getResources().getColorStateList(
                        ThemeHelper.get_button_color_selector()));
                setBackgroundResource(ThemeHelper
                        .get_button_selector());
                setTextSize((int) getResources().getDimension(R.dimen.calendar_header_text));

                if (getId() == R.id.button_tab1 || getId() == R.id.button_tab2 || getId() == R.id.button_tab3) {
                    setBackgroundResource(ThemeHelper
                            .get_component_tab());
                    setTextColor(context.getResources().getColorStateList(
                            ThemeHelper.get_component_tab_color()));
                    setTextSize((int) getResources().getDimension(R.dimen.tab_button_month_week_day));
                }


            } else {


                setTextColor(context.getResources().getColorStateList(
                        ThemeHelper.get_foreground_color()));
                setBackgroundResource(ThemeHelper
                        .get_component_combobox());

                setTextSize((int) getResources().getDimension(R.dimen.calendar_header_text));


            }

        }
    }


    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();
        }

    }


}