package com.bytesw.bytebot.etl.services;

import com.bytesw.bytebot.etl.beans.ProcessMessageResult;
import com.bytesw.bytebot.etl.dao.DeleteDataSensibleControlRepository;
import com.bytesw.bytebot.etl.dao.EventHistoryRepository;
import com.bytesw.bytebot.etl.enums.EventTypeEnum;
import com.bytesw.bytebot.etl.model.EventHistory;
import com.bytesw.bytebot.etl.utils.JsonUtils;
import com.bytesw.xdf.config.AppContextManager;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;

@Log4j2
@Service
public class ProcessMessageService {

    @Autowired
    private EventHistoryRepository eventHistoryRepository;

    @Autowired
    private DeleteDataSensibleControlRepository deleteDataSensibleControlRepository;

    public void saveHistory(Long id, String senderId) {
        EventHistory eventHistory = new EventHistory();
        eventHistory.setDate(OffsetDateTime.now());
        eventHistory.setEventId(id);
        eventHistory.setSenderId(senderId);
        eventHistoryRepository.save(eventHistory);
    }


    public ProcessMessageResult processMessage(String json, String senderId) throws Exception {
        ProcessMessageResult result = new ProcessMessageResult();
        MessageProcessed messageProcessed = detectType(json);
        if (messageProcessed == null) {
            result.setProcessed(false);
            result.setMessage("Event type not support");
            return result;
        }
        result.setProcessed(messageProcessed.process(json, senderId));
        return result;
    }

    private MessageProcessed detectType(String json) {
        MessageProcessed messageProcessed = null;
        EventTypeEnum type;
        try {
            String event = (String) JsonUtils.getFieldFromJson(json, "$.event");
            if (AppContextManager.getAppContext() != null && event != null) {
                String name = (String) JsonUtils.getFieldFromJson(json, "$.name");
                type = EventTypeEnum.fromString(event);
                if (type != null) {

                    // Verify if message is valid
                    if (EventTypeEnum.USER.equals(type)) {
                        String message_id = (String) JsonUtils.getFieldFromJson(json, "$.message_id");
                        if (message_id == null) {
                            return null;
                        }
                    }
                    if (EventTypeEnum.ACTION.equals(type) && EventTypeEnum.START_SESSION.getName().equals(name)) {
                        type = EventTypeEnum.START_SESSION;
                    }

                    messageProcessed = (MessageProcessed) AppContextManager.getAppContext().getBean(getMessageProcessedName(type));
                }
            }
        } catch (Exception e) {
            log.debug(e);
        }
        return messageProcessed;
    }

    private String getMessageProcessedName(EventTypeEnum type) {
        String[] words = type.toString().toLowerCase().split("_");
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (String word : words) {
            if (isFirst) {
                sb.append(word);
                isFirst = false;
            } else {
                sb.append(word.substring(0, 1).toUpperCase() + word.substring(1));
            }
        }
        sb.append("MessageProcessed");
        return sb.toString();
    }
}
