package com.foreverwin.mesnac.dataimport.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.modular.core.meext.MEServices;
import com.sap.me.common.CustomValue;
import com.sap.me.common.ObjectReference;
import com.sap.me.customdata.CustomDataServiceInterface;
import com.sap.me.customdata.SaveCustomDataRequest;
import com.sap.me.frame.domain.BusinessException;
import com.sap.me.plant.SiteConfigurationServiceInterface;
import com.sap.me.plant.SiteFullConfiguration;
import org.apache.log4j.Logger;

import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


/**
 * 自定义数据保存&更新
 *
 * @author Leon
 * @date 2021/7/6
 */
public class CustomDataWSClient {

    private static Logger log4j = Logger.getLogger(CustomDataWSClient.class);

    public static String getSiteDefaultLang(String site) {
        SiteConfigurationServiceInterface siteConfigurationService = null;
        try {
            siteConfigurationService = MEServices.create("com.sap.me.plant", "SiteConfigurationService", site);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //siteLocale
        String objRef = "SiteBO:" + site;

        ObjectReference request = new ObjectReference(objRef);
        try {
            SiteFullConfiguration tm = siteConfigurationService.readSite(request);
            if (tm.getSiteLocale() != null) {
                return tm.getSiteLocale().split("_")[0];
            } else {
                String gRef = "SiteBO:*";
                request = new ObjectReference(gRef);
                tm = siteConfigurationService.readSite(request);
                if (tm.getSiteLocale() != null) {
                    return tm.getSiteLocale().split("_")[0];
                }
            }
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return Locale.CHINESE.getLanguage();
    }

    public static String update(JSONObject data, String ref, String objectAlias)  throws Exception{
        String site = (String) data.get("site");

        SaveCustomDataRequest request = new SaveCustomDataRequest();
        request.setRef(ref);
        request.setObjectAlias(objectAlias);
        request.setDoFieldMerge(Boolean.TRUE);
        log4j.info("CustomDataWSClient  ref - >" + ref);
        log4j.info("JSONObject   - >" + data);


        JSONArray customArray = data.getJSONArray("customData");
        if (customArray != null && customArray.size() > 0) {
            List<CustomValue> customData = new ArrayList();

            for (int i = 0; i < customArray.size(); i++) {
                JSONObject customObject = customArray.getJSONObject(i);
                String attribute = customObject.getString("attribute");
                String value = customObject.getString("value");
                CustomValue cv = new CustomValue(attribute, value);
                customData.add(cv);
            }

            request.setCustomData(customData);
            CustomDataServiceInterface customDataService = MEServices.create("com.sap.me.customdata", "CustomDataService", site);
            customDataService.saveCustomData(request);
        }

        return null;
    }
}
