package com.ez.ertc.meeting.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

public class ActivityLauncher {
    private static final String TAG = "ActivityLauncher";
    private Context mContext;
    private RouterFragment mRouterFragment;
    private RouterFragmentApp mRouterFragmentApp;

    public static ActivityLauncher init(FragmentActivity activity) {
        return new ActivityLauncher(activity);
    }

    public static ActivityLauncher init(Activity activity) {
        return new ActivityLauncher(activity);
    }

    private ActivityLauncher(FragmentActivity activity) {
        this.mContext = activity;
        this.mRouterFragment = this.getRouterFragment(activity);
    }

    private ActivityLauncher(Activity activity) {
        this.mContext = activity;
        this.mRouterFragmentApp = this.getRouterFragment(activity);
    }

    private RouterFragment getRouterFragment(FragmentActivity activity) {
        RouterFragment routerFragment = this.findRouterFragment(activity);
        if (routerFragment == null) {
            routerFragment = RouterFragment.newInstance();
            FragmentManager fragmentManager = activity.getSupportFragmentManager();
            fragmentManager.beginTransaction().add(routerFragment, "ActivityLauncher").commitAllowingStateLoss();
            fragmentManager.executePendingTransactions();
        }

        return routerFragment;
    }

    private RouterFragmentApp getRouterFragment(Activity activity) {
        RouterFragmentApp routerFragment = this.findRouterFragment(activity);
        if (routerFragment == null) {
            routerFragment = RouterFragmentApp.newInstance();
            android.app.FragmentManager fragmentManager = activity.getFragmentManager();
            fragmentManager.beginTransaction().add(routerFragment, "ActivityLauncher").commitAllowingStateLoss();
            fragmentManager.executePendingTransactions();
        }

        return routerFragment;
    }

    private RouterFragment findRouterFragment(FragmentActivity activity) {
        return (RouterFragment)activity.getSupportFragmentManager().findFragmentByTag("ActivityLauncher");
    }

    private RouterFragmentApp findRouterFragment(Activity activity) {
        return (RouterFragmentApp)activity.getFragmentManager().findFragmentByTag("ActivityLauncher");
    }

    public void startActivityForResult(Class<?> clazz, ActivityLauncher.Callback callback) {
        Intent intent = new Intent(this.mContext, clazz);
        this.startActivityForResult(intent, callback);
    }

    public void startActivityForResult(Intent intent, ActivityLauncher.Callback callback) {
        if (this.mRouterFragment != null) {
            this.mRouterFragment.startActivityForResult(intent, callback);
        } else {
            if (this.mRouterFragmentApp == null) {
                throw new RuntimeException("please do init first!");
            }

            this.mRouterFragmentApp.startActivityForResult(intent, callback);
        }

    }

    public interface Callback {
        void onActivityResult(int resultCode, Intent data);
    }
}
