package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
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 HFMainTabUnderlineView extends RelativeLayout {
    private View mViewUnderline;
    private TextView mTextViewAssigned;
    private TextView mTextViewRequest;
    private RelativeLayout.LayoutParams mParamAssigned;
    private RelativeLayout.LayoutParams mParamRequest;
    private int mAssignedWidth;

    private OnMainTabListener mOnMainTabListener;

    private final int DURATION = 200;
    private final int INDEX_TAB_ASSIGNED = 0;
    private final int INDEX_TAB_REQUEST = 1;
    private int mIndex = INDEX_TAB_ASSIGNED;

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

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

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

    public HFMainTabUnderlineView(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_main_tab_underline, this, true);

        mViewUnderline = (View) findViewById(R.id.underline_view);
        mTextViewAssigned = (TextView) findViewById(R.id.assignedTab_textView);
        mTextViewRequest = (TextView) findViewById(R.id.requestTab_textView);

        mTextViewAssigned.setOnClickListener(onClickListener);
        mTextViewRequest.setOnClickListener(onClickListener);

        mTextViewAssigned.measure(0, 0);
        mAssignedWidth = mTextViewAssigned.getMeasuredWidth();
        mTextViewRequest.measure(0, 0);
        int requestWidth = mTextViewRequest.getMeasuredWidth();

        int height = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp3);
        mParamAssigned = new RelativeLayout.LayoutParams(mAssignedWidth, height);
        mParamAssigned.addRule(ALIGN_PARENT_BOTTOM);
        mParamRequest = new RelativeLayout.LayoutParams(requestWidth, height);
        mParamRequest.addRule(ALIGN_PARENT_BOTTOM);

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

        mTextViewAssigned.setSelected(true);
        mTextViewRequest.setSelected(false);
    }

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

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

    public void onPageScrolled(int position, float positionOffset) {
        Log.d("onPageScrolled", "onPageScrolled ::: " + position + " , " + positionOffset);

        if (position == 0 && positionOffset > 0) {
            int margin = mAssignedWidth + HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp23);
            float transX = ((float) margin) * positionOffset;
            mViewUnderline.setTranslationX(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 assignedRed = unselectedRed - (unselectedRed - selectedRed) * (1.0f - positionOffset);
            float assignedGreen = unselectedGreen - (unselectedGreen - selectedGreen) * (1.0f - positionOffset);
            float assignedBlue = unselectedBlue - (unselectedBlue - selectedBlue) * (1.0f - positionOffset);

            float requestRed = selectedRed + (unselectedRed - selectedRed) * (1.0f - positionOffset);
            float requestGreen = selectedGreen + (unselectedGreen - selectedGreen) * (1.0f - positionOffset);
            float requestBlue = selectedBlue + (unselectedBlue - selectedBlue) * (1.0f - positionOffset);

            mTextViewAssigned.setTextColor(HFCommonUtil.getColor((int) assignedRed, (int) assignedGreen, (int) assignedBlue));
            mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) requestRed, (int) requestGreen, (int) requestBlue));
        } else {
//            mViewUnderline.setTranslationX(0);
        }
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.assignedTab_textView:
                    setSelected(INDEX_TAB_ASSIGNED);
                    break;
                case R.id.requestTab_textView:
                    setSelected(INDEX_TAB_REQUEST);
                    break;
            }
        }
    };

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