/*
 * *********************************************************
 *   author   colin
 *   email    wanglin2046@126.com
 *   date     18-1-9 上午9:59
 * ********************************************************
 */

package com.rdiot.yx485;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.king.zxing.util.LogUtils;
import com.rdiot.yx485.util.IntentUtils;
import com.zcolin.frame.app.BaseFrameActivity;
import com.zcolin.frame.app.MessageEvent;

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

import java.util.ArrayList;
import java.util.List;


/**
 * DBDemo
 */
@SuppressLint("Registered")
public class BaseActivity extends BaseFrameActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置沉浸式状态栏
        Window window = getWindow();
        List<String> activities = new ArrayList<>();
        activities.add("SplashActivity");
        activities.add("MainActivityTab");
        LogUtils.d(getClass().getSimpleName());

        if (activities.contains(getClass().getSimpleName())) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 清除半透明状态栏标志
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // 添加绘制系统栏背景标志
            window.setStatusBarColor(Color.TRANSPARENT); // 设置状态栏颜色为透明

            // 如果需要内容延伸到状态栏后面（沉浸式效果）
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else if (getClass().getSimpleName().equals("LoginWithPwdActivity")) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 清除半透明状态栏标志
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // 添加绘制系统栏背景标志
            window.setStatusBarColor(Color.TRANSPARENT); // 设置状态栏颜色为透明

            // 如果需要内容延伸到状态栏后面（沉浸式效果）
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        } else {
            getWindow().setStatusBarColor(Color.WHITE);
        }
    }

    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        LogUtils.d(event.message);
        /* 处理事件 */
        if ("reLogin".equals(event.message)) {
            IntentUtils.intentToLogin(this);
        }
    }

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

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