package com.foreverwin.mesnac.dataimport.handler;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.service.ToolService;
import com.foreverwin.mesnac.dataimport.handler.base.BaseHandler;
import com.foreverwin.mesnac.dataimport.reader.FileReader;
import com.foreverwin.mesnac.dataimport.reader.FileReaderBuilder;
import com.foreverwin.mesnac.dataimport.reader.RowVisitor;
import com.foreverwin.mesnac.dataimport.service.MasterObjectDefine;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.Map;

public class ToolHandler extends BaseHandler {

    @Autowired
    private ToolService toolService;


    @Override
    public String importFile(String site, InputStream inputStream, String fileType, String mode) throws Exception {
        int row = 0;
        StringBuffer buffer = new StringBuffer();
        Integer[] failedNumber = new Integer[]{0};

        try {
            FileReader fileReader = new FileReaderBuilder().build(fileType);
            RowVisitor<JSONObject> visitor = getRowVisitor(site, mode, buffer, failedNumber);
            row = fileReader.visitor(visitor).read(inputStream, getHeaders());
        } catch (Exception e) {
            buffer.append(e.getMessage() + "\n");
        }

        /*if (buffer.length() > 0) {
            buffer.insert(0, I18nUtil.getI18nText("MaterData.import.Summary", new Object[]{row + failedNumber[0], row, failedNumber[0]}) + "\n");
            throw BusinessException.build(buffer.toString());
        }*/
        buffer.insert(0, I18nUtil.getI18nText("MaterData.import.Summary", new Object[]{row + failedNumber[0], row, failedNumber[0]}) + "\n");

        return buffer.toString();
    }


    @Override
    public Map<String, String> getHeaders() {

        return MasterObjectDefine.getHeadsMapping("TOOL");
    }

    public RowVisitor<JSONObject> getRowVisitor(String site, String mode, StringBuffer buffer, Integer[] failedNumber) {

        return new RowVisitor<JSONObject>() {
            @Override
            public int visit(long index, JSONObject jsonObject) {
                Object[] params = new Object[10];
                params[0] = index;
                params[1] = jsonObject.getString("tool");
                jsonObject.put("site", site);
                try {
                    String msg = toolService.saveOrUpdateToolByImport(jsonObject, mode);
                    if (msg.indexOf("成功") == -1){
                        buffer.append("第" + index + "行【失败】：" + msg + "\n");
                        failedNumber[0]++;
                        return 0;
                    }
                    buffer.append("第" + index + "行【成功】：" + msg + "\n");
                    return 1;
                } catch (Exception e) {
                    params[2] = e.getMessage();
                    failedNumber[0]++;
                    buffer.append("第" + index + "行：" + I18nUtil.getI18nText("MaterData.import.update.fail", params) + "\n");
                }
                return 0;
            }

            @Override
            public String[] getHeader() {
                return null;
            }
        };
    }
}
