package com.itant.shiwushu.base;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;

import com.itant.shiwushu.R;
import com.itant.shiwushu.manager.ActivityManager;
import com.itant.shiwushu.manager.SharedPreferenceManager;
import com.itant.shiwushu.manager.ToastManager;
import com.itant.shiwushu.tool.ActivityTool;
import com.itant.shiwushu.ui.login.LoginActivity;
import com.itant.shiwushu.widget.LoadingDialog;


/**
 * Created by Jason on 2018/8/25.
 */

public class LAFBaseActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    protected void onSoftKeyboardStateChanged(boolean isVisible) {}
    protected void setLoading(boolean isLoading) {
        if (isLoading) {
            if (loadingDialog == null) {
                loadingDialog = new LoadingDialog(this);
            }
            loadingDialog.show();
        } else {
            if (loadingDialog != null) {
                loadingDialog.hide();
            }
        }
    }

    protected void setLoading(boolean isLoading, boolean isCancelable) {
        setLoading(isLoading);
        loadingDialog.setCancelable(isCancelable);
    }

    protected ViewTreeObserver.OnGlobalLayoutListener getLayoutListener(final View view) {
        return new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                view.getWindowVisibleDisplayFrame(r);
                if (view.getRootView().getHeight() - (r.bottom - r.top) > 500) { // if more than 100 pixels, its probably a keyboard...
                    //键盘弹出了
                    onSoftKeyboardStateChanged(true);
                } else {
                    //键盘隐藏了
                    onSoftKeyboardStateChanged(false);
                }
            }
        };
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        // 低内存的时候，自杀，防止空指针
        /*if (level == TRIM_MEMORY_UI_HIDDEN) {
            finish();
        }*/
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().removeActivity(this);
    }

    public void needReLogin() {
        // 清除数据，重新登录
        if (!ActivityManager.getInstance().isLoginExist()) {
            ActivityManager.getInstance().setLoginExist(true);
            ToastManager.getInstance().toastShort("登录已过期");
            SharedPreferenceManager.getInstance().clear();
            Intent intent = new Intent(this, LoginActivity.class);
            //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ActivityTool.startActivity(this, intent);
            ActivityManager.getInstance().clearActivityExcept(LoginActivity.class);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_slide_left_in, R.anim.anim_slide_right_out);
    }
}
