package com.tjzhxx.union.public_store.fragment;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

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


/**
 * Created by Administrator on 2015/11/16.
 */
public class FragmentPager extends ViewPager {
    private FragmentTab[] fragmentTabs;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private FragmentManager fragmentManager;
    private FragmentPagerListener fragmentPagerListener;
    private Class<? extends Fragment>[] fragments;

    private boolean isCanScroll = false;

    private int oldItem = 0;


    public FragmentPager init(FragmentManager fragmentManager1, FragmentPagerListener fragmentPagerListener1) {
        if (fragmentManager1 == null) {
            throw new RuntimeException("FragmentManager can not been null");
        }
        this.fragmentManager = fragmentManager1;
        this.fragmentPagerListener = fragmentPagerListener1;
        return this;
    }

    public FragmentPager setTabAndFragmentData(FragmentTab[] fragmentTabs1, Class<? extends Fragment>[] fragments1) {
        if (fragmentTabs1 == null || fragments1 == null) {
            throw new RuntimeException("FragmentVisitor data init error:either can not been null");
        }
        if (fragmentTabs1.length != fragments1.length) {
            throw new RuntimeException("FragmentVisitor data init error:is not equal size");
        }

        this.fragmentTabs = fragmentTabs1;
        this.fragments = fragments1;
        for (int i = 0; i < fragmentTabs.length; i++) {
            final int finalI = i;
            fragmentTabs[finalI].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCurrentItem(finalI);
                }
            });
        }
        addFragments();
        this.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return this;
    }

    public FragmentPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOverScrollMode(OVER_SCROLL_NEVER);
        setOffscreenPageLimit(1);
    }

    public FragmentPager(Context context) {
        this(context, null);
    }


    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        if (isCanScroll()) {
            return super.onTouchEvent(arg0);
        } else {
            return false;
        }

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        if (isCanScroll()) {
            return super.onInterceptTouchEvent(arg0);
        } else {
            return false;
        }

    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (isCanScroll()) {
            return super.dispatchKeyEvent(event);
        } else {
            return false;
        }
    }

    //    @Override
//    public void setCurrentItem(int item) {
//        super.setCurrentItem(item);
//        setTab(item);
//    }
    public void setCurrentItem(int item) {
        if (item < 0) {
            return;
        }
        boolean itemEnable = true;
        if (fragmentPagerListener != null) {
            itemEnable = fragmentPagerListener.boforeItemChangedItemEnable(item);
        }
        if (fragmentPagerListener != null) {
            fragmentPagerListener.controlItemEnable(item, itemEnable);
        }
        if (!itemEnable) {
            //调到原来的item
            if (oldItem >= 0) {
                super.setCurrentItem(oldItem);
            }
            return;
        }
        //item改变
        super.setCurrentItem(item);
        if (fragmentPagerListener != null && oldItem >= 0) {
            fragmentPagerListener.onItemChanged(oldItem, item, fragmentTabs[item].getTabCaption());
        }
        oldItem = item;
        setTabs();
    }

    private void setTabs() {
        if ((fragmentTabs != null) && (fragmentTabs.length - 1 >= oldItem)) {
            for (int i = 0; i < fragmentTabs.length; i++) {
                fragmentTabs[i].setTabFoucus(i == oldItem);
            }
        }
    }

    public boolean isCanScroll() {
        return isCanScroll;
    }

    public FragmentPager setCanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
        return this;
    }

    private void addFragments() {
        if (fragments == null || fragments.length <= 0) {
            return;
        }
        fragmentList.clear();
        for (int i = 0; i < fragments.length; i++) {
            try {
                fragmentList.add(fragments[i].newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        this.setAdapter(new FragmentPagerAdapter(fragmentManager, fragmentList));
    }

    public interface FragmentPagerListener {
        void onItemChanged(int oldItem, int newItem, String tabDiscription);

        boolean boforeItemChangedItemEnable(int currentItem);

        void controlItemEnable(int item, boolean enable);
    }

    public void resetHeight(int height) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
        } else {
            layoutParams.height = height;
        }
        setLayoutParams(layoutParams);
    }
}
