package sgc.portal.base.util;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Websquare util.
 *
 * @author beomgeun.lee
 * @version 1.0.0
 * @since 2020. 6. 19.
 */
final class WqDataCollectionUtil {
    /**
     * private 생성자.
     *
     */
    private WqDataCollectionUtil() {
    }

    /**
     * getDataListByRowStatus.
     *
     * @param inDataList inDataList
     * @return HashMap data list by row status
     */
    public static HashMap<String, Object> getDataListByRowStatus(final List<HashMap<String, Object>> inDataList) {

        HashMap<String, Object> rst = new HashMap<>();
        List<HashMap<String, Object>> insertRows = new ArrayList<>();
        List<HashMap<String, Object>> updateRows = new ArrayList<>();
        List<HashMap<String, Object>> deleteRows = new ArrayList<>();

        if (inDataList != null && inDataList.size() > 0) {
            for (HashMap<String, Object> inDataMap : inDataList) {
                if (inDataMap.containsKey("rowStatus")) {
                    if ("C".equals(inDataMap.get("rowStatus"))) {
                        insertRows.add(inDataMap);
                    } else if ("U".equals(inDataMap.get("rowStatus"))) {
                        updateRows.add(inDataMap);
                    } else if ("D".equals(inDataMap.get("rowStatus"))) {
                        deleteRows.add(inDataMap);
                    }
                }
            }
        }
        rst.put("insertRows", insertRows);
        rst.put("updateRows", updateRows);
        rst.put("deleteRows", deleteRows);
        return rst;
    }

    /**
     * DataMap 또는 DataList에 session의 사용자 정보를 셋팅해서 return한다.
     *
     * @param request      request
     * @param inDataObject inDataObject
     * @return Object user info
     */
    @SuppressWarnings({ "unchecked" })
    public static Object setUserInfo(final HttpServletRequest request, final Object inDataObject) {
        Object rst = null;

        class SessionInfo {

            private final HttpServletRequest request;

            /**
             * 생성자 .
             *
             * @param request request
             */
            SessionInfo(final HttpServletRequest request) {
                this.request = request;
            }

            /**
             * map 에 세션정보 저장.
             *
             * @param map map
             * @return map
             */
            final HashMap<String, Object> setSessionInfo(final HashMap<String, Object> map) {

                String ssUserId = null;
                if (this.request != null && this.request.getSession() != null) {
                    ssUserId = (String) this.request.getSession().getAttribute("ssUserId");
                }

                // 로그인이 된 상태
                if (null != ssUserId) {
                    map.put("ssUserId", ssUserId);
                    map.put("ssUserName", this.request.getSession().getAttribute("ssUserName"));
                    map.put("ssOrgnId", this.request.getSession().getAttribute("ssOrgnId"));
                    map.put("ssOrgnName", this.request.getSession().getAttribute("ssOrgnName"));
                    map.put("ssSysAuth", this.request.getSession().getAttribute("ssSysAuth"));

                }

                return map;
            }
        }

        SessionInfo sessionInfo = new SessionInfo(request);

        if (inDataObject instanceof List) {

            ArrayList<HashMap<String, Object>> inList = (ArrayList<HashMap<String, Object>>) inDataObject;

            for (int i = 0; i < inList.size(); i++) {
                inList.set(i, sessionInfo.setSessionInfo(inList.get(i)));
            }
            rst = inList;

        } else if (inDataObject instanceof Map) {
            rst = sessionInfo.setSessionInfo((HashMap<String, Object>) inDataObject);
        }

        return rst;
    }
}
