package com.unis.sxzcl.ui.home;

import android.os.Handler;
import android.view.LayoutInflater;

import com.unis.model.BasicModel;
import com.unis.model.UserModel;
import com.unis.model.event.LoginOutEvent;
import com.unis.model.event.TextSizeEvent;
import com.unis.sxzcl.base.BaseActivity;
import com.unis.sxzcl.base.FactoryActivity;
import com.unis.sxzcl.databinding.ActivityMainBinding;
import com.unis.sxzcl.ui.login.LoginFragment;
import com.xyxsbj.mylibrary.tool.ActivityTool;

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

/**
 * 包名: com.unis.sxzcl.ui.home
 * 类名: MainActivity
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/25 15:05
 * 更新人:
 * 更新时间: 2020/11/25 15:05
 * 更新说明:
 * 版本号: 1.0
 */
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    @Override
    protected ActivityMainBinding getViewBinding(LayoutInflater inflater) {
        return ActivityMainBinding.inflate(inflater);
    }

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

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

    @Override
    protected void workTask() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 获取字典缓存
                BasicModel.get().getDepotDictionary();
                BasicModel.get().getDepartmentDictionary();
                BasicModel.get().getHouseDictionary();
                BasicModel.get().getPersonDictionary();
            }
        }, 1000);

    }

    long exitTime;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//        NavBackStackEntry currentEntry = navController.getCurrentBackStackEntry();
//        NavBackStackEntry mainEntry = navController.getBackStackEntry(R.id.mainFragment);
//        if (currentEntry == mainEntry) {
//            long currentTime = System.currentTimeMillis();
//            LogTool.i("退出时间:" + exitTime + " 当前时间:" + currentTime + " 时间差:" + (currentTime - exitTime));
//            if ((currentTime - exitTime) > 1000) {
//                ToastTool.showLong("再按一次退出程序");
//                exitTime = currentTime;
//            } else {
//                Intent home = new Intent(Intent.ACTION_MAIN);
//                home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                home.addCategory(Intent.CATEGORY_HOME);
//                startActivity(home);
//            }
//        } else {
//            super.onBackPressed();
//        }
    }

    /**
     * 退出登录
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginOut(LoginOutEvent event) {
        // 删除用户信息
        UserModel.get().removeUser();
        // 重启当前页 弹出其他页面
        ActivityTool.startActivity(getContext(), this.getClass());
        // 跳转登录
        FactoryActivity.startPage(getContext(), LoginFragment.class);
        finish();//关闭当前页
    }

    /**
     * 字体大小改变
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTextSize(TextSizeEvent event) {
        // 重启当前页面
        finish();
        ActivityTool.startActivity(getContext(), this.getClass());
    }
}
