package com.wd.workoffice.ui.fg;

import android.os.Bundle;
import android.text.TextUtils;

import com.google.android.material.tabs.TabLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.ui.activity.login.LoginActivity;
import com.wd.workoffice.ui.activity.mes.MesLoginActivity;
import com.wd.workoffice.ui.adapter.VpDataAdapter;
import com.wd.workoffice.util.UserKeeper;

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

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 消息
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MsgFragment extends WorkBaseFg {

    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_data)
    ViewPager vpData;
    private String tab[] = {
            "BAT消息", "MES消息"};
    private String type[] = {
            "0", "1"};

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        tlTab.setupWithViewPager(vpData);
        tlTab.setTabMode(TabLayout.MODE_FIXED);
    }

    @Override
    protected void initData() {
        FragmentManager sfm = getChildFragmentManager();
        List<Fragment> data = new ArrayList<>();
        for (int i = 0; i < tab.length; i++) {
            MsgListFragment saleFragment = new MsgListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type", type[i]);
            saleFragment.setArguments(bundle);
            data.add(saleFragment);
        }
        VpDataAdapter vpFgAdapter = new VpDataAdapter(sfm, data);
        vpData.setAdapter(vpFgAdapter);
        for (int i = 0; i < tab.length; i++) {
            tlTab.getTabAt(i).setText(tab[i]);
        }
        if (TextUtils.isEmpty(UserKeeper.getInstance().getUserInfo())) {
            vpData.setCurrentItem(1);
        }
    }

    @Override
    protected void initEvent() {
        vpData.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
//                if (!UserKeeper.getInstance().getMesLogin()) {
//                    startActivity(MesLoginActivity.class);
//                }
            }

            @Override
            public void onPageSelected(int i) {
                if (TextUtils.isEmpty(UserKeeper.getInstance().getUserInfo()) && i == 0) {
                    startActivity(LoginActivity.class);
                }
                if (!UserKeeper.getInstance().getMesLogin() && i == 1) {
                    startActivity(MesLoginActivity.class);
                    vpData.setCurrentItem(0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.fg_msg;
    }
}
