package com.eactive.eai.rms.onl.dashboard.modern.backup.mapper;

import com.eactive.eai.rms.common.util.TypeCastUtils;
import com.eactive.eai.rms.onl.dashboard.modern.backup.BackupContentVO;
import com.eactive.eai.rms.onl.dashboard.modern.service.ThroughputCalculationService;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class ThroughputVoMapper implements BackupMapper<BackupContentVO.ThroughputFields, ThroughputCalculationService> {

    @Override
    public BackupContentVO.ThroughputFields toVo(ThroughputCalculationService throughputCalculationService) throws Exception {
        BackupContentVO.ThroughputFields throughputFields = new BackupContentVO.ThroughputFields();
        for (Field field : BackupContentVO.ThroughputFields.class.getDeclaredFields()) {
            Method method = Arrays.stream(BackupContentVO.ThroughputFields.class.getDeclaredMethods()).filter(m -> m.getName().equalsIgnoreCase("set" + field.getName())).findAny().orElse(null);
            if (method != null)
                method.invoke(throughputFields, TypeCastUtils.castOrElse(getReflection(ThroughputCalculationService.class, field.getName(), throughputCalculationService), field.getType(), null));
        }
        return throughputFields;
    }

    @Override
    public void setVoValuesToObject(BackupContentVO.ThroughputFields vo, ThroughputCalculationService object) throws Exception {
        for (Field field : BackupContentVO.ThroughputFields.class.getDeclaredFields()) {
            Method method = Arrays.stream(BackupContentVO.ThroughputFields.class.getDeclaredMethods()).filter(m -> m.getName().equalsIgnoreCase("get" + field.getName())).findAny().orElse(null);
            if (method != null)
                setReflection(ThroughputCalculationService.class, field.getName(), object, TypeCastUtils.castOrElse(method.invoke(vo), field.getType(), null));
        }
    }

}
