package com.tengshisoft.mainmodule.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.tengshisoft.mainmodule.R;
import com.tenlionsoft.baselib.app.BaseAppContext;
import com.tenlionsoft.baselib.constant.LionActions;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.utils.LogUtils;

import java.util.List;

import androidx.annotation.Nullable;

public class CustomPushMsgActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).init();
        setContentView(R.layout.activity_custom_push_msg);
        LogUtils.e("推送页面");
        BaseAppContext.getInstance().setOtherUserId(getIntent().getStringExtra("to"));
        if (isApplicationBroughtToBackground()) {
            Intent intent = new Intent();
            intent.putExtra(LionActions.EXTRA_PUSH_MSG, getIntent().getStringExtra("page"));
            intent.putExtra(LionActions.EXTRA_MSG_TYPE, getIntent().getIntExtra("type", -1));
            intent.putExtra("to", getIntent().getStringExtra("to"));
            intent.putExtra("time", getIntent().getStringExtra("time"));
            intent.putExtra(LionActions.EXTRA_MSG_CONF_ID, getIntent().getStringExtra(LionActions.EXTRA_MSG_CONF_ID));
            intent.setAction(LionActions.ACTION_PUSH_MSG_DISPOSE);
            sendBroadcast(intent);
            finish();
        } else {
            ARouter.getInstance()
                .build(PathConfig.PATH_MODULE_MAIN_SPLASH)
                .withString(LionActions.EXTRA_PUSH_MSG, getIntent().getStringExtra("page"))
                .withString("to", getIntent().getStringExtra("to"))
                .withInt(LionActions.EXTRA_MSG_TYPE, getIntent().getIntExtra("type", -1))
                .withString("time", getIntent().getStringExtra("time"))
                .withString(LionActions.EXTRA_MSG_CONF_ID, getIntent().getStringExtra(LionActions.EXTRA_MSG_CONF_ID))
                .navigation();
            finish();
        }
    }


    private boolean isApplicationBroughtToBackground() {
        Intent intent = new Intent(this, MainTabActivity.class);
        ComponentName cmpName = intent.resolveActivity(getPackageManager());
        boolean flag = false;
        if (cmpName != null) {
            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(10);
            if (!tasks.isEmpty()) {
                for (ActivityManager.RunningTaskInfo taskInfo : tasks) {
                    if (taskInfo.baseActivity.equals(cmpName)) {
                        flag = true;
                        break;
                    }
                }
            }
        }
        return flag;
    }
}