package com.bocloud.ims.service.resource.azure.automation;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.azure.automation.AutomationAccountDriver;
import com.bocloud.cmp.model.azure.automation.AutomationAccountModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.ResourceGroup;
import com.bocloud.ims.entity.resource.azure.automation.AutomationAccount;
import com.bocloud.ims.interfaces.resource.azure.automation.AutomationAccoutService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.ResourceGroupRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: AutomationAccoutServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/8/10 11:39
 */
@Service("automationAccoutService")
public class AutomationAccoutServiceImpl implements AutomationAccoutService {

    private static Logger logger = LoggerFactory.getLogger(AutomationAccoutServiceImpl.class);

    @Autowired
    private CloudVendorRepository vendorRepository;
    @Autowired
    private ResourceGroupRepository resourceGroupRepository;
    @Autowired
    private AutomationAccountDriver accountDriver;

    @Override
    public BocloudResult<GridBean<AutomationAccount>> list(Pager pager, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult<AutomationAccount> detail(Long id, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult<Void> modify(AutomationAccount entityParams, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult<Void> create(AutomationAccount entityParams, RequestContext context) {
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult<>(false, "平台验证失败");
            }
            ResourceGroup resourceGroup = resourceGroupRepository.query(entityParams.getResourceGroupId());
            if (ObjectUtils.isEmpty(resourceGroup)) {
                logger.error("get resourceGroup failure");
                return new BocloudResult<>(false, "资源组不能为空");
            }
            AutomationAccountModel model = new AutomationAccountModel();
            BeanUtils.copyProperties(entityParams, model);
            model.setResourceGroupName(resourceGroup.getName());
            BocloudResult result = accountDriver.create(vendor.getUuid(), entityParams.getRegionId(), model);
            if (result.isFailed()) {
                return new BocloudResult<>(false, result.getMessage());
            }
            return new BocloudResult<>(true, "创建成功");
        } catch (Exception e) {
            logger.error("create automationAccout error!", e);
            return new BocloudResult<>(false, "创建失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<Void> remove(Long id, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult list(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        Long vendorId = object.getLong("vendorId");
        CloudVendor vendor = vendorRepository.query(vendorId);
        if (ObjectUtils.isEmpty(vendor)) {
            logger.error("get cloudVendor failure");
            return new BocloudResult(false, "平台验证失败");
        }
        return accountDriver.list(vendor.getUuid());
    }

    @Override
    public BocloudResult detail(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        AutomationAccount entityParams = JSONObject.parseObject(object.getString("automationAccout"), AutomationAccount.class);
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult(false, "平台验证失败");
            }
            AutomationAccountModel model = new AutomationAccountModel();
            BeanUtils.copyProperties(entityParams, model);
            return accountDriver.detail(vendor.getUuid(), model);
        } catch (Exception e) {
            logger.error("detail automationAccout error!", e);
            return new BocloudResult(false, "请求失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult remove(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        AutomationAccount entityParams = JSONObject.parseObject(object.getString("automationAccout"), AutomationAccount.class);
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult<>(false, "平台验证失败");
            }
            AutomationAccountModel model = new AutomationAccountModel();
            BeanUtils.copyProperties(entityParams, model);
            return accountDriver.remove(vendor.getUuid(), model);
        } catch (Exception e) {
            logger.error("remove automationAccout error!", e);
            return new BocloudResult<>(false, "请求失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult getStatusCounts(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        String resourceId = object.getString("resourceId");
        AutomationAccount entityParams = JSONObject.parseObject(object.getString("automationAccout"), AutomationAccount.class);
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult(false, "平台验证失败");
            }
            AutomationAccountModel model = new AutomationAccountModel();
            BeanUtils.copyProperties(entityParams, model);
            return accountDriver.getStatusCounts(vendor.getUuid(), model, resourceId);
        } catch (Exception e) {
            logger.error("getStatusCounts automationAccout error!", e);
            return new BocloudResult(false, "请求失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult softwareUpdateConfigurationMachineRuns(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        String resourceId = object.getString("resourceId");
        AutomationAccount entityParams = JSONObject.parseObject(object.getString("automationAccout"), AutomationAccount.class);
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult(false, "平台验证失败");
            }
            AutomationAccountModel model = new AutomationAccountModel();
            BeanUtils.copyProperties(entityParams, model);
            return accountDriver.softwareUpdateConfigurationMachineRuns(vendor.getUuid(), model, resourceId);
        } catch (Exception e) {
            logger.error("softwareUpdateConfigurationMachineRuns automationAccout error!", e);
            return new BocloudResult(false, "请求失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult hybridRunbookWorkerGroups(String params) {
        JSONObject messageObject = new JSONObject();
        JSONObject object = JSONTools.isJSONObj(params);
        String resourceId = object.getString("resourceId");
        AutomationAccount entityParams = JSONObject.parseObject(object.getString("automationAccout"), AutomationAccount.class);
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult(false, "平台验证失败");
            }
            AutomationAccountModel model = new AutomationAccountModel();
            BeanUtils.copyProperties(entityParams, model);
            BocloudResult result = accountDriver.hybridRunbookWorkerGroups(vendor.getUuid(), model, resourceId);

            if (result.isFailed()) {
                messageObject.put("message", false);
                return new BocloudResult(true, messageObject, "查询成功");
            }
            messageObject.put("message", true);
            return new BocloudResult(true, messageObject, "查询成功");
        } catch (Exception e) {
            messageObject.put("message", false);
            return new BocloudResult(true, messageObject, "查询成功");
        }
    }

}