package com.foreverwin.mesnac.dataimport.reader;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csvreader.CsvReader;
import com.foreverwin.mesnac.dataimport.util.AppUtil;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Map;

public class CsvFileReader extends AbstractFileReader {

    private CsvReader csvReader;


    static Logger log4j = Logger.getLogger(CsvFileReader.class);

    @Override
    public int read(InputStream inputStream, Map<String, String> cvsheads) throws IOException {

        String fileEncode = "";
        try {
            //	fileEncode = EncodeUtil.getEncode(filePath, true);

            log4j.info("log 文件编码格式 ->" + fileEncode);

        } catch (Exception e1) {
            e1.printStackTrace();
        }


        int updated = 0;


        //InputStream inputStream=new FileInputStream(filePath);

        csvReader = new CsvReader((inputStream), Charset.forName(fileEncode));
        //inputStream, Charset.forName(encode));

        try {
            //	System.out.println(csvReader.readRecord());
            //	System.out.println(csvReader.());

            //String[] header = this.visitor.getHeader();
            String[] header = null;
            if (csvReader.readRecord()) {
                header = csvReader.getValues();//head reader

            }

            for (String s : header) {
                System.out.println(s);
            }

            //	String[] header=csvReader.getHeaders();//head reader

            //csvReader.skipRecord(); //不会更新csvReader.getCurrentRecord()
            while (csvReader.readRecord()) {
                String[] values = csvReader.getValues();

                System.out.println(values.length);

                if (!AppUtil.isNull(values)) {
                    JSONObject row = new JSONObject();

                    for (int i = 0; i < values.length && i < header.length; i++) {

                        String newKey = AppUtil.mapKeyFinder(cvsheads, header[i]);
                        if (newKey != null) {
                            row.put(newKey, values[i]);
                        } else {
                            //String _name=headNames[index++];

                            if (header[i] != null && header[i].trim().length() > 0 && header[i].startsWith("自定义-")) {
                                String cust = header[i].substring("自定义-".length());
                                //String value=data.get(name);
                                JSONObject customObject = new JSONObject();
                                customObject.put("attribute", cust);
                                customObject.put("value", values[i]);

                                JSONArray customArray = null;
                                if (row.containsKey("customData")) {

                                    customArray = row.getJSONArray("customData");
                                } else {
                                    customArray = new JSONArray();
                                    row.put("customData", customArray);
                                }


                                customArray.add(customObject);


                            }

                        }

                    }
                    log4j.info("get row info->" + row);
                    updated += this.visitor.visit(csvReader.getCurrentRecord() + 2, row); //行下标默认从0开始，跳过Header
                }


            }
        } finally {
            csvReader.close();
            inputStream.close();
        }

        return updated;
    }


}