package org.nl.acs.udw.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.nl.acs.task.service.dto.TaskDto;
import org.nl.acs.udw.UnifiedData;
import org.nl.acs.udw.UnifiedDataAccessorFactory;
import org.nl.acs.udw.dto.UdwDto;
import org.nl.acs.udw.service.UdwManageService;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UdwManagerServiceImpl implements UdwManageService {

    public UdwManagerServiceImpl() {
    }

    @Override
    public List<UdwDto> queryByConditions(JSONObject where) {
        String unified_key = null;
        String keys = null;

        UnifiedDataUnit unifiedDataUnit = UnifiedDataAccessorFactory.getUnifiedDataAppService().getUnifiedDataUnit(unified_key);
        if (unifiedDataUnit == null) {
            return null;
        } else {
            String key;
            UdwDto udwDto;
            Map storage;
            ArrayList udwDtos;
            Iterator var14;
            if (keys != null) {
                storage = unifiedDataUnit.getStorage();
                udwDtos = new ArrayList();
                var14 = storage.keySet().iterator();

                while(var14.hasNext()) {
                    key = (String)var14.next();
                    if (key.indexOf(keys) != -1) {
                        udwDto = new UdwDto();
                        udwDto.setUnified_key(unified_key);
                        udwDto.setKey(key);
                        udwDto.setValue(((UnifiedData)storage.get(key)).getValue());
                        udwDtos.add(udwDto);
                    }
                }

                return udwDtos;
            } else {
                storage = unifiedDataUnit.getStorage();
                udwDtos = new ArrayList();
                var14 = storage.keySet().iterator();

                while(var14.hasNext()) {
                    key = (String)var14.next();
                    udwDto = new UdwDto();
                    udwDto.setUnified_key(unified_key);
                    udwDto.setKey(key);
                    udwDto.setValue(((UnifiedData)storage.get(key)).getValue());
                    udwDtos.add(udwDto);
                }

                return udwDtos;
            }
        }
    }

    @Override
    public Map<String, Object> queryAll(Map whereJson, Pageable page) {

        String unified_key = (String) whereJson.get("unified_key");
        String keys = (String) whereJson.get("code");
        if(StrUtil.isEmpty(unified_key))
        {
            unified_key = "opc_value";
        }
//        String unified_key = (String) whereJson.get("unified_key");
//        String code = (String) whereJson.get("code");

//        unified_key = whereJson.get("key").toString();
//        keys = whereJson.get("value").toString();


        //[[{"column":"unified_key","value":"cached","compareType":"equals","columnType":"object"}]]
        UnifiedDataUnit unifiedDataUnit = UnifiedDataAccessorFactory.getUnifiedDataAppService().getUnifiedDataUnit(unified_key);
        if (unifiedDataUnit == null) {
            return null;
        } else {
            String key;
            UdwDto udwDto;
            Map storage;
            ArrayList udwDtos;
            Iterator var14;
            if (keys != null) {
                storage = unifiedDataUnit.getStorage();
                udwDtos = new ArrayList();
                var14 = storage.keySet().iterator();

                while(var14.hasNext()) {
                    key = (String)var14.next();
                    if (key.indexOf(keys) != -1) {
                        udwDto = new UdwDto();
                        udwDto.setUnified_key(unified_key);
                        udwDto.setKey(key);
                        udwDto.setValue(((UnifiedData)storage.get(key)).getValue());
                        udwDtos.add(udwDto);
                    }
                }

                Integer currentPageNumber = page.getPageNumber() + 1;
                Integer pageMaxSize = page.getPageSize();

                List orderbyDtoList = (List) udwDtos.stream().skip((currentPageNumber - 1) * pageMaxSize)
                        .limit(pageMaxSize)
                        .collect(Collectors.toList());

                JSONObject jo = new JSONObject();
                jo.put("content", orderbyDtoList);
                jo.put("totalElements", udwDtos.size());

                return jo;
            } else {
                storage = unifiedDataUnit.getStorage();
                udwDtos = new ArrayList();
                var14 = storage.keySet().iterator();

                while(var14.hasNext()) {
                    key = (String)var14.next();
                    udwDto = new UdwDto();
                    udwDto.setUnified_key(unified_key);
                    udwDto.setKey(key);
                    udwDto.setValue(((UnifiedData)storage.get(key)).getValue());
                    udwDtos.add(udwDto);
                }
                Integer currentPageNumber = page.getPageNumber() + 1;
                Integer pageMaxSize = page.getPageSize();

                List orderbyDtoList = (List) udwDtos.stream().skip((currentPageNumber - 1) * pageMaxSize)
                        .limit(pageMaxSize)
                        .collect(Collectors.toList());

                JSONObject jo = new JSONObject();
                jo.put("content", orderbyDtoList);
                jo.put("totalElements", udwDtos.size());

                return jo;
            }
        }
    }
}
