package com.ysnows.base.widget.tablayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hwangjr.rxbus.RxBus;
import com.qmuiteam.qmui.widget.tab.QMUITab;
import com.ysnows.base.R;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by xianguangjin on 16/5/20.
 */
public class TabLayout extends RecyclerView implements OnItemClickListener {
    private ArrayList<Tab> tabs = new ArrayList<>();

    private int curTab = 0;
    private int preTab = 0;
    private MenuTabBuilder mTabBuilder;
    private MainMenuAdapter adapter;
    private GridLayoutManager layoutManager;

    public TabLayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public TabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        setHasFixedSize(true);
        TypedArray array = context.obtainStyledAttributes(attrs,
                R.styleable.TabLayout, defStyleAttr, 0);

        // tabBuilder
        int normalTextSize = array.getDimensionPixelSize(
                R.styleable.TabLayout_android_textSize,
                getResources().getDimensionPixelSize(R.dimen.qmui_tab_segment_text_size));

        normalTextSize = array.getDimensionPixelSize(
                R.styleable.TabLayout_normal_text_size, normalTextSize);
        int selectedTextSize = normalTextSize;
        selectedTextSize = array.getDimensionPixelSize(
                R.styleable.TabLayout_selected_text_size, selectedTextSize);

        mTabBuilder = new MenuTabBuilder(context)
                .setTextSize(normalTextSize, selectedTextSize)
                .setIconPosition(array.getInt(R.styleable.TabLayout_icon_position,
                        QMUITab.ICON_POSITION_LEFT));

        array.recycle();

        adapter = new MainMenuAdapter();
        setAdapter(adapter);
        adapter.setOnItemClickListener(this);
//        adapter.setAnimationEnable(false);
 //       adapter.setAnimationFirstOnly(false);
  //      adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn);
    }


    public void rendor() {
        layoutManager = new GridLayoutManager(getContext(), tabs.size());
        //setLayoutManager(new LinearLayoutManager(getContext(), HORIZONTAL, false));
        setLayoutManager(layoutManager);
        adapter.setList(tabs);
    }
    public void clear(){
        tabs.clear();
//        adapter.clearList();
    }

    private OrderCallBack callBack=null;

    public void  setListener(OrderCallBack callBack){
        this.callBack=callBack;
    }


    public TabLayout addTab(Tab tab) {
        tabs.add(tab);
        return this;
    }


    public MenuTabBuilder tabBuilder() {
        // do not change mTabBuilder to keep common config not changed
        return new MenuTabBuilder(mTabBuilder);
    }


    public void switchTab(int tabIndex) {
        preTab = curTab;
        curTab = tabIndex;

        //超出index,不做处理
        if (tabIndex >= tabs.size()) {
            return;
        }

        FragmentManager manager = ((FragmentActivity) getContext()).getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        Fragment fragment;
        for (int i = 0; i < tabs.size(); i++) {
            Tab tab = tabs.get(i);
            fragment = manager.findFragmentByTag("tab_" + i);

            if (tabIndex == i) {
                tab.setSelected(true);

                if (fragment == null) {
                    fragment = tab.fragment;
                    transaction.add(R.id.container, fragment, "tab_" + tabIndex);
                } else {
                    transaction.show(fragment);
                }
            } else {
                tab.setSelected(false);
                if (fragment == null) {
                    continue;
                }
                transaction.hide(fragment);
            }
        }

//        transaction.addToBackStack(getClass().getCanonicalName());
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        switchTab(position);


       //刷新订单信息
        if(null!=callBack){
            callBack.isShow("1");
        }

    }
}
