package kr.co.hanwha.hifive.view;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.TransitionDrawable;
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. 1. 22..
 */

public class HFMainTabView extends RelativeLayout {
    private View mViewSelected;
    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 HFMainTabView(Context context) {
        super(context);
        initialize(context, null);
    }

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

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

        mViewSelected = (View) findViewById(R.id.selected_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.main_tab_min_height);
        mParamAssigned = new LayoutParams(mAssignedWidth, height);
        mParamRequest = new LayoutParams(requestWidth, height);

        mViewSelected.setLayoutParams(mParamAssigned);
        mViewSelected.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);

        int width = mAssignedWidth + HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp10);

        if (index == INDEX_TAB_ASSIGNED) {
            mViewSelected.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mViewSelected.setLayoutParams(mParamAssigned);
                }
            }, DURATION / 2);

            mViewSelected.setTranslationX(width);
            mViewSelected.animate()
                    .translationX(0)
                    .setDuration(DURATION)
                    .start();
        } else if (index == INDEX_TAB_REQUEST) {
            mViewSelected.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mViewSelected.setLayoutParams(mParamRequest);
                }
            }, DURATION / 2);
            mViewSelected.setTranslationX(0);
            mViewSelected.animate()
                    .translationX(width)
                    .setDuration(DURATION)
                    .start();
        }

//        ColorDrawable[] color = {new ColorDrawable(Color.BLACK), new ColorDrawable(Color.WHITE)};
//        TransitionDrawable trans = new TransitionDrawable(color);
//        trans.startTransition(DURATION);
//        mTextViewMine.setTextColor(trans);

        /*
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), getResources().getColor(R.color.date_red)
                , getResources().getColor(R.color.date_grey));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                mTextViewAssigned.setTextColor((Integer) animator.getAnimatedValue());
            }

        });
        valueAnimator.start();
        */


        mViewSelected.postDelayed(new Runnable() {
            @Override
            public void run() {
                mTextViewAssigned.setSelected(index == INDEX_TAB_ASSIGNED);
                mTextViewRequest.setSelected(index == INDEX_TAB_REQUEST);
            }
        }, DURATION / 2);
    }

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

    /**************************************************
     * 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);
    }
}
