package com.jiayue.ipp.idp.service.impl.an;

import com.jiayue.ipp.common.data.entity.an.ParsingInfo;
import com.jiayue.ipp.common.data.enums.FileTypeEnum;
import com.jiayue.ipp.idp.dto.KVPDto;
import com.jiayue.ipp.idp.service.an.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ParsingInfoServiceImpl implements ParsingInfoService {

    private final ParsingDqInfoService parsingDqInfoService;
    private final ParsingCdqInfoService parsingCdqInfoService;
    private final ParsingNwpInfoService parsingNwpInfoService;


    private final Map<String, ParsingInterface> parsingInterfaceMap;


    @Override
    public List<ParsingInfo> getParsingInfos() {

        List<ParsingInfo> parsingInfos = new ArrayList<>();

        parsingInfos.addAll(parsingDqInfoService.list());
        parsingInfos.addAll(parsingCdqInfoService.list());
        parsingInfos.addAll(parsingNwpInfoService.list());

        return parsingInfos;
    }

    @Override
    public List<KVPDto> getFileType() {
        FileTypeEnum[] values = FileTypeEnum.values();
        List<KVPDto> kvpDtos = new ArrayList<>();
        for (FileTypeEnum value : values) {

            KVPDto kvpDto = new KVPDto();

            kvpDto.setLabel(value.getMessage());
            kvpDto.setValue(value.name());
            kvpDtos.add(kvpDto);
        }
        return kvpDtos;
    }

    @Override
    public boolean save(ParsingInfo parsingInfo) {

        ParsingInterface parsingInterface = parsingInterfaceMap.get("parsing" + parsingInfo.getFileType() + "InfoServiceImpl");

        return parsingInterface.save(parsingInfo);
    }

    @Override
    public boolean deleteById(ParsingInfo parsingInfo) {
        ParsingInterface parsingInterface = parsingInterfaceMap.get("parsing" + parsingInfo.getFileType() + "InfoServiceImpl");

        return parsingInterface.deleteById(parsingInfo.getId());
    }


}
