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

import com.bocloud.ims.entity.common.MapBuilder;
import com.bocloud.ims.entity.resource.hmc.HmdViosPhysicalDisk;
import com.bocloud.ims.entity.resource.hmc.HmdViosVirtualCd;
import com.bocloud.ims.entity.resource.hmc.HmdViosVirtualDisk;
import com.bocloud.ims.entity.resource.hmc.HmdViosVolumeGroup;
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 HmdViosVolumeGroupRepository extends HmdGenericDao<HmdViosVolumeGroup> {

    @Lookup protected abstract HmdPartitionRepository partitionRepository();
    @Lookup protected abstract HmdViosPhysicalDiskRepository viosPhysicalDiskRepository();
    @Lookup protected abstract HmdViosVirtualDiskRepository viosVirtualDiskRepository();
    @Lookup protected abstract HmdViosVirtualCdRepository viosVirtualCdRepository();

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

    @Override
    public void extra(HmdViosVolumeGroup vg) {
        Optional.of(vg).map(HmdViosVolumeGroup::getViosId).map(this.partitionRepository()::getById).ifPresent(vg::setVios);
        Optional.of(vg).map(HmdViosVolumeGroup::getId).map(this.viosPhysicalDiskRepository()::listByViosId).ifPresent(vg::setPhysicalDisks);
        Optional.of(vg).map(HmdViosVolumeGroup::getId).map(this.viosVirtualDiskRepository()::listByViosId).ifPresent(vg::setVirtualDisks);
        Optional.of(vg).map(HmdViosVolumeGroup::getId).map(this.viosVirtualCdRepository()::listByViosId).ifPresent(vg::setVirtualCds);
    }

    public List<HmdViosVolumeGroup> listByViosId(long viosId) {
        return this.search(Collections.singletonMap("a.vios_id", viosId));
    }

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

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

    public List<HmdViosVolumeGroup> search(long viosId, String groupName) {
        Map<String, Object> map = MapBuilder.init()
            .put("a.vios_id", viosId)
            .put("a.name", groupName)
            .get();
        return this.search(map);
    }

    @Override
    public void clear(long id) {
        this.viosPhysicalDiskRepository().listByViosId(id).stream().map(HmdViosPhysicalDisk::getId).forEach(this.viosPhysicalDiskRepository()::clear);
        this.viosVirtualDiskRepository().listByViosId(id).stream().map(HmdViosVirtualDisk::getId).forEach(this.viosVirtualDiskRepository()::clear);
        this.viosVirtualCdRepository().listByViosId(id).stream().map(HmdViosVirtualCd::getId).forEach(this.viosVirtualCdRepository()::clear);
    }

}
