package com.wd.workoffice.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
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.ShapeBadgeItem;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.event.NotifyEvent;
import com.wd.workoffice.bean.event.RefreshTokenEvent;
import com.wd.workoffice.ui.activity.bat.approval.WorkApprovalActivity;
import com.wd.workoffice.ui.activity.login.LoginActivity;
import com.wd.workoffice.ui.activity.login.MainLoginActivity;
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.DataKeeper;
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 int oldPosition;
    private ShapeBadgeItem shapeBadgeItem;

    //    private MainActivityPresenter mainPresenter;
    @Override
    protected void initView() {
        if (TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()) && !UserKeeper.getInstance().getMesLogin()) {
            finish();
            startActivity(MainLoginActivity.class);
            return;
        }
        if (!TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()))
            WorkUtils.handleAlias(Integer.valueOf(UserKeeper.getInstance().getUserId()), 2);
        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));
        shapeBadgeItem = new ShapeBadgeItem();
        shapeBadgeItem.setShape(ShapeBadgeItem.SHAPE_OVAL);
        shapeIsShow();
        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).setBadgeItem(shapeBadgeItem))
                .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;
            }

        }
        if (position == 3) {
            DataKeeper.getInstance().keepMsgStatus(false);
            shapeBadgeItem.hide(true);
        }
        oldPosition = position;
        mainContent.setCurrentItem(position);
//        switch (position) {
//            case 0:
////                com.blankj.utilcode.util.BarUtils.setStatusBarColor(this, Color.WHITE);
////                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
//                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
//                com.blankj.utilcode.util.BarUtils.setNavBarColor(this, Color.WHITE);
//                break;
//            case 1:
////                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
////                com.blankj.utilcode.util.BarUtils.setStatusBarColor(this, Color.parseColor("#fff5a623"));
//                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
//                com.blankj.utilcode.util.BarUtils.setNavBarColor(this,  Color.parseColor("#fff5a623"));
//                break;
//            case 2:
////                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
////                com.blankj.utilcode.util.BarUtils.setStatusBarColor(this, Color.WHITE);
//                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
//                com.blankj.utilcode.util.BarUtils.setNavBarColor(this, Color.WHITE);
//                break;
//            case 3:
////                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
////                com.blankj.utilcode.util.BarUtils.setStatusBarColor(this, Color.WHITE);
//                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
//                com.blankj.utilcode.util.BarUtils.setNavBarColor(this, Color.WHITE);
//                break;
//            case 4:
////                com.blankj.utilcode.util.BarUtils.setNavBarVisibility(this, true);
//                com.blankj.utilcode.util.BarUtils.setStatusBarColor(this, Color.parseColor("#fff5a623"));
////                com.blankj.utilcode.util.BarUtils.setNavBarColor(this, Color.parseColor("#fff5a623"));
//                break;
//
//        }
    }

    @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();
        }
    }

    /**
     * 消息红点是否显示
     */
    private void shapeIsShow() {
        Boolean msgStatus = DataKeeper.getInstance().getMsgStatus();
        if (msgStatus) {
            shapeBadgeItem.show();
        } else {
            shapeBadgeItem.hide();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void login(LoginEvent lg) {
        if (!UserKeeper.getInstance().getMesLogin()) {
            if (!TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()))
                WorkUtils.handleAlias(Integer.valueOf(UserKeeper.getInstance().getUserId()),1);
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            UserKeeper.getInstance().clearAll();
            DataKeeper.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);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void notify(NotifyEvent event) {
        if (event.getType() == 0) {
            shapeIsShow();
        } else {
            startActivity(WorkApprovalActivity.class, "type", "2", "status", "0", "title", "待我审批");
        }
    }
}
