package ru.nbch.indicsearchservice.loader;

import java.io.IOException;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.nbch.indicsearchservice.model.TableMetaData;
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 InformationTableLoader implements FirstLevelTableLoader, TableLoaderForId {
    private final MapperHolder mapperHolder;
    private final IndicSearchUtils indicSearchUtils;

    public InformationTableLoader(MapperHolder mapperHolder,
                                  IndicSearchUtils indicSearchUtils) {
        this.mapperHolder = mapperHolder;
        this.indicSearchUtils = indicSearchUtils;
    }

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

    @Override
    public Set<String> getTableNamesIfSuitable(DataType dataType, SearchParameter searchParameter) {
        if (dataType == DataType.TUTDF &&
            (searchParameter == SearchParameter.OLD_APP_NUM ||
                searchParameter == SearchParameter.SERIAL_NUM_APP ||
                searchParameter == SearchParameter.FID ||
                searchParameter == SearchParameter.PASSPORT ||
                searchParameter == SearchParameter.OGRN)) {
            return Set.of(getTableName());
        }

        return Set.of();
    }

    @Override
    @Transactional(readOnly = true)
    public void searchBy(UUID taskId,
                         DataType dataType,
                         SearchParameter searchParameter,
                         String searchValue,
                         String memberCode,
                         Map<String, Path> tableFileMap,
                         Map<String, TableMetaData> tableMetaDataMap) {
        if (dataType == DataType.TUTDF) {
            if (searchParameter == SearchParameter.OLD_APP_NUM || searchParameter == SearchParameter.SERIAL_NUM_APP) {
                try (Cursor<LinkedHashMap<String, Object>> cursor = mapperHolder.getIndicMapper().findAllByMemberCodeFromInformation(memberCode)) {
                    tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor, tableMetaDataMap));
                } 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().findAllByFidsFromInformation(fids)) {
                    tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor, tableMetaDataMap));
                } 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,
                           Map<String, TableMetaData> tableMetaDataMap) {
        if (dataType == DataType.TUTDF) {
            if (searchParameter == SearchParameter.PASSPORT || searchParameter == SearchParameter.OGRN) {
                try (Cursor<LinkedHashMap<String, Object>> cursor = mapperHolder.getIndicMapper().findAllByFidsFromInformation(fids)) {
                    tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor, tableMetaDataMap));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
