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.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.Volume;
import com.bocloud.ims.entity.resource.manageone.ManageOneAccount;
import com.bocloud.ims.interfaces.manageone.ManageOneAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

/**
 * 创建manage one云硬盘
 *
 * @author wangyu
 * @version 1.0
 * @since 2018年12月15日
 */
public class ManageOneVolumeBuilder implements VolumeBuilder {

	private static Logger logger = LoggerFactory.getLogger(ManageOneVolumeBuilder.class);
	private VolumeDriver driver;
	private VolumeModel model;
	private String code;
	private CloudVendor vendor;
	private String region;
	private RequestContext context;
	private ManageOneAccountService accountService;

	/**
	 * @param driver
	 * @param model
	 * @param code
	 * @param vendor
	 * @param region
	 * @param context
	 * @param accountService
	 */
	public ManageOneVolumeBuilder(VolumeDriver driver, VolumeModel model, String code, CloudVendor vendor,
			String region, RequestContext context, ManageOneAccountService accountService) {
		super();
		this.driver = driver;
		this.model = model;
		this.code = code;
		this.vendor = vendor;
		this.region = region;
		this.context = context;
		this.accountService = accountService;
	}

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

	@Override
	public BocloudResult<Volume> build() {
		BocloudResult result = null;
		if (null != context.getTenant() && 0L != context.getTenant()) {
			ManageOneAccount account = accountService.queryAccount(vendor.getId(), context.getTenant());
			Assert.notNull(account, "认证失败！");
			result = driver.create(vendor.getUuid(), account.getUsername(), account.getPassword(),
					account.getProjectId(), account.getDomainName(), model);
		} else {
			driver.create(vendor.getUuid(), code, model);
		}
		if (result.isFailed()) {
			Volume volume = new Volume();
			BeanUtils.copyProperties(model, volume);
			return new BocloudResult<>(false, volume, result.getMessage());
		}
		Volume volume = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), Volume.class);
		String status = volume.getStatus();
		volume.setStatus(BocloudStatus.Server.BUILDING.name());
		volume.setRegion(region);
		volume.preSave(context.getTarget(), context.getTarget(), context.getTenant(), vendor.getId(),
				context.getProject());
		volume.setOwnerId(context.getTarget());
		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.getVolumeUuid(), vendor.getUuid(), code);
			if (result.isSuccess()) {
				VolumeModel bean = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), VolumeModel.class);
				String state = bean.getStatus();
				if (state.equalsIgnoreCase(status)) {
					try {
						Thread.sleep(3000L);
					} catch (InterruptedException e) {
						logger.error("FusionCloudVolumeBuilder 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");
	}
}
