package com.unis.sxzcl.ui.home;

import android.view.LayoutInflater;

import androidx.navigation.NavBackStackEntry;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.unis.model.UserModel;
import com.unis.model.event.LoginOutEvent;
import com.unis.model.event.TextSizeEvent;
import com.unis.sxzcl.R;
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.unis.sxzcl.util.VersionCheckTool;
import com.unis.sxzcl.widget.dialog.SystemDialog;
import com.xyxsbj.mylibrary.tool.ActivityTool;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

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> {

    private SystemDialog mLoginoutDialog;
    private VersionCheckTool mVersionCheckTool;

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

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



    @Override
    protected void workTask() {
        // 版本检查
        mVersionCheckTool = new VersionCheckTool(getContext())
                .setUpdateCallback((data, newest) -> {
                    if (!newest)// 不是新版本时启动对话框并下载
                        mVersionCheckTool.showUpdateDialog(data.get(0));
                })
                .setError(error -> {
                })
                .start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mVersionCheckTool != null)
            mVersionCheckTool.destroy();
        mVersionCheckTool = null;
    }


    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);
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 退出登录
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginOut(LoginOutEvent event) {
        LogTool.d("退出登录");
        // 删除用户信息
        UserModel.get().removeUser();

        // 无信息时直接退出登录
        if (event.getMassage() == null) {
            loginOut();
            return;
        }
        // 弹出对话框 提示用户退出登录
        if (mLoginoutDialog == null)
            mLoginoutDialog = new SystemDialog(getContext())
                    .setHideTitle()
                    .setHideClose()
                    .setCancelable(false)
                    .setRightButton("确定", dialog -> {
                        dialog.dismiss();
                        loginOut();
                    });
        mLoginoutDialog.setMsg(event.getMassage())
                .show();
    }

    public void loginOut() {
        // 重启当前页 弹出其他页面
        ActivityTool.startActivity(MainActivity.this, MainActivity.class);
        finish();//关闭当前页
        // 跳转登录
        FactoryActivity.startPage(getContext(), LoginFragment.class);
    }

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