package com.bocloud.ims.repository.resource.hmc;

import com.bocloud.ims.entity.common.MapBuilder;
import com.bocloud.ims.entity.resource.hmc.HmdServerVirtualNetwork;
import com.bocloud.ims.entity.resource.hmc.HmdServerVirtualSwitch;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
public abstract class HmdServerVirtualNetworkRepository extends HmdGenericDao<HmdServerVirtualNetwork> {

    @Lookup protected abstract HmdServerRepository serverRepository();
    @Lookup protected abstract HmdServerVirtualSwitchRepository serverVirtualSwitchRepository();

    public HmdServerVirtualNetworkRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    @Override
    public void extra(HmdServerVirtualNetwork network) {
        Optional.of(network).map(HmdServerVirtualNetwork::getServerId).map(this.serverRepository()::getById).ifPresent(network::setServer);
        Optional.of(network).map(HmdServerVirtualNetwork::getVirtualSwitchId).map(this.serverVirtualSwitchRepository()::getById).ifPresent(network::setVirtualSwitch);
    }

    public List<HmdServerVirtualNetwork> listByServerId(long serverId) {
        return this.search(Collections.singletonMap("a.server_id", serverId));
    }

    public List<HmdServerVirtualNetwork> listBySwitchId(long switchId) {
        return this.search(Collections.singletonMap("a.virtual_switch_id", switchId));
    }

    public HmdServerVirtualNetwork getById(long id) {
        return this.search(Collections.singletonMap("a.id", id)).stream().findFirst().orElse(null);
    }

    public HmdServerVirtualNetwork getByUuid(String uuid) {
        return this.search(Collections.singletonMap("a.uuid", uuid)).stream().findFirst().orElse(null);
    }

    public HmdServerVirtualNetwork getByVlanId(long serverId, long switchId, int vlanId) {
        Map<String, Object> map = MapBuilder.init()
            .put("a.server_id", serverId)
            .put("a.virtual_switch_id", switchId)
            .put("a.vlan_id", vlanId)
            .get();
        return this.search(map).stream().findFirst().orElse(null);
    }

    public List<HmdServerVirtualNetwork> search(long serverId, String bridgeName) {
        Map<String, Object> map = MapBuilder.init()
            .put("a.server_id", serverId)
            .put("a.name", bridgeName)
            .get();
        return this.search(map);
    }

    @Override
    public void clear(long id) {
        this.delete(id);
    }

    @Override
    public void convertInstance(final HmdServerVirtualNetwork srcT, final HmdServerVirtualNetwork desT) {
        Optional<HmdServerVirtualSwitch> vSwitch = Optional.of(srcT).map(HmdServerVirtualNetwork::getVirtualSwitch).map(HmdServerVirtualSwitch::getUuid).map(this.serverVirtualSwitchRepository()::getByUuid);
        vSwitch.map(HmdServerVirtualSwitch::getId).ifPresent(srcT::setVirtualSwitchId);
        vSwitch.ifPresent(srcT::setVirtualSwitch);

        this.copy(srcT, desT);
    }
}
