package cokr.xit.ens.modules.nvsigntalk.model.struct;

import cokr.xit.ens.core.mapstruct.GenericMapper;
import cokr.xit.ens.core.mapstruct.StructMapperConfig;
import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.modules.nvsigntalk.domain.TmpltMngNvSigntalk;
import cokr.xit.ens.modules.nvsigntalk.model.TmpltMngNvSigntalkDTO;
import com.google.gson.Gson;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;
import java.util.Map;

@Mapper(config = StructMapperConfig.class)
public interface TmpltMngNvSigntalkMapper extends GenericMapper<TmpltMngNvSigntalkDTO, TmpltMngNvSigntalk> {

    @Override
    @Mapping(target = "orgMng.orgCd", source = "orgCd")
//    @Mapping(target = "messageDataFmt", expression = "java(tmpltMngNvSigntalkDTOToMessageDataFmt(tmpltMngNvSigntalkDTO.getMessageDataFmt()))")
    TmpltMngNvSigntalk toEntity(TmpltMngNvSigntalkDTO tmpltMngNvSigntalkDTO);

    @Override
    @Mapping(target = "orgCd", expression = "java(tmpltMngNvSigntalk.getOrgMng().getOrgCd())")
//    @Mapping(target = "messageDataFmt", expression = "java(tmpltMngNvSigntalkToMessageDataFmt(tmpltMngNvSigntalk.getMessageDataFmt()))")
    TmpltMngNvSigntalkDTO toDto(TmpltMngNvSigntalk tmpltMngNvSigntalk);


    default String tmpltMngNvSigntalkDTOToMessageDataFmt(Map<String, List<Map<String, String>>> messageDataFmt) {
        if (CmmnUtil.isEmpty(messageDataFmt)) return null;

        Gson gson = new Gson();
        String result = gson.toJson(messageDataFmt);
        return result;
    }

    default Map<String, List<Map<String, String>>> tmpltMngNvSigntalkToMessageDataFmt(String messageDataFmt) {
        if (CmmnUtil.isEmpty(messageDataFmt)) return null;

        Gson gson = new Gson();
        Map<String, List<Map<String, String>>> result = gson.fromJson(messageDataFmt, Map.class);
        return result;
    }


}
