package com.bocloud.ims.service.thread;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.storage.VolumeDriver;
import com.bocloud.cmp.entity.enums.BocloudStatus;
import com.bocloud.cmp.model.VolumeModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.Volume;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: FusionSphereVolumeBuilder
 * @Description:
 * @Author lxy
 * @Date 2021/11/10 17:42
 */
public class FusionSphereVolumeBuilder implements VolumeBuilder {
    private static Logger logger = LoggerFactory.getLogger(FusionSphereVolumeBuilder.class);
    private VolumeDriver driver;
    private VolumeModel model;
    private CloudVendor vendor;
    private RequestContext context;
    private String region;

    public FusionSphereVolumeBuilder(final VolumeDriver driver, final VolumeModel model, final CloudVendor vendor, final RequestContext context, String region) {
        this.driver = driver;
        this.model = model;
        this.vendor = vendor;
        this.context = context;
        this.region = region;
    }

    @Override
    public BocloudResult<Volume> build() {
        BocloudResult result = driver.create(vendor.getUuid(), model.getOsTenantName(), null, model);
        if (!result.isSuccess()) {
            return new BocloudResult<>(false, result.getMessage());
        }
        Volume volume = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), Volume.class);
        String status = volume.getStatus();
        volume.setCatalog(context.getCatalog());
        volume.preSave(context.getTarget(), context.getTarget(), context.getTenant(), vendor.getId(),
                context.getProject());
        volume.setVdcId(model.getVdcId());
        volume.setBootVolume(false);
        volume.setDataStoreId(model.getDataStoreId());
        Long start = System.currentTimeMillis();
        boolean running = true;
        while (running) {
            if (System.currentTimeMillis() - start > 1000 * 60 * 5) {
                volume.setStatus(BocloudStatus.Server.EXCEPTION.name());
                running = false;
            }
            result = driver.detail(volume.getUri(), vendor.getUuid(), null);
            if (result.isSuccess()) {
                VolumeModel bean = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), VolumeModel.class);
                String state = bean.getStatus();
                if (state.equalsIgnoreCase("CREATING")) {
                    try {
                        Thread.sleep(3000L);
                    } catch (InterruptedException e) {
                        logger.error("FusionSphereVolumeBuilder get InterruptedException : ", e);
                        Thread.currentThread().interrupt();
                    }
                } else {
                    volume.setStatus(state);
                    running = false;
                }
            } else {
                volume.setStatus(BocloudStatus.Server.EXCEPTION.name());
                running = false;
            }
        }
        return new BocloudResult<Volume>(true, volume, "success");
    }

    @Override
    public BocloudResult<Volume> call() throws Exception {
        return build();
    }
}
