package com.genlot.station.viewpagertest.pager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.genlot.station.viewpagertest.BuildConfig;
import com.genlot.station.viewpagertest.R;
import com.itant.indicator.ColorfulIndicatorAdapter;
import com.itant.indicator.IPagerFuture;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.IndicatorViewPager;

import java.util.List;

public abstract class FlowActivity extends AppCompatActivity implements IPagerFuture {
    private final String TAG = getClass().getName();

    private TextView tv_title;
    private Indicator mIndicator;
    private List<Fragment> mFragmentList;
    /**
     * 带有指示器和Fragment的adapter
     */
    private ColorfulIndicatorAdapter mIndicatorAdapter;
    private List<String> mTitleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow);
        tv_title = findViewById(R.id.tv_title);

        mFragmentList = getFragments();
        if (mFragmentList == null || mFragmentList.size() == 0) {
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "Fragment列表不能为空");
            }
            return;
        }

        ViewPager view_pager = findViewById(R.id.view_pager);
        view_pager.setOffscreenPageLimit(mFragmentList.size() - 1);
        final float normalSize = getResources().getDimension(R.dimen.indicator_normal);
        final float focusedSize = getResources().getDimension(R.dimen.indicator_focused);

        // 指示器
        mIndicator = findViewById(R.id.guide_indicator);

        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        // 将viewPager和indicator使用
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(mIndicator, view_pager);
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                // 根据焦点设置指示器大小
                setCircleSize(preItem, (int) normalSize);
                setCircleSize(currentItem, (int) focusedSize);

                // 已填写过的，指示器为实心蓝色
                setBlueCircle(preItem);

                // 设置当前标题
                if (mTitleList == null) {
                    return;
                }
                if (mTitleList.size() > 0 && (mTitleList.size() == mFragmentList.size())) {
                    setPagerTitle(mTitleList.get(currentItem));
                } else {
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "标题个数和Fragment个数不一致");
                    }
                }
            }
        });

        // 设置indicatorViewPager的适配器
        mIndicatorAdapter = new ColorfulIndicatorAdapter(getSupportFragmentManager(), mFragmentList, inflater);
        indicatorViewPager.setAdapter(mIndicatorAdapter);
        // 默认第0个圆圈是大的
        setCircleSize(0, (int) focusedSize);
        // 设置第0页标题
        if (mTitleList != null && mTitleList.size() > 0) {
            setPagerTitle(mTitleList.get(0));
        }
    }

    /**
     * 设置圆形指示器的大小
     */
    private void setCircleSize(int index, int size) {
        View view = mIndicator.getItemView(index);
        if (view == null) {
            return;
        }
        View circleView = view.findViewById(R.id.view_circle);
        ViewGroup.LayoutParams selectedParams = circleView.getLayoutParams();
        selectedParams.width = size;
        selectedParams.height = size;
        circleView.setLayoutParams(selectedParams);
    }

    /**
     * 设置圆形指示器颜色
     */
    @Override
    public void setRedCircle(int index) {
        View view = mIndicator.getItemView(index);
        if (view == null) {
            return;
        }
        View circleView = view.findViewById(R.id.view_circle);
        circleView.setBackgroundResource(R.drawable.shape_indicator_red);
    }

    @Override
    public void setBlueCircle(int index) {
        View view = mIndicator.getItemView(index);
        if (view == null) {
            return;
        }
        View circleView = view.findViewById(R.id.view_circle);
        circleView.setBackgroundResource(R.drawable.shape_indicator_blue);
    }

    /**
     * 设置圆形指示器颜色
     */
    @Override
    public void setRedCircle(Fragment fragment) {
        View view = getCircleView(fragment);
        if (view == null) {
            return;
        }
        view.setBackgroundResource(R.drawable.shape_indicator_red);
    }

    @Override
    public void setBlueCircle(Fragment fragment) {
        View view = getCircleView(fragment);
        if (view == null) {
            return;
        }
        view.setBackgroundResource(R.drawable.shape_indicator_blue);
    }

    /**
     * @param fragment
     * @return 获取fragment对应的指示器
     */
    private View getCircleView(Fragment fragment) {
        if (mFragmentList == null || mFragmentList.size() == 0) {
            return null;
        }

        int index = 0;
        for (int i = 0; i < mFragmentList.size(); i++) {
            if (fragment == mFragmentList.get(i)) {
                index = i;
                break;
            }
        }

        View view = mIndicator.getItemView(index);
        if (view == null) {
            return null;
        }

        return view.findViewById(R.id.view_circle);
    }


    /**
     * 设置标题
     * @return
     */
    @Override
    public void setPagerTitles(List<String> titles) {
        mTitleList = titles;
        if (mIndicator != null && mTitleList != null && mTitleList.size() > 0) {
            setPagerTitle(mTitleList.get(0));
            mIndicator.setCurrentItem(0);
        }
    }

    /**
     * 手动设置当前标题
     * @param title
     */
    @Override
    public void setPagerTitle(String title) {
        tv_title.setText(title);
    }

    protected abstract List<Fragment> getFragments();
}
