package app.brest.app.brest.ui;

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

import app.brest.testmin3d.R;

/**
 * Created by ptitcois on 31/10/16.
 */
public class CustomToggleButton extends Button implements View.OnClickListener {

    protected boolean mState=false;
    protected OnToggleListener mListener = null;


    public CustomToggleButton(Context context, boolean state) {
        super(context);
        mState=state;
        init();
    }

    public CustomToggleButton(Context context) {
        super(context);
        init();
    }

    public CustomToggleButton(Context context, boolean state, OnToggleListener l) {
        super(context);
        mState=state;
        mListener=l;
        init();

    }

    public CustomToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected void init()
    {
        super.setOnClickListener(this);
        setHeight(50);
        update();
    }

    protected void update()
    {
        if(mState) onOn();
        else onOff();
    }

    protected void onOn()
    {
        setText("On");
        setTextColor(getResources().getColor(R.color.dull_4));
        setBackgroundResource(R.drawable.togglebutton_on);
        setHeight(50);
        if(mListener!=null) mListener.onOn(this);
    }

    protected void onOff()
    {
        setText("Off");
        setTextColor(getResources().getColor(R.color.dull_4));
        setBackgroundResource(R.drawable.togglebutton_off);
        setHeight(50);

        if(mListener!=null) mListener.onOff(this);
    }

    public boolean getState()
    {
        return mState;
    }

    public void setState(boolean s)
    {
        mState=s;
        update();
    }


    public void setOnClickListener(app.brest.app.brest.ui.OnToggleListener l)
    {
        mListener=l;
    }

    @Override
    public void onClick(View view) {
        mState=!mState;
        update();
        if(mListener!=null) mListener.onToggle(this);
    }
}
