package com.bocloud.ims.service.resource;

import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.FlashCopy;
import com.bocloud.ims.entity.resource.Volume;
import com.bocloud.ims.interfaces.resource.FlashCopyService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.FlashCopyRepository;
import com.bocloud.ims.repository.resource.VolumeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("flashCopyService")
public class FlashCopyServiceImpl implements FlashCopyService {

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

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private FlashCopyRepository flashCopyRepository;
    @Autowired
    private VolumeRepository volumeRepository;
    @Autowired
    private CloudVendorRepository vendorRepository;

    @Override
    public BocloudResult<GridBean<FlashCopy>> list(Pager pager, Long volumeId) {
        try {
            int page = pager.getPage();
            int rows = pager.getRows();
            List<Param> params = pager.getParams();
            Map<String, String> sorter = pager.getSorter();
            boolean simple = pager.getSimple();

            List<FlashCopy> list = flashCopyRepository.listSimple(page, rows, params, sorter, volumeId);
            int total = flashCopyRepository.countSimple(params, volumeId);
            GridBean gridBean = new GridBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult listTargetVolumeFromDriver(long volumeId) {
        try {
            Volume sourceVolume = volumeRepository.query(volumeId);
            if (sourceVolume == null || sourceVolume.getDeleted()) {
                return new BocloudResult<>(false, "未找到源卷或该源卷已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(sourceVolume.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.listTargetVolumeFromDriver(volumeId);
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult<>(false, "查询失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<GridBean> listTargetVolumeFromDB(int page, int rows, long volumeId, Map<String, Object> params, Map<String, String> sorter) {
        try {
            List<Volume> list = volumeRepository.listTargetVolumeOfFlashCopyBySourceVolume(page, rows, volumeId, params, sorter);
            int total = volumeRepository.countTargetVolumeOfFlashCopyBySourceVolume(volumeId, params);
            GridBean gridBean = new GridBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult create(FlashCopy flashCopy, RequestContext context) {
        try {
            if (flashCopy.getSourceVolumeId() == null) {
                return new BocloudResult<>(false, "需要参数sourceVolumeId");
            }
            Volume sourceVolume = volumeRepository.query(flashCopy.getSourceVolumeId());
            if (sourceVolume == null || sourceVolume.getDeleted()) {
                return new BocloudResult<>(false, "未找到源卷或该源卷已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(sourceVolume.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.create(flashCopy, context);
        } catch (Exception e) {
            logger.error("创建失败：", e);
            return new BocloudResult<>(false, "创建失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult modify(FlashCopy flashCopy, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult remove(long id, boolean force, RequestContext context) {
        try {
            FlashCopy flashCopy = flashCopyRepository.get(id);
            if (flashCopy == null || flashCopy.getDeleted()) {
                return new BocloudResult<>(false, "未找到FlashCopy或该FlashCopy已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(flashCopy.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.remove(id, force, context);
        } catch (Exception e) {
            logger.error("删除失败：", e);
            return new BocloudResult<>(false, "删除失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult start(long id, RequestContext context) {
        try {
            FlashCopy flashCopy = flashCopyRepository.get(id);
            if (flashCopy == null || flashCopy.getDeleted()) {
                return new BocloudResult<>(false, "未找到FlashCopy或该FlashCopy已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(flashCopy.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.start(id, context);
        } catch (Exception e) {
            logger.error("启动失败：", e);
            return new BocloudResult<>(false, "启动失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult stop(long id, RequestContext context) {
        try {
            FlashCopy flashCopy = flashCopyRepository.get(id);
            if (flashCopy == null || flashCopy.getDeleted()) {
                return new BocloudResult<>(false, "未找到FlashCopy或该FlashCopy已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(flashCopy.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.stop(id, context);
        } catch (Exception e) {
            logger.error("停止失败：", e);
            return new BocloudResult<>(false, "停止失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<FlashCopy> detail(long id) {
        try {
            FlashCopy flashCopy = flashCopyRepository.get(id);
            return new BocloudResult<>(true, flashCopy, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult moveIn(long id, long groupId, RequestContext context) {
        try {
            FlashCopy flashCopy = flashCopyRepository.get(id);
            if (flashCopy == null || flashCopy.getDeleted()) {
                return new BocloudResult<>(false, "未找到FlashCopy或该FlashCopy已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(flashCopy.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.moveIn(id, groupId, context);
        } catch (Exception e) {
            logger.error("移动失败：", e);
            return new BocloudResult<>(false, "移动失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult moveOut(long id, RequestContext context) {
        try {
            FlashCopy flashCopy = flashCopyRepository.get(id);
            if (flashCopy == null || flashCopy.getDeleted()) {
                return new BocloudResult<>(false, "未找到FlashCopy或该FlashCopy已经被删除");
            }
            CloudVendor cloudVendor = vendorRepository.query(flashCopy.getVendorId());
            if (cloudVendor == null || cloudVendor.getDeleted()) {
                return new BocloudResult<>(false, "未找到平台或该平台已经被删除");
            }
            FlashCopyService service = applicationContext.getBean(cloudVendor.getType().toLowerCase() + FlashCopyService.class.getSimpleName(), FlashCopyService.class);
            return service.moveOut(id, context);
        } catch (Exception e) {
            logger.error("移动失败：", e);
            return new BocloudResult<>(false, "移动失败：" + e.getMessage());
        }
    }
}
