package com.ycwl.basic.device;

import com.ycwl.basic.device.checker.IDeviceStatusChecker;
import com.ycwl.basic.device.checker.impl.AliOssDeviceChecker;
import com.ycwl.basic.device.checker.impl.AlwaysOnDeviceChecker;
import com.ycwl.basic.device.enums.DeviceStoreTypeEnum;
import com.ycwl.basic.device.operator.AliOssStorageOperator;
import com.ycwl.basic.device.operator.IDeviceStorageOperator;
import com.ycwl.basic.device.operator.LocalStorageOperator;
import com.ycwl.basic.device.operator.VptPassiveStorageOperator;
import com.ycwl.basic.device.operator.WvpActiveStorageOperator;
import com.ycwl.basic.device.operator.WvpPassiveStorageOperator;
import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity;
import com.ycwl.basic.integration.device.dto.device.DeviceV2DTO;
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
import java.time.ZoneId;
import java.util.Date;

public class DeviceFactory {
    public static IDeviceStorageOperator getDeviceStorageOperator(DeviceV2DTO device, DeviceConfigEntity config) {
        IDeviceStorageOperator operator = null;
        if (config == null) {
            return null;
        }
        if (Integer.valueOf(DeviceStoreTypeEnum.ALI_OSS.getType()).equals(config.getStoreType())) {
            operator = new AliOssStorageOperator(config.getStoreConfigJson());
        } else if (Integer.valueOf(DeviceStoreTypeEnum.WVP_ACTIVE.getType()).equals(config.getStoreType())) {
            operator = new WvpActiveStorageOperator(config.getStoreConfigJson());
        } else if (Integer.valueOf(DeviceStoreTypeEnum.WVP_PASSIVE.getType()).equals(config.getStoreType())) {
            operator = new WvpPassiveStorageOperator(config.getStoreConfigJson());
        } else if (Integer.valueOf(DeviceStoreTypeEnum.VPT_PASSIVE.getType()).equals(config.getStoreType())) {
            operator = new VptPassiveStorageOperator(config.getStoreConfigJson());
        } else if (Integer.valueOf(DeviceStoreTypeEnum.AWS_OSS.getType()).equals(config.getStoreType())) {
            operator = new VptPassiveStorageOperator(config.getStoreConfigJson());
        } else if (Integer.valueOf(DeviceStoreTypeEnum.LOCAL.getType()).equals(config.getStoreType())) {
            operator = new LocalStorageOperator(config.getStoreConfigJson());
        }
        if (operator == null) {
            return null;
        }
        operator.setDevice(convertToEntity(device));
        operator.setDeviceConfig(config);
        return operator;
    }

    /**
     * 将DeviceV2DTO转换为DeviceEntity
     */
    private static DeviceEntity convertToEntity(DeviceV2DTO dto) {
        if (dto == null) {
            return null;
        }
        DeviceEntity entity = new DeviceEntity();
        entity.setId(dto.getId());
        entity.setName(dto.getName());
        entity.setNo(dto.getNo());
        entity.setScenicId(dto.getScenicId());
        entity.setStatus(dto.getIsActive());
        if (dto.getCreateTime() != null) {
            entity.setCreateAt(dto.getCreateTime());
        }
        if (dto.getUpdateTime() != null) {
            entity.setUpdateAt(dto.getUpdateTime());
        }
        return entity;
    }

    public static IDeviceStatusChecker getDeviceStatusChecker(DeviceEntity device, DeviceConfigEntity config) {
        IDeviceStatusChecker checker = null;
        if (config.getOnlineCheck() <= 0) {
            checker = new AlwaysOnDeviceChecker();
        } else {
            if (config.getStoreType() == DeviceStoreTypeEnum.ALI_OSS.getType()) {
                checker = new AliOssDeviceChecker(config.getStoreConfigJson());
            }
        }
        if (checker == null) {
            return null;
        }
        checker.setDevice(device);
        checker.setDeviceConfig(config);
        return checker;
    }
}
