package com.webchat.service.behavior;


import com.webchat.common.enums.ResourceBehaviorTypeEnum;
import com.webchat.common.exception.BusinessException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * coderutil资源互动工厂
 */
@Component
public class ResourceBehaviorFactory implements InitializingBean {

    private static final Map<String, AbstractResourceBehaviorService> serviceMap = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        initServiceMap();
    }

    /**
     * 根据 behaviorType 获取 behaviorService
     * @param behaviorType
     * @return
     */
    public static AbstractResourceBehaviorService getBehaviorService(String behaviorType) {
        AbstractResourceBehaviorService behaviorService = serviceMap.get(behaviorType);
        if (behaviorService == null) {
            throw new BusinessException("不支持的behaviorType");
        }
        behaviorService.setBehaviorType(behaviorType);
        return behaviorService;
    }
    /**
     * 注册服务
     */
    private void initServiceMap() {
        // 初始化点赞服务
        serviceMap.put(ResourceBehaviorTypeEnum.LIKE.getBehavior(), new ResourceTwoWayBehaviorService());
        // 预览服务
        serviceMap.put(ResourceBehaviorTypeEnum.VIEW.getBehavior(), new ResourceOneWayBehaviorService());
    }
}
