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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.azure.automation.DeploymentDriver;
import com.bocloud.cmp.model.azure.automation.AutomationAccountModel;
import com.bocloud.cmp.model.azure.automation.DeploymentPlanModel;
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.azure.automation.AutomationAccount;
import com.bocloud.ims.entity.resource.azure.automation.DeploymentPlan;
import com.bocloud.ims.interfaces.resource.azure.automation.DeploymentService;
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: DeploymentServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/8/11 18:09
 */
@Service("deploymentService")
public class DeploymentServiceImpl implements DeploymentService {

    private static Logger logger = LoggerFactory.getLogger(DeploymentServiceImpl.class);
    @Autowired
    private CloudVendorRepository vendorRepository;
    @Autowired
    private ResourceGroupRepository resourceGroupRepository;
    @Autowired
    private DeploymentDriver driver;

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

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

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

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

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

    @Override
    public BocloudResult listDeploymentPlan(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        DeploymentPlan entityParams = JSONObject.parseObject(object.getString("deploymentPlan"), DeploymentPlan.class);
        try {
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult(false, "平台验证失败");
            }
            DeploymentPlanModel model = new DeploymentPlanModel();
            BeanUtils.copyProperties(entityParams, model);
            return driver.listDeploymentPlan(vendor.getUuid(), model);
        } catch (Exception e) {
            logger.error("deployment plan error!", e);
            return new BocloudResult(false, "自动化账户启动部署失败，请重新部署。或者刚部署成功，数据没有更新请稍后查询！");
        }
    }

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

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

    @Override
    public BocloudResult deploymentAutomationAccount(String params) {
        JSONObject object = JSONTools.isJSONObj(params);
        String name = object.getString("name");
        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 = driver.deploymentAutomationAccount(vendor.getUuid(), model.getRegionId(), model, name);
            if (result.isFailed()) {
                return new BocloudResult(false, result.getMessage());
            }
            return new BocloudResult(true, "启动成功");
        } catch (Exception e) {
            logger.error("deployment automationAccout error!", e);
            return new BocloudResult(false, "启动失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult softwareUpdateConfigurationRuns(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 driver.softwareUpdateConfigurationRuns(vendor.getUuid(), model.getRegionId(), model);
        } catch (Exception e) {
            logger.error("deployment softwareUpdateConfigurationRuns error!", e);
            return new BocloudResult(false, "查询失败:" + e.getMessage());
        }
    }

}