package com.foreverwin.mesnac.dataimport.handler;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.dataimport.client.ResourceTypeWSClient;
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 com.sap.me.plant.ResourceTypeBasicConfiguration;

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

public class ResourceTypeHandler extends BaseHandler {

    @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("RESOURCE_TYPE");
    }

    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("resourceType");

                String resultMessage = null;
                try {
                    if (jsonObject.containsKey("resourceType") && jsonObject.getString("resourceType").trim().length() > 0) {
                        jsonObject.put("site", site);
                        Collection<ResourceTypeBasicConfiguration> collection = ResourceTypeWSClient.find(jsonObject);
                        if (collection.size() > 0) {
                            resultMessage = "资源类型主数据已经存在";
                        } else {
                            resultMessage = ResourceTypeWSClient.insert(jsonObject);
                            if (resultMessage == null) {
                                return 1;
                            }
                        }

                        if (resultMessage != null) {
                            params[2] = resultMessage;
                            failedNumber[0]++;
                            buffer.append("第" + index + "行：" + I18nUtil.getI18nText("MaterData.import.insert.fail", params) + "\n");
                            return 0;
                        }
                    }
                    return 0;
                } catch (Exception e) {
                    params[2] = e.getMessage();
                    failedNumber[0]++;
                    buffer.append("第" + index + "行：" + I18nUtil.getI18nText("MaterData.import.insert.fail", params) + "\n");
                }

                return 0;
            }

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