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

import com.bocloud.cmp.driver.compute.HostSeaDriver;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.Network;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.entity.resource.powervc.HostSeaAdapter;
import com.bocloud.ims.interfaces.resource.powervc.HostSeaService;
import com.bocloud.ims.repository.powervc.HostSeaRepository;
import com.bocloud.ims.repository.powervc.NetworkSeaRepository;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.NetworkRepository;
import com.bocloud.ims.repository.resource.ServerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class HostSeaServiceImpl implements HostSeaService {

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

    @Autowired
    private HostSeaRepository hostSeaRepository;
    @Autowired
    private NetworkSeaRepository networkSeaRepository;
    @Autowired
    private NetworkRepository networkRepository;
    @Autowired
    private CloudVendorRepository cloudVendorRepository;
    @Autowired
    private ServerRepository serverRepository;
    @Autowired
    private HostSeaDriver hostSeaDriver;

    @Override
    public BocloudResult list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter, boolean simple, RequestContext context) {
        try {
            int total = hostSeaRepository.count(params);
            List<HostSeaAdapter> list = hostSeaRepository.list(page, rows, params, sorter);
            GridBean gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult listServerAndSeaByVendor(long vendorId) {
        try {
            List<HostSeaAdapter> list = hostSeaRepository.listServerAndSeaByVendor(vendorId);
            return new BocloudResult(true, list, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult<List<HostSeaAdapter>> listByServerIdAndVlanId(long serverId, int vlanId) {
        try {
            List<HostSeaAdapter> list = hostSeaRepository.listByServerId(serverId);
            List<HostSeaAdapter> listWithVlan = new ArrayList<>();
            for (HostSeaAdapter hostSeaAdapter : list) {
                if (hostSeaAdapter.getPvid().equals(vlanId)) {
                    listWithVlan.add(hostSeaAdapter);
                }
            }
            if (listWithVlan.size() == 0) {
                listWithVlan.addAll(list);
            }
            return new BocloudResult(true, listWithVlan, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult(false, "查询失败");
        }
    }

    @Override
    public BocloudResult listByNetworkId(long networkId) {
        try {
            List<HostSeaAdapter> seas = networkSeaRepository.listHostSeaByNetworkId(networkId);
            if (seas.size() == 0) {
                Network network = networkRepository.query(networkId);
                if (network == null || network.getDeleted()) {
                    return new BocloudResult(false, "未找到网络");
                }
                List<Server> servers = serverRepository.listByVendor(network.getVendorId());
                for (Server server : servers) {
                    HostSeaAdapter sea = new HostSeaAdapter();
                    sea.setId(-1L);
                    sea.setServerId(server.getId());
                    sea.setServerHost(server.getHostname());
                    sea.setServerName(server.getName());
                    sea.setLparId(-1);
                    sea.setLparName("请勿使用");
                    sea.setSeaName("请勿使用");
                    sea.setState("不适用");
                    sea.setVswitch("不适用");
                    seas.add(sea);
                }
            }
            return new BocloudResult(true, seas, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult(false, "查询失败");
        }
    }
}
