package cokr.xit.ens.core.eventlistner._ignore;

import cokr.xit.ens.core.eventlistner._ignore.events.AcceptBeforeTransactionEvent;
import cokr.xit.ens.core.utils.MapDeserailizer;
import cokr.xit.ens.modules.kkomydoc.model.KkoMydocAcceptReqDTO;
import cokr.xit.ens.core.eventlistner._ignore.events.AcceptEvent;
import cokr.xit.ens.modules.kkomydoc.service.support.KkoMydocAcceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class KkoMydocExampleService {
    private final KkoMydocAcceptor acceptor;

    private final ApplicationEventPublisher applicationEventPublisher;

    public void process(String jsonStr){
        Gson gson = new GsonBuilder().registerTypeAdapter(Map.class, new MapDeserailizer()).serializeNulls().create();
        KkoMydocAcceptReqDTO reqDTO = gson.fromJson(jsonStr, KkoMydocAcceptReqDTO.class);

        applicationEventPublisher.publishEvent(new AcceptEvent(reqDTO));  
    }

    public void transactionProcess(){
        String jsonStr = "";
        AcceptBeforeTransactionEvent beforeEvent = new AcceptBeforeTransactionEvent() {
            @Override
            public void callback() {
                System.out.println(jsonStr);
            }
        };
        AcceptBeforeTransactionEvent afterEvent = new AcceptBeforeTransactionEvent() {
            @Override
            public void callback() {

            }
        };
        applicationEventPublisher.publishEvent(beforeEvent);
        applicationEventPublisher.publishEvent(afterEvent);
    }

}
