package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 3. 15..
 */

public class HFDashboardTabView extends RelativeLayout {
    private ImageButton mImageButtonOrange;
    private ImageButton mImageButtonGreen;

    private OnMainTabListener mOnMainTabListener;

    private final int DURATION = 200;
    private final int INDEX_TAB_ORANGE = 0;
    private final int INDEX_TAB_GREEN = 1;
    private int mIndex = INDEX_TAB_ORANGE;

    public void setOnMainTabListener(OnMainTabListener onMainTabListener) {
        mOnMainTabListener = onMainTabListener;
    }

    public HFDashboardTabView(Context context) {
        super(context);
        initialize(context, null);
    }

    public HFDashboardTabView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public HFDashboardTabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        if (isInEditMode()) return;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_dashboard_tab, this, true);

        mImageButtonOrange = (ImageButton) findViewById(R.id.orange_imageButton);
        mImageButtonGreen = (ImageButton) findViewById(R.id.green_imageButton);

        mImageButtonOrange.setOnClickListener(onClickListener);
        mImageButtonGreen.setOnClickListener(onClickListener);

        mImageButtonOrange.setSelected(true);
        mImageButtonGreen.setSelected(false);
    }

    private void setSelected(final int index) {
        if (mIndex == index) return;
        mIndex = index;

        mImageButtonOrange.setSelected(index == INDEX_TAB_ORANGE);
        mImageButtonGreen.setSelected(index == INDEX_TAB_GREEN);

        if (mOnMainTabListener != null)
            mOnMainTabListener.onTabSelected(index);
    }

    public void setCurrentItem(int index) {
        setSelected(index);
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.orange_imageButton:
                    setSelected(INDEX_TAB_ORANGE);
                    break;
                case R.id.green_imageButton:
                    setSelected(INDEX_TAB_GREEN);
                    break;
            }
        }
    };

    /**************************************************
     * OnReceiverContainerListener
     **************************************************/
    public interface OnMainTabListener {
        void onTabSelected(int position);
    }
}
