package com.sxyz.rfid.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewbinding.ViewBinding;

import com.gyf.immersionbar.ImmersionBar;
import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.utils.ToastUtil;

/**
 * @Description: java类作用描述
 * @Author: chunyuan.wu
 * @CreateDate: 2024/5/30 13:49
 * @Version: 1.0
 */


public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
    protected Context mActivity;
    protected boolean isAddActivity = true;//是否支持滑动返回/是否沉浸状态栏
    protected T mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //用于判断这个Activity的启动标志，看它所在的应用是不是从后台跑到前台的。如果是，则直接把它finish（）掉，
        // 然后系统会去Activity启动历史栈查询上一个activity，然后再新建它，所以还原到了按home键出去的那个界面。
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        if (isAddActivity) {
            App.getInstance().addActivity(this);
        }
        initParms(getIntent().getExtras());
        mActivity = this;
        mBinding = getViewBinding();
        //设置布局文件
        setContentView(mBinding.getRoot());
        ///初始化沉浸式以及bar
        initImmersionBar();
        //初始化
        initView();
        //定义事件监听的方法
        setListener();
        //定义初始化数据的方法
        initData();
    }

    /**
     * 初始化参数
     */
    public abstract void initParms(Bundle mBundle);

    /**
     * 获取布局文件
     */
    protected abstract T getViewBinding();

    /**
     * 初始化
     */
    protected abstract void initView();

    /**
     * 设置事件监听
     */
    protected abstract void setListener();


    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化沉浸式以及bar
     */
    protected void initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(false).navigationBarColor(R.color.white).init();
    }

    /**
     * @description 统一初始化titlebar
     * @author wuchunyuan
     * @time 2020/7/16 10:50
     */
    protected void initToolBarTitle(String title) {
        ImmersionBar.setTitleBar(this, findViewById(R.id.toolbar));
        ((TextView) findViewById(R.id.toolbar_title)).setText(title);
    }

    /**
     * @description 统一初始化titlebar
     * @author wuchunyuan
     * @time 2020/7/16 10:50
     */
    protected void initToolBar(int title) {
        ImmersionBar.setTitleBar(this, findViewById(R.id.toolbar));
        findViewById(R.id.iv_back).setOnClickListener(v -> onBackPressed());
        ((TextView) findViewById(R.id.toolbar_title)).setText(getResources().getText(title));
    }

    /**
     * @description 统一初始化titlebar
     * @author wuchunyuan
     * @time 2020/7/16 10:50
     */
    protected void initToolBar(String title) {
        ImmersionBar.setTitleBar(this, findViewById(R.id.toolbar));
        findViewById(R.id.iv_back).setOnClickListener(v -> onBackPressed());
        ((TextView) findViewById(R.id.toolbar_title)).setText(title);
    }

    /**
     * @description 统一初始化titlebar右侧图片
     * @author wuchunyuan
     * @time 2020/7/16 11:10
     */
    protected void initToolBarRightImg(int title, int rightId, final View.OnClickListener listener) {
        ImmersionBar.setTitleBar(this, findViewById(R.id.toolbar));
        findViewById(R.id.iv_back).setOnClickListener(v -> onBackPressed());
        ((TextView) findViewById(R.id.toolbar_title)).setText(getResources().getText(title));
        ImageView ivRight = findViewById(R.id.iv_right);
        ivRight.setImageResource(rightId);
        ivRight.setOnClickListener(view -> listener.onClick(view));
    }

    /**
     * @description 统一初始化titlebar右侧文字
     * @author wuchunyuan
     * @time 2020/7/16 11:10
     */
    protected void initToolBarRightTxt(String title, int rightId, final View.OnClickListener listener) {
        ImmersionBar.setTitleBar(this, findViewById(R.id.toolbar));
        findViewById(R.id.iv_back).setOnClickListener(v -> onBackPressed());
        ((TextView) findViewById(R.id.toolbar_title)).setText(title);
        TextView txtRight = findViewById(R.id.txt_right);
        txtRight.setText(rightId);
        txtRight.setOnClickListener(view -> listener.onClick(view));
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 如果你的app可以横竖屏切换，并且适配4.4或者emui3手机请务必在onConfigurationChanged方法里添加这句话
        ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(true).navigationBarColor(R.color.black).init();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    /**
     * @description 简化Toast
     * @author wuchunyuan
     * @time 2020/7/10 9:53
     */
    protected void showToast(String msg) {
        ToastUtil.showShort(msg);
    }

    /**
     * @description 简化Toast
     * @author wuchunyuan
     * @time 2020/7/10 9:53
     */
    protected void showToast(int msg) {
        ToastUtil.showShort(msg);
    }

    @ColorInt
    protected int color(@ColorRes int colorRes) {
        return ContextCompat.getColor(this, colorRes);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.destroy(this, null);
    }

    /**
     * startActivity 方法优化
     */
    public void startActivity(Class<? extends Activity> cls) {
        startActivity(new Intent(this, cls));
    }

    public void startActivityFinish(Class<? extends Activity> cls) {
        startActivityFinish(new Intent(this, cls));
    }

    public void startActivityFinish(Intent intent) {
        startActivity(intent);
        finish();
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mActivity, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

}