package com.sikey.wa04.business.application.converter;

import com.sikey.wa04.business.domain.entity.Child;
import com.sikey.wa04.business.domain.entity.Device;
import com.sikey.wa04.business.domain.entity.DeviceConfig;
import com.sikey.wa04.business.infrastructure.dto.response.user.SignInResponse;
import lombok.experimental.UtilityClass;

import java.util.Objects;

@UtilityClass
public class ChildConverter {

    public SignInResponse.SignInChild to(Child child) {
        SignInResponse.SignInChild signInChild = new SignInResponse.SignInChild()
                .setCid(child.getId())
                .setName(child.getName())
                .setAvatar(child.getAvatar())
//                .setRole()
                ;

        Device device = child.getDevice();
        if (device != null) {
            DeviceConfig config = device.getConfig();

            Integer battery = null;
            Integer signal = null;
            Integer maxSignal = null;
            Integer stepCount = null;
            if (Objects.nonNull(config)) {
                battery = config.getBattery();
                signal = config.getSignal();
                maxSignal = config.getMaxSignal();
                stepCount = config.getStepCount();
            }
            signInChild.setDevice(new SignInResponse.SignInChildDevice()
                    .setDid(device.getId().getId())
                    .setTicket(device.getTicket())
                    .setBattery(battery)
                    .setSignal(signal)
                    .setMaxSignal(maxSignal));
            signInChild.setStepCount(stepCount);
        }
        return signInChild;
    }
}
