package com.bocloud.cmp.model;

import com.bocloud.cmp.model.manageone.VdcModel;
import com.bocloud.common.utils.ListTool;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * 华为manage one数据同步model
 *
 * @author wangyu
 * @version 3.0.0-SNAPSHOT
 * @since 2018/9/25
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ManageOneSyncModel {
    private List<RegionModel> regions;
    private List<HostModel> hostModels;
    private List<ServerModel> serverModels;
    private List<NetworkModel> networkModels;
    private List<VolumeModel> volumeModels;
    private List<VolumeTypeModel> volumeTypeModels;
    private List<SnapshotModel> snapshotModels;
    private List<ImageModel> imageModels;
    private List<FlavorModel> flavorModels;
    private List<PortModel> portModels;
    private List<RouterModel> routerModels;
    private List<SecurityGroupModel> groupModels;
    private List<KeypairModel> keypairModels;
    private List<ClusterModel> clusterModels;
    private List<FloatingIpModel> floatingIpModels;
    private List<FirewallModel> firewallModels;
    private List<FirewallPolicyModel> firewallPolicyModels;
    private List<FirewallRuleModel> firewallRuleModels;
    private List<VdcModel> vdcModels;
    private List<VpcModel> vpcModels;
    private List<StorageModel> storageModels; // 存储池

    private List<ManageOneProjectModel> manageOneProjectModels;

    public static <T> List<T> addAll(List<T> oldObjects, List<T> newObjects) {
        oldObjects = ListTool.isEmpty(oldObjects) ? new ArrayList<>() : oldObjects;
        newObjects = ListTool.isEmpty(newObjects) ? new ArrayList<>() : newObjects;
        oldObjects.addAll(newObjects);
        return oldObjects;
    }

    public static ManageOneSyncModel distinct(ManageOneSyncModel model) {
        Map<String, RegionModel> regionMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getRegions())) {
            model.getRegions().forEach(r -> regionMap.put(r.getName(), r));
        }
        model.setRegions(new ArrayList<>(regionMap.values()));

        Map<String, HostModel> hostMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getHostModels())) {
            model.getHostModels().forEach(r -> hostMap.put(r.getUuid(), r));
        }
        model.setHostModels(new ArrayList<>(hostMap.values()));

        Map<String, ServerModel> serverMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getServerModels())) {
            model.getServerModels().forEach(r -> serverMap.put(r.getInstanceId(), r));
        }
        model.setServerModels(new ArrayList<>(serverMap.values()));

        Map<String, NetworkModel> networkMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getNetworkModels())) {
            model.getNetworkModels().forEach(r -> networkMap.put(r.getNetworkUuid(), r));
        }
        model.setNetworkModels(new ArrayList<>(networkMap.values()));

        Map<String, VolumeModel> volumeMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getVolumeModels())) {
            model.getVolumeModels().forEach(r -> volumeMap.put(r.getVolumeUuid(), r));
        }
        model.setVolumeModels(new ArrayList<>(volumeMap.values()));

        Map<String, VolumeTypeModel> volumeTypeMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getVolumeTypeModels())) {
            model.getVolumeTypeModels().forEach(r -> volumeTypeMap.put(r.getVolumeTypeUuid(), r));
        }
        model.setVolumeTypeModels(new ArrayList<>(volumeTypeMap.values()));

        Map<String, SnapshotModel> snapshotMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getSnapshotModels())) {
            model.getSnapshotModels().forEach(r -> snapshotMap.put(r.getSnapshotUuid(), r));
        }
        model.setSnapshotModels(new ArrayList<>(snapshotMap.values()));

        Map<String, ImageModel> imageMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getImageModels())) {
            model.getImageModels().forEach(r -> imageMap.put(r.getImageUuid(), r));
        }
        model.setImageModels(new ArrayList<>(imageMap.values()));

        Map<String, FlavorModel> flavorMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getFlavorModels())) {
            model.getFlavorModels().forEach(r -> flavorMap.put(r.getFlavorUuid(), r));
        }
        model.setFlavorModels(new ArrayList<>(flavorMap.values()));

        Map<String, PortModel> portMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getPortModels())) {
            model.getPortModels().forEach(r -> portMap.put(r.getPortUuid(), r));
        }
        model.setPortModels(new ArrayList<>(portMap.values()));

        Map<String, RouterModel> routerMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getRouterModels())) {
            model.getRouterModels().forEach(r -> routerMap.put(r.getRouterUuid(), r));
        }
        model.setRouterModels(new ArrayList<>(routerMap.values()));

        Map<String, SecurityGroupModel> groupMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getGroupModels())) {
            model.getGroupModels().forEach(r -> groupMap.put(r.getGroupUuid(), r));
        }
        model.setGroupModels(new ArrayList<>(groupMap.values()));

        Map<String, KeypairModel> keypairMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getKeypairModels())) {
            model.getKeypairModels().forEach(r -> keypairMap.put(r.getName(), r));
        }
        model.setKeypairModels(new ArrayList<>(keypairMap.values()));

        Map<String, FloatingIpModel> fipMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getFloatingIpModels())) {
            model.getFloatingIpModels().forEach(r -> fipMap.put(r.getFloatingIpUuid(), r));
        }
        model.setFloatingIpModels(new ArrayList<>(fipMap.values()));

        Map<String, FirewallModel> fwMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getFirewallModels())) {
            model.getFirewallModels().forEach(r -> fwMap.put(r.getFirewallUuid(), r));
        }
        model.setFirewallModels(new ArrayList<>(fwMap.values()));

        Map<String, FirewallPolicyModel> fwPolicyMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getFirewallPolicyModels())) {
            model.getFirewallPolicyModels().forEach(r -> fwPolicyMap.put(r.getPolicyUuid(), r));
        }
        model.setFirewallPolicyModels(new ArrayList<>(fwPolicyMap.values()));

        Map<String, FirewallRuleModel> fwRuleMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getFirewallRuleModels())) {
            model.getFirewallRuleModels().forEach(r -> fwRuleMap.put(r.getRuleUuid(), r));
        }
        model.setFirewallRuleModels(new ArrayList<>(fwRuleMap.values()));

        Map<String, VdcModel> vdcMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getVdcModels())) {
            model.getVdcModels().forEach(r -> vdcMap.put(r.getVdcId(), r));
        }
        model.setVdcModels(new ArrayList<>(vdcMap.values()));

        Map<String, VpcModel> vpcMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getVpcModels())) {
            model.getVpcModels().forEach(r -> vpcMap.put(r.getVpcId(), r));
        }
        model.setVpcModels(new ArrayList<>(vpcMap.values()));

        Map<String, ManageOneProjectModel> projectMap = new HashMap<>();
        if (!ListTool.isEmpty(model.getManageOneProjectModels())) {
            model.getManageOneProjectModels().forEach(r -> projectMap.put(r.getProjectUuid(), r));
        }
        model.setManageOneProjectModels(new ArrayList<>(projectMap.values()));

        return model;
    }
}
