package com.wd.workoffice.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.event.MesLoginEvent;
import com.wd.workoffice.bean.event.RefreshTokenEvent;
import com.wd.workoffice.ui.activity.login.LoginActivity;
import com.wd.workoffice.ui.activity.mes.MesLoginActivity;
import com.wd.workoffice.ui.adapter.VpMainAdapter;
import com.wd.workoffice.ui.fg.MainFragment;
import com.wd.workoffice.ui.fg.MesFragment;
import com.wd.workoffice.ui.fg.MsgFragment;
import com.wd.workoffice.ui.fg.MyFragment;
import com.wd.workoffice.ui.fg.WorkFragment;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

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

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import flexible.xd.android_base.base.AppManager;
import flexible.xd.android_base.model.event.LoginEvent;
import me.jessyan.autosize.internal.CancelAdapt;

public class MainActivity extends WorkBaseActivity implements BottomNavigationBar.OnTabSelectedListener, View.OnClickListener, CancelAdapt {

    @BindView(R.id.bnb_main_bottom_bar)
    BottomNavigationBar bnbMainBottomBar;
    @BindView(R.id.activity_main)
    RelativeLayout activityMain;
    @BindView(R.id.main_content)
    ViewPager mainContent;

    private ArrayList<Fragment> fragments;
    private FragmentManager fm;
    private TextBadgeItem numberBadgeItem;
    private int oldPosition;

    //    private MainActivityPresenter mainPresenter;
    @Override
    protected void initView() {
        if (TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()) && !UserKeeper.getInstance().getMesLogin()) {
            finish();
            startActivity(MainLoginActivity.class);
            return;
        }
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        bnbMainBottomBar.setMode(BottomNavigationBar.MODE_FIXED);
        bnbMainBottomBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bnbMainBottomBar.setBackgroundColor(getResources().getColor(R.color.white));
//        numberBadgeItem = new TextBadgeItem();
//        numberBadgeItem.setBorderWidth(4)
//                .setBackgroundColorResource(R.color.red)
//                .setText("0");
        bnbMainBottomBar
                .addItem(new BottomNavigationItem(R.mipmap.main_bat_choose, "首页").
                        setActiveColorResource(R.color.main_icon_select).
                        setInActiveColorResource(R.color.main_icon).setInactiveIconResource(R.mipmap.main_bat))
                .addItem(new BottomNavigationItem(R.mipmap.main_work_choose, "工作台").
                        setActiveColorResource(R.color.main_icon_select).setInActiveColorResource(R.color.main_icon).
                        setInactiveIconResource(R.mipmap.main_work))
                .addItem(new BottomNavigationItem(R.mipmap.main_mes_choose, "MES").
                        setActiveColorResource(R.color.main_icon_select).setInActiveColorResource(R.color.main_icon).
                        setInactiveIconResource(R.mipmap.main_mes))
                .addItem(new BottomNavigationItem(R.mipmap.main_msg_choose, "消息").
                        setActiveColorResource(R.color.main_icon_select).setInActiveColorResource(R.color.main_icon).
                        setInactiveIconResource(R.mipmap.main_msg))
                .addItem(new BottomNavigationItem(R.mipmap.main_my_choose, "我的")
                        .setActiveColorResource(R.color.main_icon_select).setInActiveColorResource(R.color.main_icon).
                                setInactiveIconResource(R.mipmap.main_my))
                .initialise();
        fm = getSupportFragmentManager();
        WorkUtils.refreshToken();
        fragments = getFragments();
        bnbMainBottomBar.setTabSelectedListener(this);
//        mainContent.setOffscreenPageLimit(4);
//        VpMainAdapter vpFgAdapter = new VpMainAdapter(fm, fragments);
//        mainContent.setAdapter(vpFgAdapter);
//        if (TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()) && UserKeeper.getInstance().getMesLogin()) {
//            bnbMainBottomBar.selectTab(2);
//        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

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


    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }


    public ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new MainFragment());
        fragments.add(new WorkFragment());
        fragments.add(new MesFragment());
        fragments.add(new MsgFragment());
        fragments.add(new MyFragment());
        return fragments;
    }

    @Override
    public void onTabSelected(int position) {
        if (position == 0 || position == 1) {
            if (TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo())) {
                bnbMainBottomBar.selectTab(oldPosition, false);
                startActivity(LoginActivity.class);
                return;
            }
        }
        if (position == 2) {
            if (!UserKeeper.getInstance().getMesLogin()) {
                bnbMainBottomBar.selectTab(oldPosition, false);
                startActivity(MesLoginActivity.class);
                return;
            }
        }
        oldPosition = position;
        mainContent.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {
    }

    /**
     * 阻止保存fragment
     *
     * @param outState
     */
    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }


    long lastTime = 0;

    @Override
    public void onBackPressed() {
        long curTime = System.currentTimeMillis();
        if (curTime - lastTime > 2000) {
            lastTime = curTime;
            toast("再按一次退出");
        } else {
            AppManager.getAppManager().appExit(this);
            super.onBackPressed();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void login(LoginEvent lg) {
        if (!UserKeeper.getInstance().getMesLogin()) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            UserKeeper.getInstance().clearAll();
            startActivity(intent);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeView(RefreshTokenEvent refreshTokenEvent) {
        fragments = getFragments();
        bnbMainBottomBar.setTabSelectedListener(this);
        mainContent.setOffscreenPageLimit(4);
        VpMainAdapter vpFgAdapter = new VpMainAdapter(fm, fragments);
        mainContent.setAdapter(vpFgAdapter);
        if (TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()) && UserKeeper.getInstance().getMesLogin()) {
            bnbMainBottomBar.selectTab(2);
        }
    }

}
