package com.mrzhang.component.componentlib.router;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.mrzhang.component.componentlib.applicationlike.IApplicationLike;

import java.util.HashMap;

/**
 * Created by mrzhang on 2017/6/15.
 */

public class Router {

    private HashMap<String, Object> services = new HashMap<>();
    //注册的组件的集合
    private static HashMap<String, IApplicationLike> components = new HashMap<>();

    private static volatile Router instance;

    private Router() {
    }

    public static Router getInstance() {
        if (instance == null) {
            synchronized (Router.class) {
                if (instance == null) {
                    instance = new Router();
                }
            }
        }
        return instance;
    }

    public synchronized void addService(String serviceName, Object serviceImpl) {
        if (serviceName == null || serviceImpl == null) {
            return;
        }
        services.put(serviceName, serviceImpl);
    }

    public synchronized Object getService(String serviceName) {
        if (serviceName == null) {
            return null;
        }
        return services.get(serviceName);
    }

    public synchronized void removeService(String serviceName) {
        if (serviceName == null) {
            return;
        }
        services.remove(serviceName);
    }

//    public static void registerComponent(String classname) {
//        try {
//            Class clazz = Class.forName(classname);
//            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
//            applicationLike.onCreate();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void unregisterComponent(String classname) {
//        try {
//            Class clazz = Class.forName(classname);
//            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
//            applicationLike.onStop();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    /**
     * 注册组件
     *
     * @param classname 组件名
     */
    public static void registerComponent(@Nullable String classname) {
        if (TextUtils.isEmpty(classname)) {
            return;
        }
        if (components.keySet().contains(classname)) {
            return;
        }
        try {
            Class clazz = Class.forName(classname);
            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
            applicationLike.onCreate();
            components.put(classname, applicationLike);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反注册组件
     *
     * @param classname 组件名
     */
    public static void unregisterComponent(@Nullable String classname) {
        if (TextUtils.isEmpty(classname)) {
            return;
        }
        if (components.keySet().contains(classname)) {
            components.get(classname).onStop();
            components.remove(classname);
            return;
        }
    }

}

@@ -1393,719 +1393,8 @@
  %7D%0A%0A
-//    public static void registerComponent(String classname) %7B%0A//        try %7B%0A//            Class clazz = Class.forName(classname);%0A//            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();%0A//            applicationLike.onCreate();%0A//        %7D catch (Exception e) %7B%0A//            e.printStackTrace();%0A//        %7D%0A//    %7D%0A//%0A//    public static void unregisterComponent(String classname) %7B%0A//        try %7B%0A//            Class clazz = Class.forName(classname);%0A//            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();%0A//            applicationLike.onStop();%0A//        %7D catch (Exception e) %7B%0A//            e.printStackTrace();%0A//        %7D%0A//    %7D%0A%0A%0A
     
@@ -2395,21 +2395,329 @@
 ;%0A        %7D%0A
+        try %7B%0A            Class clazz = Class.forName(classname);%0A            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();%0A            applicationLike.onStop();%0A            components.remove(classname);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A%7D%0A

fix issue #12 https://github.com/luojilab/DDComponentForAndroid/issues/12