package com.genlot.ninglottery.base;

import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager;

import com.genlot.ninglottery.widget.Diaolog.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends FragmentActivity {
    private LoadingDialog mLoadingDialog;
    private Unbinder mBinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutID());
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);   //进入页面隐藏输入框
        mBinder = ButterKnife.bind(this);
        mLoadingDialog = new LoadingDialog(this);
        initViews(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBinder.unbind();
        dismissLoading();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);   //进入页面隐藏输入框
    }

    public abstract int getLayoutID();

    public abstract void initViews(Bundle savedInstanceState);

    private boolean isMainThread() {
        return Looper.myLooper() == Looper.getMainLooper();
    }

    public void showLoading() {
        if (isMainThread()) {
            mLoadingDialog.show();
        } else {
            runOnUiThread(() -> mLoadingDialog.show());
        }
    }

    public void showLoading(String msg) {
        if (isMainThread()) {
            mLoadingDialog.show(msg);
        } else {
            runOnUiThread(() -> mLoadingDialog.show(msg));
        }
    }

    public void dismissLoading() {
        if (mLoadingDialog != null) {
            if (isMainThread()) {
                mLoadingDialog.close();
            } else {
                runOnUiThread(() -> mLoadingDialog.close());
            }
        }
    }
}
