package ru.yandex.autotests.direct.fakebsproxy.service;

import org.springframework.stereotype.Service;
import ru.yandex.autotests.direct.fakebsproxy.beans.FakeBSProxyLogBean;
import ru.yandex.autotests.direct.fakebsproxy.dao.FakeBSProxyLogBeanMongoHelper;
import ru.yandex.autotests.direct.fakebsproxy.service.utils.FakeBsProxyHttpClient;

import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.net.URI;

/**
 * Created by buhter on 10/11/15.
 */
@Service
public class FakeBSProxyClientDataService {
    private static final FakeBSProxyLogBeanMongoHelper MONGO_HELPER = new FakeBSProxyLogBeanMongoHelper();

    //TODO: NOT READY FOR USE!!!
    public String handleRequest(FakeBSProxyLogBean logBean, String xml) {
        logBean.setRequestUUID("BUGGY");
        logBean.setRequestEntity(xml);
        try {
            logBean.setResponseEntity(
                    new FakeBsProxyHttpClient().post(URI.create("http://perl-fake-services.test.direct.yandex.ru:17080/FakeBS/"), xml, logBean.getHeaders(), MediaType.TEXT_XML)
            );
        } catch (IOException e) {
            logBean.setResponseEntity(e.getMessage());
            MONGO_HELPER.addMongoBean(logBean);
            return e.getMessage();
        }

        logBean.setResponseEntity(logBean.getResponseEntity().replace("<EID x", "<Error xsi:type=\"xsd:int\">1</Error><ErrorMessage xsi:type=\"xsd:string\">23rfadvdasfvasfdsv't exist</ErrorMessage><EID x"));
        MONGO_HELPER.addMongoBean(logBean);
        return logBean.getResponseEntity();
    }
}
