package com.bocloud.ims.service.resource.h3c;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.common.TaskMsgDriver;
import com.bocloud.cmp.driver.compute.ServerDriver;
import com.bocloud.cmp.entity.enums.BocloudStatus;
import com.bocloud.cmp.model.h3c.H3cServerModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.CloudServer;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.h3c.H3cCloudServerService;
import com.bocloud.ims.repository.resource.CloudServerRepository;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.ResourceEventRepository;
import com.bocloud.ims.repository.resource.ResourceRepository;
import com.bocloud.ims.repository.resource.ServerRepository;
import com.bocloud.ims.service.utils.ResourceEventPublisher;
import com.bocloud.service.lock.LockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service("h3cCloudServerService")
public class H3cCloudServerServiceImpl extends H3cCommonService implements H3cCloudServerService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	LockFactory lockFactory;

	@Autowired
	CloudVendorRepository cloudVendorRepository;

	@Autowired
	CloudServerRepository cloudServerRepository;

	@Autowired
	ServerRepository serverRepository;

	@Autowired
	ServerDriver serverDriver;

	@Autowired
	ResourceRepository resourceRepository;

	@Autowired
	ResourceEventRepository resourceEventRepository;

	@Autowired
	ResourceEventPublisher resourceEventPublisher;

	@Autowired
	private TaskMsgDriver taskMsgDriver;

	@Override
	public BocloudResult console(Long id, Long userId) {
		try {
			CloudServer cloudServer = cloudServerRepository.query(id);
			// 云供应商
			CloudVendor vendor = cloudVendorRepository.query(cloudServer.getVendorId());
			if (null == vendor) {
				return new BocloudResult(false, "获取云供应商失败");
			}
			JSONObject authentication = JSONObject.parseObject(vendor.getAuthentication());
			BocloudResult result = serverDriver.getJnlpXml(vendor.getUuid(), null,
					Long.valueOf(cloudServer.getInstanceId()), authentication.getString("address"));
			if (result.isFailed()) {
				return result;
			}
			Map<String, String> resultMap = new HashMap<>();
			resultMap.put(Common.NAME, cloudServer.getName());
			resultMap.put(Common.VALUE, String.valueOf(result.getData()));
			return new BocloudResult(true, resultMap, "成功");
		} catch (Exception e) {
			logger.error("出错", e);
		}
		return new BocloudResult(false, "获取控制台失败");
	}

	private void putServerStatus(CloudServer server, H3cServerModel h3cServerModel) {
		if (h3cServerModel.getStatus().equals(2)) {
			server.setStatus(BocloudStatus.Server.RUNNING.name());
		} else if (h3cServerModel.getStatus().equals(3)) {
			server.setStatus(BocloudStatus.Server.STOPPED.name());
		}
	}

	@Override
	protected ResourceEventPublisher getResourceEventPublisher() {
		return this.resourceEventPublisher;
	}

	@Override
	protected TaskMsgDriver getTaskMsgDriver() {
		return this.taskMsgDriver;
	}

}
