package com.unis.sxzcl.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.fragment.app.Fragment;

import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.ActivityFactoryBinding;


/**
 * 包名: com.unis.sxzcl.ui
 * 类名: FactoryActivity
 * 作用描述: 工厂Activity
 * 通用Activity页面,用来加载Fragment页面
 *
 * 创建人: mQ
 * 创建时间: 2020/11/9 14:16
 * 更新人:
 * 更新时间: 2020/11/9 14:16
 * 更新说明:
 * 版本号: 1.0
 */
public class FactoryActivity extends BaseActivity<ActivityFactoryBinding> {

    private final static String key_factory_name = "factoryName";

    /**
     * 跳转页面(携带参数)
     *
     * @param context 上下文
     * @param cls     目标Fragment页面
     * @param bundle  携带参数
     */
    public static void startPage(Context context, Class<? extends Fragment> cls, Bundle bundle) {
        if (bundle == null)
            bundle = new Bundle();

        //将fragment类名存入bundle
        bundle.putString(key_factory_name, cls.getName());

        //跳转到工厂Activity
        Intent intent = new Intent(context, FactoryActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /**
     * 跳转页面(不携带参数)
     *
     * @param context 上下文
     * @param cls     目标Fragment页面
     */
    public static void startPage(Context context, Class<? extends Fragment> cls) {
        startPage(context, cls, new Bundle());
    }

    @Override
    protected ActivityFactoryBinding getViewBinding(LayoutInflater inflater) {
        return ActivityFactoryBinding.inflate(inflater);
    }

    @Override
    protected void init() {
        Bundle bundle = getIntent().getExtras();

        //获取fragmentr类名
        String fragmentName = bundle.getString(key_factory_name, "");
        try {
            //实例化fragment
            Fragment fragment = (Fragment) Class.forName(fragmentName).newInstance();
            fragment.setArguments(bundle);

            //显示fragment页面
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.factory_container, fragment)
                    .commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void workTask() {

    }

}
