package com.eactive.eai.rms.onl.loader.service;

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.converter.ElinkObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class TransactionDeployJsonEmptyService extends BaseService implements TransactionDeployJsonService  {

    protected final ElinkObjectMapper mapper = new ElinkObjectMapper();;

    public TransactionDeployJsonEmptyService() {
        mapper.enable(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL);
        mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        mapper.writerWithDefaultPrettyPrinter();
    }

    @Override
    public void insertLayoutLoading(String path, String fileName) throws Exception {

    }

    @Override
    public void insertTransformLoading(String path, String fileName) throws Exception {

    }

    @Override
    public void insertInterfaceLoading(String path, String fileName) throws Exception {

    }

    @Override
    public String getInterface(String serviceName) throws Exception {
        return null;
    }

    @Override
    public String getLayout(String layoutName) throws Exception {
        return null;
    }

    @Override
    public String getTransform(String transformName) throws Exception {
        return null;
    }

    @Override
    public void interfaceDownLoading(String path, String serviceName) throws Exception {

    }

    @Override
    public void layoutDownLoading(String path, String layoutName) throws Exception {

    }

    @Override
    public void transformDownLoading(String path, String transformName) throws Exception {

    }

    protected String load(String filePath, String encoding) {
        File file = new File(filePath);
        byte[] data = null;
        try (ByteArrayOutputStream outStream = new ByteArrayOutputStream();
             FileInputStream fis = new FileInputStream(file))
        {
            int c = -1;
            byte[] bytes = new byte[1024];

            while ((c = fis.read(bytes, 0, bytes.length)) != -1) {
                outStream.write(bytes, 0, c);
            }
            data = new byte[outStream.toByteArray().length];
            data = outStream.toByteArray();
        } catch (Exception ignored) {
        }

        try {
            return new String(data, encoding);
        } catch (UnsupportedEncodingException e) {
            return new String(data);
        }
    }

    protected String load(String filePath) throws Exception {
        return load(filePath, Charset.defaultCharset().name());
    }

}
