package enea.lecservice.services.mocks;

import com.fasterxml.jackson.core.type.TypeReference;
import enea.lecservice.dto.Form;
import enea.lecservice.models.dto.ServiceResponse;
import enea.lecservice.services.IFormService;
import enea.lecservice.utils.FileUtils;
import enea.lecservice.utils.JacksonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Log4j2
@Profile("dev")
@Service
public class MockedFormService implements IFormService {
    @Override
    public ServiceResponse<Object> get(String code, String token) {
        String fileName = "mocks/device.json";
        if (FileUtils.exists(fileName))
            return ServiceResponse.Success(
                    JacksonUtils.fromJson(FileUtils.getContentAsString(fileName), new TypeReference<Object>( ) {})
            );

        return ServiceResponse.Error("File mocked non trovato");
    }

    @Override
    public ServiceResponse<Object> post(Form form) {
        if (form == null)
            return ServiceResponse.Error("Dati dell form vuoti");

        if (form.getCode() == null || form.getCode().isEmpty())
            return ServiceResponse.Error("Il codice del form non può essere vuoto!");

        log.info(String.format("Data to post: %s", JacksonUtils.toJson(form)));
        return ServiceResponse.Success(true);
    }
}
