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;

/**
 * Created by sanghoonkwon on 2018. 2. 9..
 */

public class HFMainTabFontView extends RelativeLayout {
    private TextView mTextViewAssigned;
    private TextView mTextViewRequest;
    private LayoutParams mParamAssigned;
    private 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 HFMainTabFontView(Context context) {
        super(context);
        initialize(context, null);
    }

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

    public HFMainTabFontView(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_font, this, true);

        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.main_tab_min_height);
        mParamAssigned = new LayoutParams(mAssignedWidth, height);
        mParamRequest = new LayoutParams(requestWidth, height);


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

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

        /*
        final float smallSize = 13;
        final float largeSize = 28;
        long animationDuration = 200;

        if (index == INDEX_TAB_ASSIGNED) {
            ValueAnimator animAssigned = ValueAnimator.ofFloat(smallSize, largeSize);
            animAssigned.setDuration(animationDuration);
            animAssigned.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    float animatedValue = (float) valueAnimator.getAnimatedValue();
                    Log.d("animatedValue", "animatedValue : " + animatedValue);
                    mTextViewAssigned.setTextSize(animatedValue);
                }
            });

            ValueAnimator animRequest = ValueAnimator.ofFloat(largeSize, smallSize);
            animRequest.setDuration(animationDuration);
            animRequest.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    float animatedValue = (float) valueAnimator.getAnimatedValue();
                    mTextViewRequest.setTextSize(animatedValue);
                }
            });

            AnimatorSet animSet = new AnimatorSet();
            animSet.playTogether(animAssigned, animRequest);
            animSet.start();
        } else if (index == INDEX_TAB_REQUEST) {
            ValueAnimator animAssigned = ValueAnimator.ofFloat(largeSize, smallSize);
            animAssigned.setDuration(animationDuration);
            animAssigned.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    float animatedValue = (float) valueAnimator.getAnimatedValue();
                    mTextViewAssigned.setTextSize(animatedValue);
                }
            });

            ValueAnimator animRequest = ValueAnimator.ofFloat(smallSize, largeSize);
            animRequest.setDuration(animationDuration);
            animRequest.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    float animatedValue = (float) valueAnimator.getAnimatedValue();
                    mTextViewRequest.setTextSize(animatedValue);
                }
            });

            AnimatorSet animSet = new AnimatorSet();
            animSet.playTogether(animAssigned, animRequest);
            animSet.start();
        }
        */
    }

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

    public void onPageScrolled(int position, float positionOffset) {
        final float smallSize = 13;
        final float largeSize = 27;

        if (position == 0) {
            if (positionOffset <= 0) {
                mTextViewAssigned.setTextSize(largeSize);
                mTextViewRequest.setTextSize(smallSize);
            } else {
                float assignedFont = smallSize + (largeSize - smallSize) * (1.0f - positionOffset);
                if (assignedFont < smallSize) assignedFont = smallSize;
                else if (assignedFont > largeSize) assignedFont = largeSize;

                float requestFont = smallSize + (largeSize - smallSize) * positionOffset;
                if (requestFont < smallSize) requestFont = smallSize;
                else if (requestFont > largeSize) requestFont = largeSize;

                mTextViewAssigned.setTextSize(assignedFont);
                mTextViewRequest.setTextSize(requestFont);

                final float largeRed = 49;
                final float largeGreen = 50;
                final float largeBlue = 53;
                final float smallRed = 74;
                final float smallGreen = 75;
                final float smallBlue = 81;

                float assignedRed = smallRed - (smallRed - largeRed) * (1.0f - positionOffset);
                float assignedGreen = smallGreen - (smallGreen - largeGreen) * (1.0f - positionOffset);
                float assignedBlue = smallBlue - (smallBlue - largeBlue) * (1.0f - positionOffset);

                float requestRed = largeRed + (smallRed - largeRed) * (1.0f - positionOffset);
                float requestGreen = largeGreen + (smallGreen - largeGreen) * (1.0f - positionOffset);
                float requestBlue = largeBlue + (smallBlue - largeBlue) * (1.0f - positionOffset);

                mTextViewAssigned.setTextColor(HFCommonUtil.getColor((int) assignedRed, (int) assignedGreen, (int) assignedBlue));
                mTextViewRequest.setTextColor(HFCommonUtil.getColor((int) requestRed, (int) requestGreen, (int) requestBlue));
            }
        } else {
            mTextViewAssigned.setTextSize(smallSize);
            mTextViewRequest.setTextSize(largeSize);
        }
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new 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);
    }
}
