package com.tofly.xrtymis.custom.service.export.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tofly.api.CodeClient;
import com.tofly.xrtymis.custom.commons.Constate;
import com.tofly.xrtymis.custom.service.TfYwpnValveService;
import com.tofly.xrtymis.custom.service.export.ExportValveService;
import com.tofly.xrtymis.entity.TfYwpnHydrant;
import com.tofly.xrtymis.entity.TfYwpnValve;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportValveService")
public class ExportValveServiceImpl implements ExportValveService {

    private final TfYwpnValveService valveService;
    private final CodeClient codeClient;

    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        List<String> list = Arrays.asList(dto.split(Constate.CSV));
        ArrayList<TfYwpnValve> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnValve valve = valveService.getById(id);
                if (valve != null) {
                    TfYwpnValve ywpnValve = tfYwpnValveCode2Name(valve);
                    result.add(ywpnValve);
                }
            }
        }
        List<Map<String, Object>> returnMap = new ArrayList<>();
        String resultListToString = JSONArray.toJSONStringWithDateFormat(result,"yyyy-MM-dd");
        List<Map> result1 = JSONObject.parseArray(resultListToString , Map.class);
        Map<String, Object> parentMap = new HashMap<>();
        parentMap.put("list", result1);
        returnMap.add(parentMap);
        return returnMap;
    }

    private TfYwpnValve tfYwpnValveCode2Name(TfYwpnValve valve) {
        if(StringUtils.isNotEmpty(valve.getValvetype())) {
            String valveType = codeClient.getname(Long.parseLong(valve.getValvetype()));
            if (StringUtils.isNotBlank(valveType)) {
                valve.setValvetype(valveType);
            }
        }
        if(StringUtils.isNotEmpty(valve.getValveclass())) {
            String valveclass = codeClient.getname(Long.parseLong(valve.getValveclass()));
            if (StringUtils.isNotBlank(valveclass)) {
                valve.setValveclass(valveclass);
            }
        }
        if(StringUtils.isNotEmpty(valve.getMaterial())) {
            String material = codeClient.getname(Long.parseLong(valve.getMaterial()));
            if (StringUtils.isNotBlank(material)) {
                valve.setMaterial(material);
            }
        }
        if(StringUtils.isNotEmpty(valve.getConnecttype())) {
            String connecttype = codeClient.getname(Long.parseLong(valve.getConnecttype()));
            if (StringUtils.isNotBlank(connecttype)) {
                valve.setConnecttype(connecttype);
            }
        }
        if(StringUtils.isNotEmpty(valve.getMotorized())) {
            String motorized = codeClient.getname(Long.parseLong(valve.getMotorized()));
            if (StringUtils.isNotBlank(motorized)) {
                valve.setMotorized(motorized);
            }
        }
        if(StringUtils.isNotEmpty(valve.getIslock())) {
            String islock = codeClient.getname(Long.parseLong(valve.getIslock()));
            if (StringUtils.isNotBlank(islock)) {
                valve.setIslock(islock);
            }
        }
        if(StringUtils.isNotEmpty(valve.getManholetype())) {
            String manholetype = codeClient.getname(Long.parseLong(valve.getManholetype()));
            if (StringUtils.isNotBlank(manholetype)) {
                valve.setManholetype(manholetype);
            }
        }
        if(StringUtils.isNotEmpty(valve.getManholematerial())) {
            String Manholematerial = codeClient.getname(Long.parseLong(valve.getManholematerial()));
            if (StringUtils.isNotBlank(Manholematerial)) {
                valve.setManholematerial(Manholematerial);
            }
        }
        if(StringUtils.isNotEmpty(valve.getWelltype())) {
            String welltype = codeClient.getname(Long.parseLong(valve.getWelltype()));
            if (StringUtils.isNotBlank(welltype)) {
                valve.setWelltype(welltype);
            }
        }
        if(StringUtils.isNotEmpty(valve.getWellmateial())) {
            String wellmateial = codeClient.getname(Long.parseLong(valve.getWellmateial()));
            if (StringUtils.isNotBlank(wellmateial)) {
                valve.setWellmateial(wellmateial);
            }
        }
        return valve;
    }

    @Override
    public Boolean excelDataStorage(List<Object> dataList) {
        return null;
    }
}
