package com.sxyz.rfid.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

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


/**
 * @Description: java类作用描述
 * @Author: chunyuan.wu
 * @CreateDate: 2024/5/30 14:40
 * @Version: 1.0
 */
public abstract class BaseFragment<T extends ViewBinding> extends Fragment {
    protected T mBinding;
    protected Context mActivity;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getActivity().getIntent().getExtras();
        initParms(bundle);
        mBinding = getViewBinding();
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化
        initView();
        setListener();
        initData();
    }

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

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

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

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

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


    protected void initToolBarTitle(String title) {
        ImmersionBar.setTitleBar(this, getActivity().findViewById(R.id.toolbar));
        ((TextView) getActivity().findViewById(R.id.toolbar_title)).setText(title);
    }

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(mActivity, clz));
    }

    /**
     * [携带数据的页面跳转]
     *
     * @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);
    }

    /**
     * [含有Bundle通过Class打开编辑界面]
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        startActivityForResult(cls, bundle, requestCode);
    }

    /**
     * @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(getActivity(), colorRes);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}