package com.unis.sxzcl.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewbinding.ViewBinding;

import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.unis.model.Constant;
import com.unis.sxzcl.R;
import com.unis.sxzcl.widget.dialog.ProgressSysDialog;
import com.xyxsbj.mylibrary.tool.ActivityTool;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.SPTool;

/**
 * 包名: com.unis.sxzcl.base
 * 类名: BaseActivity
 * 作用描述: Activity基类
 * 创建人: mQ
 * 创建时间: 2020/11/9 13:58
 * 更新人:
 * 更新时间: 2020/11/9 13:58
 * 更新说明:
 * 版本号: 1.0
 */
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {

    private boolean active;

    public T mBinding;
    private ProgressSysDialog mProgressDialog;
    private Gson mGson;
    private boolean statusBarWhite = true;

    protected abstract T getViewBinding(LayoutInflater inflater);

    protected abstract void init();

    protected abstract void workTask();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        updateTextSize();//更新字体大小
        mBinding = getViewBinding(getLayoutInflater());
        setContentView(mBinding.getRoot());
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        //监听窗口变化
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
            if (statusBarWhite)
                ActivityTool.hideNavigationAndDark(getContext(), getWindow());
            else
                ActivityTool.hideNavigationAndLight(getContext(), getWindow());
        });

        active = true;
        //设置沉浸式标题栏颜色
        StatusBarUtil.setTranslucent(this, 0);
        setFitsSystemWindows(this, false);

        //初始化
        init();
        //执行业务
        workTask();
    }

    public void setStatusBarWhite(boolean statusBarWhite) {
        this.statusBarWhite = statusBarWhite;
        if (statusBarWhite)
            ActivityTool.hideNavigationAndDark(getContext(), getWindow());
        else
            ActivityTool.hideNavigationAndLight(getContext(), getWindow());
    }

    protected Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        active = false;
    }

    public boolean isActive() {
        return active;
    }

    public Gson getGson() {
        if (mGson == null)
            mGson = new Gson();
        return mGson;
    }

    /**
     * 更新字体大小
     */
    private void updateTextSize() {
        String themeFlag = SPTool.getString(Constant.KEY_TEXT_SIZE, "2");
        LogTool.d("字体大小: " + themeFlag);
        switch (themeFlag) {
            case "1":
                this.setTheme(R.style.Theme_1);
                break;
            case "2":
                this.setTheme(R.style.Theme_2);
                break;
            case "3":
                this.setTheme(R.style.Theme_3);
                break;
            case "4":
                this.setTheme(R.style.Theme_4);
                break;
            case "5":
                this.setTheme(R.style.Theme_5);
                break;
            case "6":
                this.setTheme(R.style.Theme_6);
                break;
        }
    }

    /**
     * 设置页面最外层布局 FitsSystemWindows 属性 *
     *
     * @param activity
     * @param value
     */
    public void setFitsSystemWindows(Activity activity, boolean value) {
        ViewGroup contentFrameLayout = activity.findViewById(android.R.id.content);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null) {
            parentView.setFitsSystemWindows(value);
        }
    }

    /**
     * 初始化刷新控件
     */
    protected void initRefreshView(SwipeRefreshLayout refreshLayout,
                                   SwipeRefreshLayout.OnRefreshListener listener) {
        refreshLayout.setColorSchemeResources(R.color.colorTheme, R.color.colorThemeL);
        refreshLayout.setOnRefreshListener(listener);
    }

    protected void showProgress(String msg, boolean isTouch) {
        if (mProgressDialog == null)
            mProgressDialog = new ProgressSysDialog(getContext());
        if (msg == null)
            msg = "请等待...";
        mProgressDialog.setMessage(msg);
        mProgressDialog.getDialog().setCancelable(isTouch);
        if (!mProgressDialog.getDialog().getDialog().isShowing())
            mProgressDialog.show();
    }

    protected void showProgress(String msg) {
        showProgress(msg, true);
    }

    protected void hideProgress() {
        if (mProgressDialog != null)
            mProgressDialog.dismiss();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    public int getStatusHeight() {
        //获取状态栏高度
        int height = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            height = getResources().getDimensionPixelSize(resourceId);
        }
        LogTool.i("状态栏高度:" + height);
        return height;
    }

}
