package com.sheep.jiuyan.samllsheep.page.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  RappTabLayout</p>
 * @ <p>Description:</p>
 * @ date:  2017/12/26 0:05
 * @ QQ:    315096953
 */

public class RappTabLayout extends RelativeLayout {

    private ScrollerLine mScrollerLine;
    private LinearLayout mLayoutLine;
    private LinearLayout mLayoutMain;
    private int mCount = 0;
    private int mLastSelect = 0;
    private ViewPager mViewPager;//对应的ViewPager
    private boolean mIsInitLine = false;
    private List<TextView> mListTitle = new ArrayList<>();


    public RappTabLayout(Context context) {
        super(context);
        initView(context);
    }

    public RappTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public RappTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        if (mLayoutLine != null) {
            return;
        }
        mLayoutLine = new LinearLayout(context);
        mLayoutMain = new LinearLayout(context);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mLayoutLine.setLayoutParams(layoutParams);

        mLayoutMain.setOrientation(LinearLayout.HORIZONTAL);
        mLayoutMain.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT));
        mScrollerLine = new ScrollerLine(context);
        mScrollerLine.setBackgroundColor(Color.WHITE);
        mLayoutLine.addView(mScrollerLine);
    }

    public void setTitle(int position, String title) {
        mListTitle.get(position).setText(title);
    }


    /**
     * 只能调用一次。没去清空那些了。
     *
     * @param viewPager
     * @param resId     第一个子组件是TextView，设置SelectDrawable
     */
    public void setupWithViewPager(@NonNull ViewPager viewPager, @LayoutRes int resId) {
        mViewPager = viewPager;
        if (mViewPager.getAdapter() == null) {
            throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
        }
        mCount = mViewPager.getAdapter().getCount();
        mListTitle.clear();
        for (int i = 0; i < mCount; i++) {
            ViewGroup view = (ViewGroup) View.inflate(getContext(), resId, null);
            mListTitle.add((TextView) view.getChildAt(0));
            mListTitle.get(mListTitle.size() - 1).setText(mViewPager.getAdapter().getPageTitle(i));
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup
                    .LayoutParams.MATCH_PARENT);
            layoutParams.weight = 1;
            view.setLayoutParams(layoutParams);
            view.setOnClickListener(new clsTabSelect(i));
            mLayoutMain.addView(view);
        }

        viewPager.addOnPageChangeListener(cls_Pager);
        addView(mLayoutMain);
        addView(mLayoutLine);
        //默认第一个选中
        mScrollerLine.moveDegree(mLayoutLine, 0.001f, 0, mCount);
        mLayoutMain.getChildAt(0).setSelected(true);
    }


    public void selectTab(int postion) {
        mScrollerLine.moveDegree(mLayoutLine, 0.001f, postion, mCount);
        mLayoutMain.getChildAt(postion).setSelected(true);
        mViewPager.setCurrentItem(postion, false);
    }


    /**
     * 顶部Tab的监听
     */
    private class clsTabSelect implements OnClickListener {
        private int posiotion;

        public clsTabSelect(int pos) {
            posiotion = pos;
        }

        @Override
        public void onClick(View view) {
            if (mViewPager == null) {
                return;
            }
            mViewPager.setCurrentItem(posiotion);
        }
    }

    /**
     * 设置下滑线的高和宽
     *
     * @param width
     * @param heigh
     * @return
     */
    public RappTabLayout setLine(int width, int heigh) {
        mScrollerLine.setLayoutParams(new LinearLayout.LayoutParams(width, heigh));
        return this;
    }

    /**
     * 设置下滑线的margin值。一般用来做间隔
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @return
     */
    public RappTabLayout setLineMargin(int left, int top, int right, int bottom) {
        LinearLayout.LayoutParams layoutParams = ((LinearLayout.LayoutParams) mScrollerLine
                .getLayoutParams());
        layoutParams.setMargins(left, top, right, bottom);
        mScrollerLine.setLayoutParams(layoutParams);
        return this;
    }

    /**
     * 设置下滑线的颜色
     *
     * @param color
     * @return
     */
    public RappTabLayout setLineColor(int color) {
        mScrollerLine.setBackgroundColor(color);
        return this;
    }


    /**
     * 滑动监听
     */
    private ViewPager.OnPageChangeListener cls_Pager = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mScrollerLine.moveDegree(mLayoutLine, positionOffset, position, mCount);
        }

        @Override
        public void onPageSelected(int position) {
            mLayoutMain.getChildAt(mLastSelect).setSelected(false);
            mLayoutMain.getChildAt(position).setSelected(true);
            mLastSelect = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

}
