package com.foreverwin.mesnac.dataimport.client;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.modular.core.meext.MEServices;
import com.sap.me.common.ObjectReference;
import com.sap.me.user.*;

import java.lang.reflect.UndeclaredThrowableException;

public class UserWSClient {
    public static String removeMEUser(JSONObject data) {
        try {
            if (data.containsKey("defaultSite")) {
                String site = data.getString("defaultSite");
                UserConfigurationServiceInterface userService = MEServices.create("com.sap.me.user", "UserConfigurationService", site);
                String userId = data.getString("id");
                String userRef = "UserBO:" + site + "," + userId;
                ObjectReference request = new ObjectReference(userRef);
                userService.deleteUser(request);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof UndeclaredThrowableException){
                return ((UndeclaredThrowableException)e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }

    public static String createMEUser(JSONObject data) {
        try {
            if (data.containsKey("defaultSite")) {
                String site = data.getString("defaultSite");
                UserConfigurationServiceInterface userService = MEServices.create("com.sap.me.user", "UserConfigurationService", site);
                String userId = data.getString("id");
                CreateUserRequest meuser = new CreateUserRequest();
                meuser.setUserId(userId);
                userService.createUser(meuser);
                UserGroupConfigurationServiceInterface userGroupService = MEServices.create("com.sap.me.user", "UserGroupConfigurationService", site);
                //ObjectReference arg0
                String id = data.getString("id");
                String defaultGroups = data.getString("defaultGroups");
                String[] groups = defaultGroups.split(",");
                for (String _group : groups) {
                    String groupRef = "UserGroupBO:" + site + "," + _group;
                    String userRef = "UserGroupBO:" + site + "," + userId;
                    ObjectReference request = new ObjectReference(groupRef);
                    UserGroupFullConfiguration myGroup = userGroupService.readUserGroup(request);
                    UserGroupMember newMember = new UserGroupMember();
                    newMember.setUserRef(userRef);
                    if (!myGroup.getUserGroupMembers().contains(newMember)) {
                        myGroup.getUserGroupMembers().add(newMember);
                        userGroupService.updateUserGroup(myGroup);
                    }
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof UndeclaredThrowableException){
                return ((UndeclaredThrowableException)e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }
}
