package ru.nbch.indicsearchservice.loader;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.nbch.indicsearchservice.loader.subtable.RuUidSubTableLoader;
import ru.nbch.indicsearchservice.model.enums.DataType;
import ru.nbch.indicsearchservice.model.enums.SearchParameter;
import ru.nbch.indicsearchservice.util.IndicSearchUtils;
import ru.nbch.indicsearchservice.util.MapperHolder;

@Component
public class RuUidTableLoader implements FirstLevelTableLoader, TableLoaderForId {
    private final MapperHolder mapperHolder;
    private final IndicSearchUtils indicSearchUtils;
    private final List<RuUidSubTableLoader> ruUidSubTableLoaders;

    public RuUidTableLoader(MapperHolder mapperHolder,
                            IndicSearchUtils indicSearchUtils,
                            List<RuUidSubTableLoader> ruUidSubTableLoaders) {
        this.mapperHolder = mapperHolder;
        this.indicSearchUtils = indicSearchUtils;
        this.ruUidSubTableLoaders = ruUidSubTableLoaders;
    }

    @Override
    public String getTableName() {
        return "RU_UID";
    }

    @Override
    @Transactional(readOnly = true)
    public void searchBy(UUID taskId, DataType dataType, SearchParameter searchParameter, String searchValue, String memberCode, Map<String, Path> tableFileMap) {
        if (dataType == DataType.GUTDF || dataType == DataType.RUTDF) {
            if (searchParameter == SearchParameter.UUID || searchParameter == SearchParameter.ACCT_NUM) {
                try (Cursor<LinkedHashMap<String, Object>> cursor = mapperHolder.getIndicMapper().findAllByMemberCodeFromRuUid(memberCode)) {
                    Set<Integer> foundFids = new HashSet<>();
                    tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor, foundFids));
                    ruUidSubTableLoaders.parallelStream().forEach(tl -> tl.searchByRuUidFid(taskId, foundFids, tableFileMap));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (searchParameter == SearchParameter.FID) {
                Set<Integer> fids = Set.of(Integer.parseInt(searchValue));

                try (Cursor<LinkedHashMap<String, Object>> cursor = mapperHolder.getIndicMapper().findAllByFidsFromRuUid(fids)) {
                    tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor));
                    ruUidSubTableLoaders.parallelStream().forEach(tl -> tl.searchByRuUidFid(taskId, fids, tableFileMap));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    @Transactional(readOnly = true)
    public void searchById(UUID taskId, DataType dataType, SearchParameter searchParameter, Set<Integer> fids, String memberCode, Map<String, Path> tableFileMap) {
        if (dataType == DataType.GUTDF || dataType == DataType.RUTDF) {
            if (searchParameter == SearchParameter.PASSPORT || searchParameter == SearchParameter.OGRN) {
                try (Cursor<LinkedHashMap<String, Object>> cursor = mapperHolder.getIndicMapper().findAllByFidsFromRuUid(fids)) {
                    tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
