package com.nokia.alarm.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import com.nokia.alarm.entity.ResultMap;

public class ExportUtil {
    public static String filePath = "csv";
    public static String fileNamePrifix = "resultMap";
    public static final String recordSeparator = "\r\n";
    public static final char delimiter = ',';

    public static String saveToCsv(List<ResultMap> results, Date date) {
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String dString = format.format(new Date());
        File file = new File(filePath, fileNamePrifix + dString + ".csv");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        Appendable writer = null;
        CSVPrinter printer = null;
        CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
                .setRecordSeparator(recordSeparator)
                .setDelimiter(delimiter)
                .build();
        try {
            writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
            printer = csvFormat.print(writer);
            for (ResultMap resultMap : results) {
                Object[] arr = toArray(resultMap, date);
                if (arr != null) {
                    printer.printRecord(arr);
                }
            }
            printer.flush();
            printer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dString;
    }

    private static Object[] toArray(ResultMap resultMap, Date date) {
        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        String sdate = format2.format(date);
        return new Object[] { sdate,
                resultMap.getUniqueId(),
                resultMap.getUniqueClearId(),
                resultMap.getEventTime(),
                resultMap.getCancelTime(),
                resultMap.getStatus(),
                resultMap.getProjectStatus(),
                resultMap.getVendorAlarmSerial(),
                resultMap.getAlarmTitle(),
                resultMap.getVendorSeverity(),
                resultMap.getVendorAlarmType(),
                resultMap.getSpecificProblem(),
                resultMap.getSpecificProblemID(),
                resultMap.getEquipmentClass(),
                resultMap.getEquipmentName(),
                resultMap.getLocateNeClass(),
                resultMap.getLocateNeName(),
                resultMap.getProvince(),
                resultMap.getRegion(),
                resultMap.getCity(),
                resultMap.getGrid(),
                resultMap.getArea(),
                resultMap.getVendor(),
                resultMap.getSpecialty(),
                resultMap.getSiteId(),
                resultMap.getRoomId(),
                resultMap.getTowerSiteNo(),
                resultMap.getProEquipmentStatus(),
                resultMap.getProjectName(),
                resultMap.getProjectNo(),
                resultMap.getProjectStartTime(),
                resultMap.getProjectEndTime(),
                resultMap.getWorkSendStatus(),
                resultMap.getWorkStatus(),
                resultMap.getWorkNo(),
                resultMap.getWorkArchivedTime(),
                resultMap.getWorkDutyDep(),
                resultMap.getWorkSendPerson(),
                resultMap.getAlarmLogicClass(),
                resultMap.getAlarmLogicSubClass(),
                resultMap.getStandardAlarmName(),
                resultMap.getAlarmSeverity(),
                resultMap.getStandardAlarmId(),
                resultMap.getEffectOnEquipment(),
                resultMap.getEffectOnBusiness(),
                resultMap.getStandardFlag(),
                resultMap.getNetworkType(),
                resultMap.getEquipmentDN(),
                resultMap.getLocateNEDN(),
                resultMap.getTacCode(),
                resultMap.getTacId(),
                resultMap.getAlarmText()
        };
    }
}
