package com.unis.sxzcl.ui.todo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.unis.model.DataCallback;
import com.unis.model.ProcessModel;
import com.unis.model.bean.todo.TodoListType;
import com.unis.model.event.TodoEvent;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentBaseTabPagerBinding;
import com.unis.sxzcl.widget.TabFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

/**
 * 包名: com.unis.sxzcl.ui.todo
 * 类名: TodoHomeFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/5 15:35
 * 更新人:
 * 更新时间: 2020/12/5 15:35
 * 更新说明:
 * 版本号: 1.0
 */
public class TodoHomeFragment extends BaseFragment<FragmentBaseTabPagerBinding> {

    private List<String> mTabs = Arrays.asList("已发起", "待处理", "已处理");
    private List<Fragment> mFragments;
    private TabFragmentPagerAdapter mAdapter;

    @Override
    protected FragmentBaseTabPagerBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentBaseTabPagerBinding.inflate(inflater);
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        mBinding.bar.setTitle("待办事项").setLeftClickListener(v -> onBack());

        initFragment();
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mAdapter = new TabFragmentPagerAdapter(
                getChildFragmentManager(), mTabs, mFragments);
        mBinding.viewPager.setAdapter(mAdapter);
        mBinding.viewPager.setOffscreenPageLimit(mFragments.size());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(TodoListFragment.newFragment(TodoListType.APPLY));
        mFragments.add(TodoListFragment.newFragment(TodoListType.WAIT_DISPOSED));
        mFragments.add(TodoListFragment.newFragment(TodoListType.FINISH));
    }

    @Override
    protected void onFirstShow() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            int tabIndex = bundle.getInt("tabIndex", 0);
            mBinding.viewPager.setCurrentItem(tabIndex);
        }
        getTodoCount();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(TodoEvent event) {
        getTodoCount();
    }

    /**
     * 获取待办数量
     */
    private void getTodoCount() {
        ProcessModel.get().getTodoCount(new DataCallback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                if (!isActive()) return;

                if ((int) data == 0) {
                    mTabs.set(1, "待处理");
                    mBinding.tabLayout.getTabAt(1).setText("待处理");
                    return;
                }
                if (data > 99) {
                    mTabs.set(1, "待处理(99+)");
                    mBinding.tabLayout.getTabAt(1).setText("待处理(99+)");
                } else {
                    mTabs.set(1, "待处理(" + data + ")");
                    mBinding.tabLayout.getTabAt(1).setText("待处理(" + data + ")");
                }
            }
        });
    }
}
