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.RelativeLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.util.HFCommonUtil;

public class HFMyHomeTabUnderlineView extends RelativeLayout {
    private View mViewUnderline;
    private TextView mTextViewOkr;
    private TextView mTextViewTodo;
    private TextView mTextViewRequest;
    private TextView mTextViewProfile;
    private LayoutParams mParamOkr;
    private LayoutParams mParamTodo;
    private LayoutParams mParamRequest;
    private LayoutParams mParamProfile;
    private int mOkrWidth;
    private int mTodoWidth;
    private int mRequestWidth;
    private int mProfileWidth;

    private OnMyHomeTabListener onMyHomeTabListener;

    private final int INDEX_TAB_OKR = 0;
    private final int INDEX_TAB_TODO = 1;
    private final int INDEX_TAB_REQUEST = 2;
    private final int INDEX_TAB_PROFILE = 3;
    private int mIndex = INDEX_TAB_OKR;

    public void setOnMyHomeTabListener(OnMyHomeTabListener onMyHomeTabListener) {
        this.onMyHomeTabListener = onMyHomeTabListener;
    }

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

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

    public HFMyHomeTabUnderlineView(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_my_home_tab_underline, this, true);

        mViewUnderline = (View) findViewById(R.id.underline_view);
        mTextViewOkr = (TextView) findViewById(R.id.okrTab_textView);
        mTextViewTodo = (TextView) findViewById(R.id.todoTab_textView);
        mTextViewRequest = (TextView) findViewById(R.id.requestTab_textView);
        mTextViewProfile = (TextView) findViewById(R.id.profileTab_textView);

        mTextViewOkr.setOnClickListener(onClickListener);
        mTextViewTodo.setOnClickListener(onClickListener);
        mTextViewRequest.setOnClickListener(onClickListener);
        mTextViewProfile.setOnClickListener(onClickListener);

        mTextViewOkr.measure(0, 0);
        mOkrWidth = mTextViewOkr.getMeasuredWidth();
        mTextViewTodo.measure(0, 0);
        mTodoWidth = mTextViewTodo.getMeasuredWidth();
        mTextViewRequest.measure(0, 0);
        mRequestWidth = mTextViewRequest.getMeasuredWidth();
        mTextViewProfile.measure(0, 0);
        mProfileWidth = mTextViewProfile.getMeasuredWidth();
        int height = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp3);

        mParamOkr = new LayoutParams(mOkrWidth, height);
        mParamOkr.addRule(ALIGN_PARENT_BOTTOM);
        mParamTodo = new LayoutParams(mTodoWidth, height);
        mParamTodo.addRule(ALIGN_PARENT_BOTTOM);
        mParamRequest = new LayoutParams(mRequestWidth, height);
        mParamRequest.addRule(ALIGN_PARENT_BOTTOM);
        mParamProfile = new LayoutParams(mProfileWidth, height);
        mParamProfile.addRule(ALIGN_PARENT_BOTTOM);

        mViewUnderline.setLayoutParams(mParamOkr);
        mViewUnderline.setTranslationX(0);

        mTextViewOkr.setSelected(true);
        mTextViewTodo.setSelected(false);
        mTextViewRequest.setSelected(false);
        mTextViewProfile.setSelected(false);
    }

    private void setSelected(final int index) {
        if (mIndex == index) return;
        mIndex = index;
        if (onMyHomeTabListener != null)
            onMyHomeTabListener.onTabSelected(index);
    }

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

    public void onPageScrolled(int position, float positionOffset) {
        float todoX = ((float) mOkrWidth + HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp18));
        float requestX = ((float) mOkrWidth + mTodoWidth + (HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp18) * 2));
        float profileX = ((float) mOkrWidth + mTodoWidth + mRequestWidth + (HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp18) * 3));
        float transX;
        final float selectedRed = 55;
        final float selectedGreen = 56;
        final float selectedBlue = 59;
        final float unselectedRed = 190;
        final float unselectedGreen = 192;
        final float unselectedBlue = 198;

        float goToUnselectedRed = unselectedRed - (unselectedRed - selectedRed) * (1.0f - positionOffset);
        float goToUnselectedGreen = unselectedGreen - (unselectedGreen - selectedGreen) * (1.0f - positionOffset);
        float goToUnselectedBlue = unselectedBlue - (unselectedBlue - selectedBlue) * (1.0f - positionOffset);

        float goToSelectedRed = selectedRed + (unselectedRed - selectedRed) * (1.0f - positionOffset);
        float goToSelectedGreen = selectedGreen + (unselectedGreen - selectedGreen) * (1.0f - positionOffset);
        float goToSelectedBlue = selectedBlue + (unselectedBlue - selectedBlue) * (1.0f - positionOffset);

        if (position == 0 && positionOffset > 0) {
            transX = todoX * positionOffset;
            mViewUnderline.setLayoutParams(mParamOkr);
            mViewUnderline.setTranslationX(transX);
            mViewUnderline.getLayoutParams().width = (int) (mOkrWidth + (mTodoWidth - mOkrWidth) * positionOffset);
            mTextViewOkr.setTextColor(HFCommonUtil.getColor((int) goToUnselectedRed, (int) goToUnselectedGreen, (int) goToUnselectedBlue));
            mTextViewTodo.setTextColor(HFCommonUtil.getColor((int) goToSelectedRed, (int) goToSelectedGreen, (int) goToSelectedBlue));
            mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewProfile.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
        } else if (position == 1 && positionOffset == 0) {
            mViewUnderline.setLayoutParams(mParamTodo);
            mViewUnderline.setTranslationX(todoX);
            mTextViewOkr.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewTodo.setTextColor(HFCommonUtil.getColor((int) selectedRed, (int) selectedGreen, (int) selectedBlue));
            mTextViewProfile.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
        } else if (position == 1 && positionOffset > 0) {
            transX = todoX + ((float) (mTodoWidth + (HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp18))) * positionOffset);
            mViewUnderline.setLayoutParams(mParamTodo);
            mViewUnderline.setTranslationX(transX);
            mViewUnderline.getLayoutParams().width = (int) (mTodoWidth + (mRequestWidth - mTodoWidth) * positionOffset);
            mTextViewOkr.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewTodo.setTextColor(HFCommonUtil.getColor((int) goToUnselectedRed, (int) goToUnselectedGreen, (int) goToUnselectedBlue));
            mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) goToSelectedRed, (int) goToSelectedGreen, (int) goToSelectedBlue));
            mTextViewProfile.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
        } else if (position == 2 && positionOffset == 0) {
            mViewUnderline.setLayoutParams(mParamRequest);
            mViewUnderline.setTranslationX(requestX);
            mTextViewOkr.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewTodo.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) selectedRed, (int) selectedGreen, (int) selectedBlue));
            mTextViewProfile.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));

        } else if (position == 2 && positionOffset > 0) {
            transX = requestX + ((float) (mRequestWidth + (HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp18))) * positionOffset);
            mViewUnderline.setLayoutParams(mParamRequest);
            mViewUnderline.setTranslationX(transX);
            mViewUnderline.getLayoutParams().width = (int) (mRequestWidth + (mProfileWidth - mRequestWidth) * positionOffset);
            mTextViewOkr.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewTodo.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) goToUnselectedRed, (int) goToUnselectedGreen, (int) goToUnselectedBlue));
            mTextViewProfile.setTextColor(HFCommonUtil.getColor((int) goToSelectedRed, (int) goToSelectedGreen, (int) goToSelectedBlue));

        } else if (position == 3 && positionOffset == 0) {
            mViewUnderline.setLayoutParams(mParamProfile);
            mViewUnderline.setTranslationX(profileX);
            mTextViewOkr.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewTodo.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) unselectedRed, (int) unselectedGreen, (int) unselectedBlue));
            mTextViewProfile.setTextColor(HFCommonUtil.getColor((int) selectedRed, (int) selectedGreen, (int) selectedBlue));
        }
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.okrTab_textView:
                    setSelected(INDEX_TAB_OKR);
                    break;
                case R.id.todoTab_textView:
                    setSelected(INDEX_TAB_TODO);
                    break;
                case R.id.requestTab_textView:
                    setSelected(INDEX_TAB_REQUEST);
                    break;
                case R.id.profileTab_textView:
                    setSelected(INDEX_TAB_PROFILE);
                    break;
            }
        }
    };

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